我創建了鏈接到檔案的動態 JChekboxes。我想看看通過動作監聽器檢查了哪些。我試過getSource(),我試過getState(),它們都沒有作業......
for (int f = 0; f < numberCheckBox[0]; f ) {
String line1 = tableLines[f].toString().trim();
String[] dataRow1 = line1.split("/");
checkBoxList[f] = new JCheckBox(Arrays.toString(dataRow1).replace("[", "").replace("]", ""));
Checkp.add(checkBoxList[f]);
System.out.print(checkBoxList[f]);
}
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(files));
Object[] tableLines = br.lines().toArray();
numberCheckBox[0] = tableLines.length;
for (int j = 0; j < numberCheckBox[0]; j ) {
if(e.getSource() == finalCheckBoxList[j]){
if(e.getStateChange() == 1){
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
uj5u.com熱心網友回復:
您要做的第一件事是查看如何使用按鈕、復選框和單選按鈕
所以來自JCheckBoxJavaDocs
被選中
公共布爾 isSelected()回傳按鈕的狀態。如果選擇了切換按鈕,則為 true,否則為 false。
回傳:
如果選擇了切換按鈕,則回傳 true,否則回傳 false
此外,如果您需要某種方式將資訊傳遞給ActionListener. 您可以繼續依賴index這些值,但如果可能的話,我總是更喜歡封裝這些資訊。
所以,在這個例子中,我使用了actionCommand和clientProperty方法來播種我可能想要在ActionListener
我確實使用了 isSelected ,但效果不佳。
那么你做錯了什么,因為下面的例子作業得很好
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
int[] numberCheckBox = new int[] { 5 };
JCheckBox[] checkBoxList = new JCheckBox[5];
String[] tableLines = new String[] {
"this/is/a/test",
"hello/world",
"something/wicked/this/way/comes",
"a/long/long/time/ago",
"i/have/something/to/say",
};
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.anchor = GridBagConstraints.LINE_START;
for (int f = 0; f < numberCheckBox[0]; f ) {
String line1 = tableLines[f].toString().trim();
String[] dataRow1 = line1.split("/");
checkBoxList[f] = new JCheckBox(String.join(", ", dataRow1));
checkBoxList[f].setActionCommand(line1);
checkBoxList[f].putClientProperty("value", line1);
add(checkBoxList[f], gbc);
System.out.print(checkBoxList[f]);
}
JButton save = new JButton("Save");
gbc.anchor = GridBagConstraints.CENTER;
add(save, gbc);
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JCheckBox cb : checkBoxList) {
if (cb.isSelected()) {
System.out.println("Action Command = " cb.getActionCommand());
System.out.println("Client Property = " (String)cb.getClientProperty("value"));
}
}
}
});
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429899.html
