所以我试图从 JDialog 中获取用户的输入,这是 JDialog 中的代码:
public void addPipeUI(){
Integer[] grade = {1,2,3,4,5};
Integer[] colour = {0, 1, 2};
float length;
int diameter;
boolean chemResist, innerIns, outerRein;
GridLayout gridLayout = new GridLayout(0,2);
JDialog dialog = new JDialog();
dialog.setSize(400, 400);
dialog.setLocation(250, 250);
dialog.setTitle("Add Pipe");
dialog.setLayout(gridLayout);
dialog.setVisible(true);
dialog.add(new JLabel("Grade"));
JComboBox gradeField = new JComboBox(grade);
dialog.add(gradeField);
dialog.add(new JLabel("Colour"));
JComboBox colourField = new JComboBox(colour);
dialog.add(colourField);
dialog.add(new JLabel("Length (Meters)"));
JTextField lengthField = new JTextField();
dialog.add(lengthField);
dialog.add(new JLabel("Diameter (Inches)"));
JTextField diameterField = new JTextField();
dialog.add(diameterField);
JRadioButton innerInsField = new JRadioButton("Inner Insluation");
dialog.add(innerInsField);
JRadioButton outerReinField = new JRadioButton("Outer Reinforcement");
dialog.add(outerReinField);
JRadioButton chemResistField = new JRadioButton("Chemical Resistance");
dialog.add(chemResistField);
JButton ok = new JButton("OK");
dialog.add(ok);
}
在顶部,您可以看到我希望用户输入哪些信息。
当按下 OK 按钮时,我想让局部变量等于用户输入的内容,然后将这些变量返回给我的 Main 类进行处理。 我觉得我需要在 OK 按钮上设置一个动作侦听器,但是当我这样做时,不能使用局部变量,现在我很困惑。如何使局部变量等于用户输入的内容?