gpt4 book ai didi

java - 如何根据某种特征有效地引用指定的按钮

转载 作者:行者123 更新时间:2023-12-02 11:05:33 25 4
gpt4 key购买 nike

这个问题与此类似:How do you reference a button inside of its actionlistener?

我想创建一个包含 8 个切换按钮的简单表单。如果我选择切换按钮并单击保存按钮,它将写入文本文件,即“按钮 x,打开”。下次我打开表单时,表单将在记事本中检查按钮 x 是否已打开。如果打开,则切换按钮将已被选中,反之亦然。

我知道如何在记事本中写入和读取,但我不确定如何检查用户是否选择按钮 2,然后代码将写入第二行“Button2, on”

这是我迄今为止要编写的代码:

Path path = Paths.get(csvFile);

// check if button x is selected, if yes : <- how to refer to button x ?
BufferedWriter bw = new BufferedWriter(New FileWriter(csvFile, true);
writer.write ("button x,on" + "\r\n");
writer.close

这是我打开表单时的代码:

BufferedReader br = null;
String line = "";
String resFilesplitby = ",";
br = new BufferedReader(new FileReader(csvFile));

while((line = br.readLine()) != null){
String[] condition = line.split(csvFilesplitby);
String power = condition[1];

// check if button x is already selected
if (button x power.equals("on")){
button x.isSelected();
}
}

最佳答案

我设法找到了解决问题的简单方法通过将按钮添加到数组中。

JToggleButton[] buttons = new JToggleButton[8];

buttons[0] = seat1; //this is variable name of my button.
buttons[1] = seat2;
buttons[2] = seat3;
buttons[3] = seat4;
buttons[4] = seat5;
buttons[5] = seat6;
buttons[6] = seat7;
buttons[7] = seat8;

// do the work here
for (JToggleButton btn : buttons){
if(btn.isSelected){

}
}

关于java - 如何根据某种特征有效地引用指定的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000363/

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