【发布时间】:2017-09-13 21:28:00
【问题描述】:
如何将实际的复选框状态(真/假)从 GUI 类传递到另一个类? I want to run some part of code only if checkbox in GUI is selected.我想它必须是 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);
【问题讨论】:
-
请改进您的问题,使其更容易回答而无需猜测。您没有显示在何处或如何运行
TextAreaLogProgram类,没有显示在何处创建TextAreaLogProgram对象,并且您发布的代码格式不好,使得代码难以阅读和遵循。你真的想让人们更容易地提供帮助,所以做这些事情符合你的最大利益。此外,请浏览help center 的“如何提问”部分,了解有关如何改进问题的更多信息。 -
但是话虽如此,问题归结为检查一个对象的状态另一个对象,而关键是references,得到一个对象在另一个对象中的引用。此外,JCheckBox 不应是局部变量,而应是 TextAreaLogProgram 类的实例字段。
-
我已经编辑并格式化了您发布的代码。
标签: java swing class jcheckbox