gpt4 book ai didi

Java CheckBox 可选择,但 isSelected() 返回 false

转载 作者:行者123 更新时间:2023-12-01 20:24:48 26 4
gpt4 key购买 nike

所以我几个小时以来一直在寻找解决方案,但我就是找不到我的错误。我使用循环创建了 JCheckBoxes 并将其添加到滚动面板中。但如果我检查它们,isSelected() 总是返回 false。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Conn_Fak_GUI extends JFrame{
public static ArrayList<String> fields;
public Conn_Fak cf;
public ArrayList <JCheckBox> cb;

public Conn_Fak_GUI()
{
this.setTitle("AMAG - Schnittstelle");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);


JLabel db_fak = new JLabel("Pfad zur Amicron - Datenbank: ");
db_fak.setBounds(10,10,200,20);
this.add(db_fak);

JTextArea input_db_fak = new JTextArea();
input_db_fak.setBounds(10,30,300,20);
this.add(input_db_fak);

JLabel usernameinfo = new JLabel("Benutzername:");
usernameinfo.setBounds(10,60,100,20);
this.add(usernameinfo);

JTextField username = new JTextField();
username.setBounds(110,60,100,20);
this.add(username);

JLabel passwordinfo = new JLabel("Passwort:");
passwordinfo.setBounds(10,80,100,20);
this.add(passwordinfo);

JTextField password = new JTextField();
password.setBounds(110,80,100,20);
this.add(password);


JButton connect = new JButton("Verbinden");
connect.setBounds(350,80,100,20);
connect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{

try {
cf = new
Conn_Fak(input_db_fak.getText(),username.getText(),password.getText());
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fields= cf.getFields();
displayFields();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
displayFields();
}
});


JButton export = new JButton("Exportieren");
export.setBounds(350,360,100,20);
export.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println(cb.get(1).isSelected());
ExportData ed = new ExportData(fields,cf,cb);
try {
ed.exportToFile();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
this.add(export);
this.add(connect);

//FileChooser
JLabel ofile = new JLabel("Zieldatei: ");
ofile.setBounds(10,340,200,20);
this.add(ofile);

JTextArea input_ofile = new JTextArea();
input_ofile.setBounds(10,360,300,20);
this.add(input_ofile);

this.setVisible(true);
}
public void displayFields()
{
JCheckBox jb;
cb = new ArrayList<JCheckBox>();


for(int i = 0; i < fields.size(); i++)
{
jb = new JCheckBox();
jb.setText(fields.get(i));
cb.add(jb);
}




JPanel contentPane = new JPanel();
JPanel listOfFiles = new JPanel();
listOfFiles.setLayout(new BoxLayout(listOfFiles, BoxLayout.Y_AXIS));


for(int i = 0; i < cb.size(); i++){
listOfFiles.add(cb.get(i));
}

JScrollPane jScrollPane = new JScrollPane(listOfFiles);





jScrollPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setPreferredSize(new Dimension (300, 200));
contentPane.setBounds(10,120,300,200);
contentPane.add(jScrollPane);

this.add(contentPane);
this.revalidate();
this.repaint();

}


}

import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JCheckBox;

public class ExportData {
ArrayList <String> activatedFields;
ArrayList <String> fields;
ArrayList <JCheckBox> cb;
Conn_Fak cf;

public ExportData(ArrayList<String> fields, Conn_Fak cf,ArrayList<JCheckBox>
cb){
super();
this.fields = fields;
this.cf = cf;
this.cb = cb;
}

public void exportToFile() throws SQLException{
System.out.println(cb.size());
if(!cb.get(1).isSelected()){
System.out.println(cb.get(1));
}


for(int i = 0; i < cb.size(); i++){
if(cb.get(i).isSelected()){
System.out.println(cb.get(i).getText());
activatedFields.add(cb.get(i).getText());
}

}
for(int j = 0; j < activatedFields.size();j++){
System.out.println("+"+activatedFields.get(j));
}
cf.getData(activatedFields);


}


}

希望你能帮助我。如果您需要更多代码,我会将其发布。

最佳答案

您的问题不清楚,因为您的代码不完整。

但是我对 ArrayList 进行了硬编码并创建了一些复选框。我做了很小的改变,它对我有用。

请引用以下部分代码:

for(int i = 0; i < fields.size(); i++)
{
final String s = fields.get(i);
final JCheckBox jb = new JCheckBox(s);

jb.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if(jb.isSelected())
JOptionPane.showMessageDialog(getParent(), s +"Selected");

}
});
cb.add(jb);
}

工作屏幕截图:

enter image description here

关于Java CheckBox 可选择,但 isSelected() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44045200/

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