如何将实际的复选框状态(true / false)从GUI类传递到另一个类?我只想在选择GUI中的复选框时运行部分代码。我想它必须是if语句(下面的高亮部分),但我不能让它工作。
public class csvtoxls {
public static void main() throws IOException {
//here you enter the path to your directory.
//for example: Path workDir = Paths.get("C:\\Users\\Kamil\Desktop\\csvtoxlspython\\Nowy folder (2)")
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Wybierz folder do konwersji: ");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
if (jfc.getSelectedFile().isDirectory()) {
System.out.println("You selected the directory: " + jfc.getSelectedFile());
String z;
//@SuppressWarnings("deprecation")
Path workDir = jfc.getSelectedFile().toPath();
System.out.println(workDir);
//Path workDir = FileSystems.getDefault(jfc.getCurrentDirectory()).jfc.getCurrentDirectory();
//Path workDir = Paths.get(gui.pickPath(jfc));
File dirg = jfc.getSelectedFile();
//String str = dirg.getPath();
// ************* CODE WITH ISSUE *************
if TextAreaLogProgram.checkbox.isSelected() {
try {
Thread.sleep(5000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String str = dirg.getPath();
delfiles td = new delfiles();
td.deleteFiles(str + "/", ".csv");
System.out.println("SUCCESS!");
msgbox.infoBox("SUCCES!", "CSVtoXLS");
}
GUI类:
public class TextAreaLogProgram extends JFrame {
private JTextArea textArea;
private JButton buttonStart = new JButton("CONVERT");
private JButton buttonClear = new JButton("CLEAR");
private PrintStream standardOut;
public TextAreaLogProgram() {
super("CSVtoXLS");
JCheckBox checkbox = new JCheckBox();
add(checkbox);
checkbox.setText("Delete files");
checkbox.setSelected(true);
答案 0 :(得分:2)
你的另一个类需要一个带参数的方法或构造函数才能接受来自另一个类的值
有关详细信息,请参阅Passing Information to a Method or a Constructor
其他问题:
Thread.sleep
,这对于Swing GUI来说效果不佳,因为这会使整个GUI处于睡眠状态,从而使其无响应。如果你想要Swing延迟,请使用Swing Timer(google上的优秀教程)