如何在程序继续 运行 in java 之前等待事件完成
How to wait for an event to complete, before the program continue running in java
我正在尝试使用 JFileChooser 获取路径,然后继续执行程序。首先,会出现一个框架并提示用户按下一个按钮。按下按钮时,会出现 JFileChooser window 以便用户可以选择目录。
问题是这样的:当按下按钮时,程序不等待用户选择目录,尽管 JFileChooser window 出现。它继续执行下一个命令并抛出 NullPointerException。为了弄清楚,我添加了 promtENTERKey 方法,因此程序停止并等待按下 "ENTER" 键。
很好,但我不想按 "ENTER" 键。我希望程序在用户选择目录 后 继续,而不按任何键。我检查过类似的问题,但我做不到。我是 java 的新手(当然我的代码得到了很多帮助)。有什么想法吗?
我的代码如下:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DirChooser extends JPanel implements ActionListener {
JButton button;
static JFileChooser chooser = null;
public DirChooser() {
button = new JButton("Select Directory");
button.addActionListener (this);
add(button);
}
public static void main (String [] args) {
String path = getDir();
// ... code ...
}
public static String getDir() {
String dir = null;
JFrame frame = new JFrame("");
DirChooser panel = new DirChooser();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
promtENTERKey();
dir = chooser.getSelectedFile().getAbsolutePath();
frame.dispose();
return dir;
}
public Dimension getPreferredSize(){
return new Dimension(200, 100);
}
public void actionPerformed (ActionEvent e) {
chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File("."));
chooser.setDialogTitle ("choose directory");
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed (false); // disable the "All files" option.
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println ("\n" + "Directory has been selected");
}
else {
System.out.println("No Selection ");
}
}
public static void promtENTERKey() {
System.out.println("\n" + "Select directory first and then press \"ENTER\" to continue...");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
}
以下 mre 是否展示了您正在努力实现的目标?
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class DirChooser {
private static String path;
public static void main (String [] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Directory");
button.addActionListener (e-> {
path = getDir();
System.out.println ("File chooser returned Path: " + path);
});
frame.add(button);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static String getDir() {
JFileChooser chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File("."));
chooser.setDialogTitle ("choose directory");
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed (false); // disable the "All files" option.
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
return chooser.getSelectedFile().getAbsolutePath();
return null;
}
}
通常,我认为下一个动作应该从你的动作监听器开始。为了适应您提出的框架,我们可以制作一个倒计时闩锁来阻止而不是使用扫描器。
CountDownLatch latch = new CountDownLatch(1);
public void actionPerformed (ActionEvent e) {
//code to choose file.
latch.countDown();
}
现在您可以将您的 getEnter 替换为
latch.await();
我正在尝试使用 JFileChooser 获取路径,然后继续执行程序。首先,会出现一个框架并提示用户按下一个按钮。按下按钮时,会出现 JFileChooser window 以便用户可以选择目录。
问题是这样的:当按下按钮时,程序不等待用户选择目录,尽管 JFileChooser window 出现。它继续执行下一个命令并抛出 NullPointerException。为了弄清楚,我添加了 promtENTERKey 方法,因此程序停止并等待按下 "ENTER" 键。
很好,但我不想按 "ENTER" 键。我希望程序在用户选择目录 后 继续,而不按任何键。我检查过类似的问题,但我做不到。我是 java 的新手(当然我的代码得到了很多帮助)。有什么想法吗?
我的代码如下:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DirChooser extends JPanel implements ActionListener {
JButton button;
static JFileChooser chooser = null;
public DirChooser() {
button = new JButton("Select Directory");
button.addActionListener (this);
add(button);
}
public static void main (String [] args) {
String path = getDir();
// ... code ...
}
public static String getDir() {
String dir = null;
JFrame frame = new JFrame("");
DirChooser panel = new DirChooser();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
promtENTERKey();
dir = chooser.getSelectedFile().getAbsolutePath();
frame.dispose();
return dir;
}
public Dimension getPreferredSize(){
return new Dimension(200, 100);
}
public void actionPerformed (ActionEvent e) {
chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File("."));
chooser.setDialogTitle ("choose directory");
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed (false); // disable the "All files" option.
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println ("\n" + "Directory has been selected");
}
else {
System.out.println("No Selection ");
}
}
public static void promtENTERKey() {
System.out.println("\n" + "Select directory first and then press \"ENTER\" to continue...");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
}
以下 mre 是否展示了您正在努力实现的目标?
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class DirChooser {
private static String path;
public static void main (String [] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Directory");
button.addActionListener (e-> {
path = getDir();
System.out.println ("File chooser returned Path: " + path);
});
frame.add(button);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static String getDir() {
JFileChooser chooser = new JFileChooser ();
chooser.setCurrentDirectory (new java.io.File("."));
chooser.setDialogTitle ("choose directory");
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed (false); // disable the "All files" option.
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
return chooser.getSelectedFile().getAbsolutePath();
return null;
}
}
通常,我认为下一个动作应该从你的动作监听器开始。为了适应您提出的框架,我们可以制作一个倒计时闩锁来阻止而不是使用扫描器。
CountDownLatch latch = new CountDownLatch(1);
public void actionPerformed (ActionEvent e) {
//code to choose file.
latch.countDown();
}
现在您可以将您的 getEnter 替换为
latch.await();