gpt4 book ai didi

java - 选择单选按钮后如何关闭 JOptionPane?

转载 作者:行者123 更新时间:2023-11-30 03:57:26 27 4
gpt4 key购买 nike

我是编程新手,上周刚刚开始,所以所有的 java mumbojumbo 对我来说都很困惑。我能够为我的 BMI 程序创建一个选项 Pane ,询问带有单选按钮的单位系统(公制/英制),这决定了在查找 BMI 时要执行的计算。这一切都工作正常,除了第一个选项 Pane 在选择选项时不会关闭,我如何使其在选择选项时关闭。我希望带有单选按钮的 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!");
}
}
}

最佳答案

在这里用这个问题的答案进行了一些修改

Closing A JOptionPane Programatically

这似乎有效。

我创建了一个 JButton 以添加到 JOptionPane 中的对象列表中。并实现了一个监听器,它将响应某人单击“确定”的事件。

这将为用户提供一些事情可做,以便他们知道他们正在使用正确的测量并且他们已经正确地单击了该选项。

将其复制到原始 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);

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

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

关于java - 选择单选按钮后如何关闭 JOptionPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com