2

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

我创建了两个类: Gui(contains 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);
    }

}


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

4 回答 4

5

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

改变

 /* BUTTONS *
     *         */
    JButton btnBrowse = new JButton("Browse");
    panel_1.add(btnBrowse);

 /* BUTTONS *
     *         */
    btnBrowse = new JButton("Browse");
    panel_1.add(btnBrowse);
于 2013-06-14T09:25:58.707 回答
2

这个:

 if(event.getSource() == btnBrowse)

应该

 if(event.getSource().equals(btnBrowse))

在java中你不能使用==来标识一个相等的对象,你总是必须使用equals()来确保两个对象是相同的。

这个:

 JButton btnBrowse = new JButton("Browse");

应该

 btnBrowse = new JButton("Browse");

您正在使用本地变量来隐藏您的类成员变量,因此 if 子句始终与空值相比较。btnBrowse 永远不会存储在您的班级中。

于 2013-06-14T09:12:51.677 回答
1

我会说在 actionPerformed 方法的入口处添加一个 System.out.println 并检查它是否确实被调用。

此外,最好对每个按钮使用操作命令,这样您就不必检查事件源是否相等。像这样的东西:

btnBrowse.setActionCommand("Browse"); //before attaching the listener

然后在 actionPerformed

String actionCommand = event.getActionCommand();
if("Browse".equals(actionCommand)) {
   JFileChooser fileChooser = new JFileChooser();
   int retVal = fileChooser.showOpenDialog(null);
}
于 2013-06-14T09:20:53.573 回答
0

浏览按钮应该作为文件选择器的参数给出。

int returnVal = fc.showOpenDialog(log);

应该,

 int returnVal = fc.showOpenDialog(btnBrowse); 
于 2013-06-14T09:20:22.613 回答