gpt4 book ai didi

java - 如何获取 JCheckBox 的状态,哪些组件位于 VerticalBox 中?

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:48 25 4
gpt4 key购买 nike

我正在创建一个脚本,您可以在其中选择一个文件夹,您可以通过 JCheckBox 选择其中的组件并将它们复制到另一个文件夹。我的问题是,我使用 VerticalBox 来存储文件名并将 JCheckBox 添加到其中,所以我不知道如何获取单个 CheckBox 的状态,因为它的变量被每个新文件覆盖,我只能从最后列出的文件中读取它。

这是(未完成的)代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

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

public class CopyGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private Box box;
private File fromFile;
private File toFile;
private File folder;
private File[] listOfFiles;
private JButton beginButton;
private JButton fromButton;
private JButton toButton;
private JCheckBox checkBox;
private JLabel fromLabel;
private JLabel toLabel;
private JPanel panel;
private JScrollPane scrollPane;
private JTextField fromField;
private JTextField toField;

public CopyGUI() {
init();
}

private void init() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200, 200, 420, 700);
setResizable(false);
setTitle("File Copy");

panel = new JPanel(null);
add(panel);

fromLabel = new JLabel("From:");
fromLabel.setBounds(40, 20, 50, 20);

fromField = new JTextField();
fromField.setBounds(100, 20, 200, 20);

fromButton = new JButton("Browse");
fromButton.setBounds(300, 20, 80, 20);
fromButton.addActionListener(this);

toLabel = new JLabel("To: ");
toLabel.setBounds(40, 40, 50, 20);

toField = new JTextField();
toField.setBounds(100, 40, 200, 20);

toButton = new JButton("Browse");
toButton.setBounds(300, 40, 80, 20);
toButton.addActionListener(this);

panel.add(fromLabel);
panel.add(fromField);
panel.add(fromButton);

panel.add(toLabel);
panel.add(toField);
panel.add(toButton);

beginButton = new JButton("Begin Copy");
beginButton.setBounds(280, 620, 100, 20);
beginButton.addActionListener(this);

panel.add(beginButton);

scrollPane = new JScrollPane();
scrollPane.setBounds(40,80,340,520);

box = Box.createVerticalBox();

scrollPane = new JScrollPane(box);
scrollPane.setBounds(40,80,340,520);

panel.add(scrollPane);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == fromButton) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int op = fileChooser.showOpenDialog(this);
if (op == JFileChooser.APPROVE_OPTION) {
fromFile = fileChooser.getSelectedFile();
fromField.setText(fromFile.getAbsolutePath() + "\\");
folder = new File(fromFile.getAbsolutePath());
listOfFiles = folder.listFiles();
}

box.removeAll();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
checkBox = new JCheckBox(listOfFiles[i].getName());
box.add(checkBox);
}
else if (listOfFiles[i].isDirectory()) {
}
}

panel.revalidate();
panel.repaint();
}

if (e.getSource() == toButton) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int op = fileChooser.showOpenDialog(this);
if (op == JFileChooser.APPROVE_OPTION) {
toFile = fileChooser.getSelectedFile();
toField.setText(toFile.getAbsolutePath() + "\\");
}
}

if (e.getSource() == beginButton) {

System.out.println(checkBox.isSelected());

}
}

public static void main(String[] args) {
new CopyGUI().setVisible(true);
}
}

最佳答案

您需要保留对您添加的每个 JCeckBox 的引用:

    JCheckBox checkBox = new JCheckBox(listOfFiles[i].getName());
box.add(checkBox);

//you could keep reference in a List such as
List<JCheckBox> listOfChkBox = new ArrayList<>(); //class variable
listOfChkBox.add(checkBox);

//or a map
Map<String,JCheckBox> mapOfChkBox = new HashMap<>(); //class variable
mapOfChkBox.add(listOfFiles[i].getName(), checkBox);

关于java - 如何获取 JCheckBox 的状态,哪些组件位于 VerticalBox 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46496298/

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