如何将GUI类中的实际复选框状态(true/false)传递给另一个类?我只想在GUI中选择复选框的情况下运行部分代码。我想它必须是如果声明(下面高亮部分),但我不能得到它的工作。另一个类中的Java复选框状态
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);
请改善您的问题,使其更容易回答,而无需猜测。您不会显示运行“TextAreaLogProgram”类的位置或方式,也不会显示您创建“TextAreaLogProgram”对象的位置,并且发布的代码格式不好,从而使代码难以阅读和遵循。你真的想让人们很容易帮忙,所以这样做对你最有利。另请参阅[帮助]的如何解答问题部分,了解如何改进问题。 –
但话虽如此,这个问题归结为检查一个对象的状态,另一个对象的关键是***引用***,获得另一个对象的引用。此外,JCheckBox不应该是局部变量,而应该是TextAreaLogProgram类的实例字段。 –
我编辑并格式化了您的发布代码。 –