这个JFileChooser有什么问题

yea*_*amf 2 java user-interface swing jfilechooser

我创建了简单的gui应用程序,JFilechooser用于打开pdf文件.gui有一个浏览按钮和一个textArea来可视化文件的内容.

我创建了两个类:Gui(包含main())和GuiJFrame来实现gui,处理程序和监听器.我设法获得了窗口应用程序,但浏览按钮似乎不起作用.我不知道我哪里弄错了,请帮帮我

import java.awt.EventQueue;

public class Gui {
    /** Launch the application. */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Gui window = new Gui();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** Create the application. */
    public Gui() {
        initialize();
    }

    /** Initialize the contents of the frame. */
    private void initialize() {
        GuiJFrame guiJFrame = new GuiJFrame();
        guiJFrame.setVisible(true);
    }

}
Run Code Online (Sandbox Code Playgroud)


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class GuiJFrame extends JFrame {

    private JButton btnBrowse;
    private JTextArea   log, filtered_log;

    public GuiJFrame() {
        this.setTitle("TikaExtractorGui");
        this.setBounds(100, 100, 700, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel panel_1 = new JPanel();
        this.getContentPane().add(panel_1, BorderLayout.NORTH);
        JPanel panel_2 = new JPanel();
        this.getContentPane().add(panel_2, BorderLayout.CENTER);

        /*
         * BUTTONS *
         */
        JButton btnBrowse = new JButton("Browse");
        panel_1.add(btnBrowse);
        /*
         * Text_Areas*
         */
        JTextArea log = new JTextArea(50, 30);
        panel_2.add(log);
        log.setEditable(false);
        /*
         * LAYOUT *
         */
        setLayout(new FlowLayout());
        add(panel_1, FlowLayout.CENTER);
        add(panel_2, FlowLayout.LEFT);

        JScrollPane logScrollPane1 = new JScrollPane(log);
        logScrollPane1.setSize(300, 300);
        add(logScrollPane1);

        /*
         * Setting the handlers *
         */
        ActionHandler a_handler = new ActionHandler();
        btnBrowse.addActionListener(a_handler);

    }

    private class ActionHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            TikaExtractorInterface ex = new TikaExtractor();
            PDFParser parser = new PDFParser();
            String g = null;

            if (event.getSource() == btnBrowse) {
                final JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(log);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    if (file.getName().endsWith("pdf")) {
                        file.getPath();
                        if (file != null) {
                            try {
                                g = ex.extractFromFile(parser, file);
                                log.setText(g);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mak*_*kky 5

我知道它为什么不起作用.

更改

 /* BUTTONS *
     *         */
    JButton btnBrowse = new JButton("Browse");
    panel_1.add(btnBrowse);
Run Code Online (Sandbox Code Playgroud)

 /* BUTTONS *
     *         */
    btnBrowse = new JButton("Browse");
    panel_1.add(btnBrowse);
Run Code Online (Sandbox Code Playgroud)

  • 在队友身上发现.他为浏览按钮声明了一个实例变量,然后在构造函数中再次创建了一个新对象. (2认同)