JFilechooser无法正常使用选择按钮

时间:2013-12-11 03:34:14

标签: java swing jfilechooser

当我单击选择文件按钮或写入按钮时,不会发生任何事件。 按下选择按钮时,控制台中甚至不会出现错误。另一方面,写入按钮确实显示错误。

 public class Exam extends JPanel
    implements ActionListener {
    /**
         * 
         */
        private static final long serialVersionUID = 1L;
    static private String newline = "\n";
    private JTextArea log;
    private JFileChooser fc;
    private static JButton selBtn = new JButton("Select File");
    private static JButton writeBtn = new JButton ("Write File");




        public static void main (String[] args)
        {

            //create a JFrame object

            JFrame frame = new JFrame("QBStats App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //create a JPanel named as mainPanel

            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());
            mainPanel.setPreferredSize(new Dimension (500,500));

            //create an instance of BtnListener 
            BtnListener btnListener = new BtnListener();

            //create a JButton for Select File

            selBtn.setActionCommand("Select");


            //create a write button 


            writeBtn.addActionListener(btnListener);
            writeBtn.setActionCommand("Write");


            //create a sort button
            JButton sortBtn = new JButton ("Sort");
            sortBtn.setActionCommand("Sort");
            sortBtn.addActionListener(btnListener);

            //radio buttons
            JRadioButton dogButton = new JRadioButton("Last Name");
            dogButton.setActionCommand("test");

            JRadioButton rabbitButton = new JRadioButton("Yards");
            rabbitButton.setActionCommand("test");

            JRadioButton pigButton = new JRadioButton("Rating");
            pigButton.setActionCommand("test");

            //Group the radio buttons.
            ButtonGroup group = new ButtonGroup();
            group.add(dogButton);
            group.add(rabbitButton);
            group.add(pigButton);

            //Register a listener for the radio buttons

            //create a JPanel named as headPanel

            JPanel headPanel = new JPanel ();
            headPanel.add(selBtn);
            headPanel.add(writeBtn);

            //add headPanel to North of mainPanel
            mainPanel.add(headPanel, BorderLayout.NORTH);

            //create a JPanel named as footPanel
            JPanel footPanel = new JPanel ();
            footPanel.add(dogButton);
            footPanel.add(rabbitButton);
            footPanel.add(pigButton);
            footPanel.add(sortBtn);


            //add footPanel to South of mainPanel
            mainPanel.add(footPanel, BorderLayout.SOUTH);

            //create a JScrollPane with ta1 component from BtnListener instance as argument
            //set above JScrollPane to your preferred dimension size
            JScrollPane log = new JScrollPane (btnListener.ta1);
            log.setPreferredSize (new Dimension (450, 400));

            //create a JScrollPane with ta2 component from BtnListener instance as argument
            //set above JScrollPane to your preferred dimension size
            JScrollPane sp2 = new JScrollPane (btnListener.ta2);
            sp2.setPreferredSize (new Dimension (450, 400));

            //create a JSplitPane object that divides above two JScrollPane objects
            JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                    log, sp2);
            //use setResizeWeight method from JSplitMethod to 0.5
            sp.setResizeWeight(0.5);
            //add above created JSplitPane to CENTER of the mainPanel
            mainPanel.add(sp, BorderLayout.CENTER);

            //add mainPanel to JFrame object ContentPane
            //Pack and set JFrame to be visible
            frame.getContentPane().add(mainPanel);

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

        public void actionPerformed(ActionEvent e) {
             //Handle open button action.
            if (e.getSource() == selBtn) {
                int returnVal = fc.showOpenDialog(Exam.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would open the file.
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());

            //Handle save button action.
            } else if (e.getSource() == writeBtn) {
                int returnVal = fc.showSaveDialog(Exam.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    //This is where a real application would save the file.
                    log.append("Saving: " + file.getName() + "." + newline);
                } else {
                    log.append("Save command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }
    }//end of Exam class

1 个答案:

答案 0 :(得分:1)

该代码在任何时候都不会调用selBtn.addActionListener(..)。显然,他们不会发射一个可以检测到的事件。