我是编程的新手,上周刚开始,所以所有的java mumbojumbo对我来说都很混乱。我能够为我的BMI程序创建一个选项窗格,询问哪个单位系统(公制/英制)有无线电按钮,这决定了在找到BMI时要执行的计算。这一切都很好,除了选择一个选项时第一个选项窗格不关闭,如何选择一个选项时关闭它。我希望带有radiobuttons的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!");
}
}
}
答案 0 :(得分:2)
在这里用这个问题的答案解决了一下
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);
另外,当您没有单击“确定”但选择了单选按钮时,单击“关闭”按钮后程序将继续运行。 所以这将是你要做的事情。
在提出问题之前,尝试通过网站找到问题的答案。但无论如何,我很乐意帮助你。