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

huangapple go评论79阅读模式
英文:

Problem while trying to open file chooser Java

问题

我正在使用IntelliJ Idea 2020.2.1 IDE进行我的项目开发。

我设计了一个简单的界面,其中包含一个文件选择器以选择文件。

我已经创建了这个界面,但是我不确定这是否是正确的创建方式。我通过“Swing UI Designer->GUI FORM”创建了一个GUI表单。但是我无法在界面中使用这些变量。我需要创建另一个JButton、JPanel等等。

这是我的GUI表单界面的外观。

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

以下是我的代码。

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("文件选择界面");
        jFrame.setSize(400, 400);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton selectSubtitleButton = new JButton("选择文件");
        SubtitleUploadPage subtitleUploadPage = new SubtitleUploadPage();
        selectSubtitleButton.addActionListener(subtitleUploadPage);
        JPanel fileUploadPanel = new JPanel();
        fileUploadPanel.add(selectSubtitleButton);
        pathText = new JLabel("未选择文件");
        fileUploadPanel.add(pathText);
        jFrame.add(fileUploadPanel);
        jFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String com = e.getActionCommand();
        if (com.equals("选择文件")) {
            JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            j.setAcceptAllFileFilterUsed(false);
            j.setDialogTitle("选择一个 .srt 文件");
            FileNameExtensionFilter restrict = new FileNameExtensionFilter("仅限 .srt 文件", "srt");
            j.addChoosableFileFilter(restrict);
            int r = j.showOpenDialog(null);
            if (r == JFileChooser.APPROVE_OPTION) {
                pathText.setText(j.getSelectedFile().getAbsolutePath());
            } else
                pathText.setText("用户取消了操作");
        }
    }
}

这是结果。如您所见,它按预期工作。

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

但是正如您所见,我又创建了JButton和JPanel。因此,我无法使用预先创建的那些。

当我尝试将预定义的JPanel更改为静态的,如下所示:

private static JPanel fileUploadPanel;

它会出现错误:“无法绑定:字段是静态的:com.convert.utf8.SubtitleUploadPage.fileUploadPanel”。

如果我不插入静态关键字,我无法直接使用这个JPanel,因为主方法是静态的。

我知道我的代码可以工作,但我认为这不是正确的创建方式。我搜索了很多,找到了一些示例,但它们都扩展了JFrame,我相当肯定这也不是正确的方式。

请问有人能够向我展示如何从GUI表单直接使用GUI元素(如JButton、JPanel等)的正确方式吗?提前感谢。

英文:

I am using Intellij Idea 2020.2.1 IDE for my project.

I design a simple screen which includes a file chooser to select a file.

I made it but I am not sure it is proper way to make it. I created a GUI form via "Swing UI Designer->GUI FORM". But I couldn't use this variables for screen. I need to create another Jbutton , jPanel etc.

Here is my GUI Form screen looks like.

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

And this my code.

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");
}
}
}

And here is the result. As you can see it works as intended.

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

But as you can see I created Jbutton and Jpanel again. So I can't use pre-created ones.

When I try to change pre-defined Jpanel to static like :

private static JPanel fileUploadPanel;

It gives error "Cannot bind: field is static: com.convert.utf8.SubtitleUploadPage.fileUploadPanel"

If I didn't insert static keyword I can't use this Jpanel directly because main method is static.

I know my code works but I think it is not the proper way to make it. I searched it a lot I found some examples but they extends JFrame and I am pretty sure it is not the correct way either.

Could someone please show me the correct way to use a GUI element (Jbutton, Jpanel etc.) from GUI Form directly ? Thanks in advance

答案1

得分: 0

我最终找到了解决方法。以下是我最初想要创建的代码部分。

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);
    }
}
英文:

I figure it out finally. Here is the code which I want to create at first place.

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);
}
}

huangapple
  • 本文由 发表于 2020年10月26日 21:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64538041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定