Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试打开文件选择器Java时出现问题_Java_User Interface_Intellij Idea - Fatal编程技术网

尝试打开文件选择器Java时出现问题

尝试打开文件选择器Java时出现问题,java,user-interface,intellij-idea,Java,User Interface,Intellij Idea,我正在为我的项目使用Intellij Idea 2020.2.1 IDE 我设计了一个简单的屏幕,其中包括一个选择文件的文件选择器 我做到了,但我不确定这是正确的方法。我通过“SwingUIDesigner->GUI表单”创建了一个GUI表单。但我不能在屏幕上使用这个变量。我需要创建另一个Jbutton、jPanel等 这是我的GUI窗体屏幕的外观 这是我的密码 public class SubtitleUploadPage implements ActionListener { privat

我正在为我的项目使用Intellij Idea 2020.2.1 IDE

我设计了一个简单的屏幕,其中包括一个选择文件的文件选择器

我做到了,但我不确定这是正确的方法。我通过“SwingUIDesigner->GUI表单”创建了一个GUI表单。但我不能在屏幕上使用这个变量。我需要创建另一个Jbutton、jPanel等

这是我的GUI窗体屏幕的外观

这是我的密码

public class SubtitleUploadPage implements ActionListener {
private JButton SelectSubtitleFileButton;
private JPanel fileUploadPanel;
static JLabel pathText;

public SubtitleUploadPage() {
}

public static void main(String[] args) {
    JFrame jFrame = new JFrame("File Select Screen");
    jFrame.setSize(400, 400);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton selectSubtitleButton = new JButton("Select File");
    SubtitleUploadPage subtitleUploadPage = new SubtitleUploadPage();
    selectSubtitleButton.addActionListener(subtitleUploadPage);
    JPanel fileUploadPanel = new JPanel();
    fileUploadPanel.add(selectSubtitleButton);
    pathText = new JLabel("no file selected");
    fileUploadPanel.add(pathText);
    jFrame.add(fileUploadPanel);
    jFrame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    String com = e.getActionCommand();
    if (com.equals("Select File")) {
        JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        j.setAcceptAllFileFilterUsed(false);
        j.setDialogTitle("Select a .srt file");
        FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .srt files", "srt");
        j.addChoosableFileFilter(restrict);
        int r = j.showOpenDialog(null);
        if (r == JFileChooser.APPROVE_OPTION) {
            pathText.setText(j.getSelectedFile().getAbsolutePath());
        }
        else
            pathText.setText("the user cancelled the operation");
    }
}
}
这是结果。正如您所见,它按预期工作

但正如你所看到的,我再次创建了Jbutton和Jpanel。所以我不能使用预先创建的

当我尝试将预定义的Jpanel更改为静态时,如:

private static JPanel fileUploadPanel;
它给出错误“无法绑定:字段是静态的:com.convert.utf8.SubtitleUploadPage.fileUploadPanel”

若我并没有插入static关键字,我就不能直接使用这个Jpanel,因为main方法是static的

我知道我的代码是有效的,但我认为这不是正确的方法。我搜索了很多,找到了一些例子,但是它们扩展了JFrame,我很确定这也不是正确的方法


有人能告诉我从GUI表单直接使用GUI元素(Jbutton、Jpanel等)的正确方法吗?提前谢谢

我终于明白了。下面是我首先要创建的代码

public class SubtitleUploadPage {
private JButton SelectSubtitleFileButton;
private JPanel fileUploadPanel;
private JLabel fileLocationLabel;

public SubtitleUploadPage() {

    SelectSubtitleFileButton.addActionListener(e -> {
        String com = e.getActionCommand();
        System.out.println(com);
        if (com.equals("Select File")) {
            JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            fileChooser.setAcceptAllFileFilterUsed(false);
            fileChooser.setDialogTitle("Select a .srt file");
            FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .srt files", "srt");
            fileChooser.addChoosableFileFilter(restrict);
            int dialogResult = fileChooser.showOpenDialog(null);
            if (dialogResult == JFileChooser.APPROVE_OPTION) {
                fileLocationLabel.setText(fileChooser.getSelectedFile().getAbsolutePath());
            }
            else
                fileLocationLabel.setText("the user cancelled the operation");
        }
    });
}

public static void main(String[] args) {
    JFrame jFrame = new JFrame("File Select Screen");
    jFrame.setContentPane(new SubtitleUploadPage().fileUploadPanel);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.pack();
    jFrame.setVisible(true);
}
}