【问题标题】:How do you close JOptionPane after a radiobutton has been selected?选择单选按钮后如何关闭 JOptionPane?
【发布时间】:2014-04-02 21:49:27
【问题描述】:

我是编程新手,上周刚开始,所以所有的 java mumbojumbo 都让我很困惑。我能够为我的 BMI 程序创建一个选项窗格,询问哪个单位制(公制/英制)带有单选按钮,这决定了在查找 BMI 时要执行的计算。这一切都很好,除了第一个选项窗格在选择选项时没有关闭,我如何在选择选项时关闭它。我希望带有单选按钮的 jpane 在 do 语句中关闭。

package javaapplication21;
import java.text.DecimalFormat;
import javax.swing.*;
import java.*;

public class JavaApplication21 {

    public static void main(String[] args) {
        JPanel jPanel = new JPanel();
        ButtonGroup group = new ButtonGroup();
        JRadioButton metricButton = new JRadioButton("Metric");
        metricButton.setActionCommand("Metric");
        JRadioButton imperialButton = new JRadioButton("Imperial");
        imperialButton.setActionCommand("Imperial");
        group.add(metricButton);
        group.add(imperialButton);
        jPanel.add(metricButton);
        jPanel.add(imperialButton);

        JOptionPane.showOptionDialog(null, "Please select prefered units", "BMI Calculator", JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null, new Object[] { metricButton, imperialButton}, null);

        DecimalFormat oneDigit = new DecimalFormat("#,##0.0");

        double bodyMassIndex, weight, height;
        String unitsWeight = "-1", unitsHeight = "-1";

        do{
            if (metricButton.isSelected()){
                unitsWeight = " in kg.";
                unitsHeight = " in meters";
            }  
            else if (imperialButton.isSelected()){
                unitsWeight = " in lbs";
                unitsHeight = " in inches";
            }
        }
        while ("-1".equals(unitsWeight));

        String weightInput = JOptionPane.showInputDialog("Please enter your weight" + unitsWeight);
        String heightInput = JOptionPane.showInputDialog("Please enter your height" + unitsHeight);

        if (metricButton.isSelected()){
            height = Double.parseDouble(heightInput);
            weight = Double.parseDouble(weightInput);
            bodyMassIndex = weight / (height * height);
            System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2"); 
             if (bodyMassIndex < 15)
                 System.out.println("You are starving");
             else if (bodyMassIndex < 18.5)
                 System.out.println("You are underweight");
             else if (bodyMassIndex < 25)
                 System.out.println("You are healthy");
             else if (bodyMassIndex < 30)
                 System.out.println("You are obese");
             else if (bodyMassIndex < 40)
                 System.out.println("You are morbidly obese");
             else 
                 System.out.println("You are at high risk of many health concerns");
        }
        else if (imperialButton.isSelected()){ 
            height = Double.parseDouble(heightInput);
            weight = Double.parseDouble(weightInput);
            bodyMassIndex = (weight * 703) / (height * height);
            System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2"); 
                 if (bodyMassIndex < 15)
                     System.out.println("You are starving");
                 else if (bodyMassIndex < 18.5)
                     System.out.println("You are underweight");
                 else if (bodyMassIndex < 25)
                     System.out.println("You are healthy");
                 else if (bodyMassIndex < 30)
                     System.out.println("You are obese");
                 else if (bodyMassIndex < 40)
                     System.out.println("You are morbidly obese");
            else
                 System.out.println("No more big macs!");
            }
      }
}

【问题讨论】:

    标签: java swing joptionpane jradiobutton


    【解决方案1】:

    在这里回答了这个问题

    Closing A JOptionPane Programatically

    而且它似乎奏效了。

    我创建了一个 JButton 以添加到 JOptionPane 中的对象列表中。 并实现了一个监听器,它会响应某人点击 OK 的事件。

    这将为用户提供一些操作,以便他们知道他们正在使用正确的测量值并且他们已经正确地点击了该选项。

    将其复制到您原来的 JOptionPane 上。

    JButton yesButton = new JButton("Ok");
    
        yesButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
    
                Window[] windows = Window.getWindows();
                for (Window window : windows) {
                    if (window instanceof JDialog) {
                        JDialog dialog = (JDialog) window;
                        if (dialog.getContentPane().getComponentCount() == 1
                                && dialog.getContentPane().getComponent(0) instanceof JOptionPane) {
                            dialog.dispose();
                        }
                    }
                }
    
            }
        });
    
        JOptionPane.showOptionDialog(null, "Please select prefered units",
                "BMI Calculator", JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, new Object[] {
                        metricButton, imperialButton, yesButton }, null);
    

    此外,当您未单击“确定”但选择了单选按钮时,单击关闭按钮后程序会继续运行。 所以这将是你需要做的事情。

    在您提出问题之前,请尝试通过网站找到问题的答案。但我很乐意提供帮助,所以还是谢谢你。

    【讨论】:

    • 非常感谢您的帮助我对此很陌生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2016-03-08
    • 2016-01-22
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多