我正在尝试创建一个带有多个复选框的JFrame,用户可以选择这些复选框(更改布尔数组中的值)以及提交按钮。按下Submit按钮时,该方法应返回boolean数组。但是,我无法弄清楚如何让代码等待提交被按下。我尝试了一些非常糟糕的解决方案,包括布尔和一个循环,但那些并不是一贯的工作,也不是正确的方法,我相当肯定。
代码如下:
public class UniversalUtility {
public void uI() {
boolean[] selections = utilitySelection();
for (boolean i : selections) {
System.out.println(i);
}
}
private boolean submitted;
public boolean[] utilitySelection() {
JFrame frame = new JFrame("Utility Selector");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0,1));
Border border = BorderFactory.createTitledBorder("Utilities");
panel.setBorder(border);
boolean[] selections = new boolean[4];
JCheckBox check1 = new JCheckBox("VS to VCF Friendly ");
panel.add(check1);
check1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selections[0] = true;
}
});
JCheckBox check2 = new JCheckBox("HGMD Fix");
panel.add(check2);
check2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selections[1] = true;
}
});
JCheckBox check3 = new JCheckBox("CADD Preparer");
panel.add(check3);
check3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selections[2] = true;
}
});
JCheckBox check4 = new JCheckBox("Line Chunks");
panel.add(check4);
check4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selections[3] = true;
}
});
JButton button = new JButton("Submit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
submitted = true;
frame.dispose();
}
});
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300,200);
frame.setVisible(true);
while (!submitted) {}
return selections;
}
public static void main(String[] args) {
(new UniversalUtility()).uI();
}
}
提前感谢您的帮助。