0
我有一個動態的應用程序,需要用複選框創建一個窗口界面。複選框的值將控制程序的後續步驟。java - 在特定時刻捕捉checkbutton的值
所以我有這個按鈕接口,我應該選擇我想要的按鈕,然後按下「運行」按鈕(在選擇的時刻程序必須等待)。 我該怎麼做?
(我不能使用HTML標籤一個服務器應用程序。這將是太容易了。)
這是我的代碼。問題是程序在按下RUN按鈕之前控制複選框的值。所以所有的按鈕狀態都會導致FALSE。
public class JRadiobuttonExample extends JFrame {
private JCheckBox radioButton1;
private JCheckBox radioButton2;
private JCheckBox radioButton3;
private JLabel label;
int i, state[] = {0,0,0,0};
JCheckBox b[];
static ArrayList<Boolean> check=new ArrayList<Boolean>();
JButton altervista=new JButton("RUN");
static int num;
public JCheckBox[] getB() {
return b;
}
public void setB(JCheckBox[] b) {
this.b = b;
}
public ArrayList<Boolean> getCheck() {
return check;
}
public void setCheck(ArrayList<Boolean> check2) {
check = check2;
}
public JRadiobuttonExample(int num, ArrayList<String> lbl) {
super("JRadiobuttonExample");
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
b= new JCheckBox[num];
CheckBoxListener ckListener = new CheckBoxListener();
for(i=0; i<num; i++) {
b[i] = new JCheckBox(lbl.get(i));
b[i].addItemListener(ckListener);
getContentPane().add(b[i]);
}
setVisible(true);
getContentPane().add(altervista);
getContentPane().setVisible(true);
Ascoltatore asc=new Ascoltatore();
altervista.addActionListener(asc);
}
class Ascoltatore extends WindowAdapter implements ActionListener{
public void windowClosing(WindowEvent e){
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==altervista){
}
}
}
public static void main(String argv[])
{
ArrayList<String> col=new ArrayList<String>();
col.add("hello");
col.add("everyone");
num=col.size();
JRadiobuttonExample b = new JRadiobuttonExample(num,col);
JCheckBox[] buttons=b.getB();
for (JCheckBox c:buttons){
check.add(c.isSelected());
}
System.out.println("checkbox values are:");
for (boolean sel:check){
System.out.println(sel);
}
}
}
所以問題在於那個命令? altervista.addActionListener(ASC); –
非常感謝..這是一個有用的提示! ..問題是在actionListener本身...我不得不調用下一個方法不是在主代碼中,但在方法ActionPerformed! –
是的。這個方法在'if(e.getSource()== altervista){'(這是不必要的)內部,應該檢查複選框的狀態並顯示下一個屏幕。 –