JFileChooser 的 showOpenDialog() 不打开用于打开文件的对话框

showOpenDialog() of JFileChooser doesn't open dialog box for opening files

我正在编写用于合并 pdf 文件的 GUI 实用程序,但 JFileChooser 的 showOpenDialog() 方法不会打开用于打开文件的对话框。单击文件菜单中的"Open"项时没有任何反应。为了您的方便,Apache pdfbox 库的下载 link 是: https://pdfbox.apache.org/download.cgi

我试图将 openFiles() 方法的内容移动到 actionPerformed() 方法中的相应位置,但它仍然不起作用。但是,在出于不同目的的另一个人的代码中,showOpenDialog() 方法在从 actionPerformed 方法内部调用的方法中工作。代码如下:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

public class PDFMerger extends JFrame implements ActionListener {
    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIGHT = 500;
    private JMenuItem openItem;
    private JMenuItem saveItem;
    private JMenuItem exitItem;
    private JTextArea textArea;
    private JTextField textField;
    private JButton bindButton;
    private File[] files;
    private File mergedFile;

    public static void main(String[] args) {
        PDFMerger pdfMerger = new PDFMerger();
        pdfMerger.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        pdfMerger.setDefaultCloseOperation(EXIT_ON_CLOSE);
        pdfMerger.setVisible(true);
    }

    public PDFMerger () {
        JMenu menu = new JMenu("File");
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        saveItem = new JMenuItem("Save");
        saveItem.setEnabled(false);
        saveItem.addActionListener(this);
        exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        menu.add(openItem);
        menu.add(saveItem);
        menu.add(exitItem);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        setJMenuBar(menuBar);
        textArea = new JTextArea(40, 50);
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane, BorderLayout.CENTER);
        JPanel operationPanel = new JPanel();
        BoxLayout layout = new BoxLayout(operationPanel, BoxLayout.LINE_AXIS);
        operationPanel.setLayout(layout);
        JLabel label = new JLabel("Result: ");
        textField = new JTextField(30);
        textField.setEditable(false);
        bindButton = new JButton("Bind");
        bindButton.addActionListener(this);
        bindButton.setEnabled(false);
        operationPanel.add(label);
        operationPanel.add(textField);
        operationPanel.add(bindButton);
        add(operationPanel, BorderLayout.NORTH);
    }

    public void actionPerformed (ActionEvent event) {

        if (event.getSource() == exitItem) 
            System.exit(0);
        else if (event.getSource() == openItem) {
            files = openFiles();
            bindButton.setEnabled(false);
        }
        else if (event.getSource() == bindButton) {
            mergedFile = mergeFiles();
            saveItem.setEnabled(true);
        }
        else if (event.getSource() == saveItem)
            saveFile();
    }

    public File[] openFiles () {
        File[] selectedFiles = null;
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setMultiSelectionEnabled(true);
        int option = chooser.showOpenDialog(PDFMerger.this);

        if (option == JFileChooser.APPROVE_OPTION) 
            selectedFiles = chooser.getSelectedFiles();
        return selectedFiles;
    }

    public File mergeFiles () {
        File merged = null;
        // TODO

        return merged;
    }
    public void saveFile () {
        //TODO
    }
}

感谢您的帮助。

我修复了您的代码并打开了 JFileChooser。

我所做的主要更改是:

  1. 我通过调用 SwingUtilities invokeLater 方法启动了 Swing GUI。此方法确保 Swing 组件在 Event Dispatch Thread.

  2. 上创建和执行
  3. 我用的是JFrame。我没有扩展 JFrame。只有当您打算重写 class 方法中的一种或多种时,您才可以扩展 Swing 组件。

  4. 您的代码难以阅读。我将代码划分为逻辑方法。我单独留下了您令人困惑的代码,但是当您创建 Swing 布局时,您确实应该按行、列顺序创建组件。您还应该将特定组件的所有方法调用组合在一起。它使查找和解决问题变得更加容易。

  5. 你不应该只有一个动作侦听器来处理所有事情。您应该为每个按钮或菜单项设置单独的动作侦听器。

无论如何,这是您的代码。一旦我从逻辑上将事物分开,就更容易找到阻止 JFileChooser 显示的错误。

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class PDFMerger implements ActionListener {

    private JFrame frame;
    private JMenuItem openItem;
    private JMenuItem saveItem;
    private JMenuItem exitItem;
    private JTextArea textArea;
    private JTextField textField;
    private JButton bindButton;
    private File[] files;
    private File mergedFile;

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

    public PDFMerger() {
        frame = new JFrame("PDF Merger");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = createJMenuBar();
        frame.setJMenuBar(menuBar);

        JPanel operationPanel = createOperationPanel();
        frame.add(operationPanel, BorderLayout.NORTH);
        JPanel textPanel = createTextPanel();
        frame.add(textPanel, BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createOperationPanel() {
        JPanel operationPanel = new JPanel();
        BoxLayout layout = new BoxLayout(operationPanel, BoxLayout.LINE_AXIS);
        operationPanel.setLayout(layout);
        JLabel label = new JLabel("Result: ");
        textField = new JTextField(30);
        textField.setEditable(false);
        bindButton = new JButton("Bind");
        bindButton.addActionListener(this);
        bindButton.setEnabled(false);
        operationPanel.add(label);
        operationPanel.add(textField);
        operationPanel.add(bindButton);
        return operationPanel;
    }

    private JPanel createTextPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        textArea = new JTextArea(40, 50);
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane, BorderLayout.CENTER);
        return panel;
    }

    private JMenuBar createJMenuBar() {
        JMenu menu = new JMenu("File");
        openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        saveItem = new JMenuItem("Save");
        saveItem.setEnabled(false);
        saveItem.addActionListener(this);
        exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        menu.add(openItem);
        menu.add(saveItem);
        menu.add(exitItem);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        return menuBar;
    }

    @Override
    public void actionPerformed (ActionEvent event) {
        if (event.getSource() == exitItem) {
            frame.dispose();
            System.exit(0);
        } else if (event.getSource() == openItem) {
            files = openFiles();
            bindButton.setEnabled(false);
        } else if (event.getSource() == bindButton) {
            mergedFile = mergeFiles();
            saveItem.setEnabled(true);
        } else if (event.getSource() == saveItem)
            saveFile();
    }

    public File[] openFiles () {
        File[] selectedFiles = null;
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setMultiSelectionEnabled(true);
        int option = chooser.showOpenDialog(frame);

        if (option == JFileChooser.APPROVE_OPTION) 
            selectedFiles = chooser.getSelectedFiles();
        return selectedFiles;
    }

    public File mergeFiles () {
        File merged = null;
        // TODO

        return merged;
    }
    public void saveFile () {
        //TODO
    }
}