简体   繁体   English

等待用户输入 Java Swing

[英]Wait for user input Java Swing

I am using JAVA Swing to create a very basic UI.我正在使用 JAVA Swing 创建一个非常基本的 UI。 When I run the program, a window will open with a message and browse button(using frame and JButtons for the same).当我运行程序时,将打开一个带有消息和浏览按钮的窗口(使用框架和 JButtons 相同)。 On click of browse button, another window will open to navigate to the file.单击浏览按钮,将打开另一个窗口以导航到该文件。 This I have achieved by calling a FileChooser on the click event of Browse button.这是我通过在浏览按钮的单击事件上调用 FileChooser 来实现的。 However, my program does not wait for user input.但是,我的程序不会等待用户输入。 The first window with browse button opens and program keeps on executing and ends up in an error as no file has been selected.带有浏览按钮的第一个窗口打开,程序继续执行并最终出现错误,因为没有选择文件。 How do I halt the execution till user input is provided?在提供用户输入之前如何停止执行? In a forum it was advised to use showOpenDialog() method of browser but that straightway opens a browsing window, whereas I want to give the provision to user to click on Browse button browsewindow pick file window在论坛中,建议使用浏览器的 showOpenDialog() 方法,但会直接打开浏览窗口,而我想为用户提供点击浏览按钮浏览窗口选择文件窗口的权限

My code is below我的代码在下面

    frame.setLayout(new FlowLayout());
    // set up a file picker component
    JFilePicker filePicker = new JFilePicker("Pick a file", "Browse...");
    filePicker.setMode(JFilePicker.MODE_OPEN);
    filePicker.addFileTypeFilter(".jpg", "JPEG Images");
    filePicker.addFileTypeFilter(".mp4", "MPEG-4 Videos");

    // access JFileChooser class directly
    JFileChooser fileChooser = filePicker.getFileChooser();
    fileChooser.setCurrentDirectory(new File("C:/"));
    // add the component to the frame
    frame.add(filePicker);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(520, 100);
    frame.setLocationRelativeTo(null);    // center on screen
    frame.setVisible(true);
    System.out.println();

JPicker is the custom class which creates a filechooser and sets things to be done on click of Browse button JPicker 是自定义类,它创建一个文件选择器并设置点击浏览按钮时要完成的事情

Of Course, You set the JFrame visible at the end of its' initialization.当然,您将 JFrame 设置为在其初始化结束时可见。 You need to do this within the main() method of your startup class.您需要在启动类的main()方法中执行此操作。 Where is yours?你的在哪里?

The JFilePicker (created by: Nam Ha Minh ) is applied to a JFrame as a Java Component in order to save a little time in GUI development. JFilePicker (由Nam Ha Minh创建)作为 Java 组件应用于 JFrame,以便在 GUI 开发中节省一点时间。 I personally would just use the JFileChooser directly within a JButton ActionPerformed event.我个人只会在 JButton ActionPerformed事件中直接使用 JFileChooser。 If you had followed the directions properly then you would see that you need a main() method which only makes sense.如果您正确地遵循了说明,那么您会发现您需要一个main()方法,该方法才有意义。 What your application Startup class should look like is something like this:您的应用程序 Startup 类应该是这样的:

import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestJFilePicker extends JFrame {

    private static final long serialVersionUID = 1L;

    public TestJFilePicker() {
        super("Test using JFilePicker");

        setLayout(new FlowLayout());

        // set up a file picker component
        JFilePicker filePicker = new JFilePicker("Pick a file", "Browse...");
        filePicker.setMode(JFilePicker.MODE_OPEN);
        filePicker.addFileTypeFilter(".jpg", "JPEG Images");
        filePicker.addFileTypeFilter(".mp4", "MPEG-4 Videos");

        // access JFileChooser class directly
        JFileChooser fileChooser = filePicker.getFileChooser();
        fileChooser.setCurrentDirectory(new File("D:/"));

        // add the component to the frame
        add(filePicker);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(520, 100);
        setLocationRelativeTo(null);    // center on screen
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestJFilePicker().setVisible(true);
            }
        });
    }

}

The above code (which is the work of Nam Ha Minh ) of course assumes that you have already applied the JFilePicker and the FileTypeFilter class files to your project.上面的代码(这是Nam Ha Minh的作品)当然假设您已经将JFilePickerFileTypeFilter类文件应用到您的项目中。 Without them the above code will not work.没有它们,上面的代码将无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM