如何将Jbutton与代码链接以打开文件浏览器来选择文件?

时间:2014-09-01 13:10:37

标签: java swing button jbutton

尝试编写一个简单的程序,我想要一个单击按钮打开文件浏览器窗口,以便用户可以选择要上传到程序的文件......

我目前有以下代码打开浏览器窗口......

如何将此代码链接到我现有的按钮代码,以便在单击按钮时打开文件浏览器?

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

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MainClass {

  public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
        String command = actionEvent.getActionCommand();
        if (command.equals(JFileChooser.APPROVE_SELECTION)) {
          File selectedFile = theFileChooser.getSelectedFile();
          System.out.println(selectedFile.getParent());
          System.out.println(selectedFile.getName());
        } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
          System.out.println(JFileChooser.CANCEL_SELECTION);
        }
      }
    };
    fileChooser.addActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
  }
}

谢谢

有人可以把它放到MVC中吗?我需要为考试做这件事,我不知道怎么做......

import java.awt.BorderLayout;

public class Tab扩展了JFrame {

private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;

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

{
    this.setTitle("Cipher Toolkit");
}

// set the title
/**
 * Create the frame.
 */
public Tab() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(500, 400, 700, 600);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmPrint = new JMenuItem("Print");
    mnFile.add(mntmPrint);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);

    JMenu mnAbout = new JMenu("About");
    menuBar.add(mnAbout);

    JMenuItem mntmAboutTheAuthors = new JMenuItem("About the Authors");

    mnAbout.add(mntmAboutTheAuthors);

    JMenuItem mntmProgramVersion = new JMenuItem("Program Version");
    mnAbout.add(mntmProgramVersion);

    JMenu mnHelp = new JMenu("Help");
    menuBar.add(mnHelp);

    JMenuItem mntmFaqs = new JMenuItem("FAQ's");
    mnHelp.add(mntmFaqs);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(20, 10, 655, 520);
    contentPane.add(tabbedPane);

    JPanel panel = new JPanel();
    panel.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Ceasar Encrypt", null, panel, null);
    panel.setBorder(new TitledBorder(new LineBorder(Color.blue, 2, true)));

    JButton btnNewButton_1 = new JButton("LOAD INPUT FILE");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);
        }
    });

    panel.add(btnNewButton_1);
    btnNewButton_1.setPreferredSize(new Dimension(600, 100));
    btnNewButton_1.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_2 = new JButton("SELECT OUTPUT FILE");
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);
        }
    });

    panel.add(btnNewButton_2);
    btnNewButton_2.setPreferredSize(new Dimension(600, 100));
    btnNewButton_2.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton = new JButton("ENCRYPT!");
    panel.add(btnNewButton);
    btnNewButton.setPreferredSize(new Dimension(600, 100));
    btnNewButton.setFont(new Font("Arial", Font.BOLD, 40));

    JLabel lblSelectShiftAmount = new JLabel("SELECT SHIFT AMOUNT");
    panel.add(lblSelectShiftAmount);

    textField = new JTextField();
    panel.add(textField);
    textField.setColumns(10);

    JPanel panel_1 = new JPanel();
    panel_1.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Ceasar Decrypt", null, panel_1, null);
    panel_1.setBorder(new TitledBorder(new LineBorder(Color.green, 2, true)));

    JButton btnNewButton_4 = new JButton("LOAD INPUT FILE");
    btnNewButton_4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);

        }
    });
    panel_1.add(btnNewButton_4);
    btnNewButton_4.setPreferredSize(new Dimension(600, 100));
    btnNewButton_4.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_5 = new JButton("SELECT OUTPUT FILE");
    btnNewButton_5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);

        }
    });
    panel_1.add(btnNewButton_5);
    btnNewButton_5.setPreferredSize(new Dimension(600, 100));
    btnNewButton_5.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_3 = new JButton("DECRYPT!");
    panel_1.add(btnNewButton_3);
    btnNewButton_3.setPreferredSize(new Dimension(600, 100));
    btnNewButton_3.setFont(new Font("Arial", Font.BOLD, 40));

    JLabel lblSelectShiftAmount_1 = new JLabel("SELECT SHIFT AMOUNT");
    panel_1.add(lblSelectShiftAmount_1);

    textField_1 = new JTextField();
    panel_1.add(textField_1);
    textField_1.setColumns(10);

    JPanel panel_2 = new JPanel();
    panel_2.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Substitution Encrypt", null, panel_2, null);
    panel_2.setBorder(new TitledBorder(new LineBorder(Color.red, 2, true)));

    JButton btnNewButton_6 = new JButton("LOAD INPUT FILE");
    btnNewButton_6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);
        }
    });
    panel_2.add(btnNewButton_6);
    btnNewButton_6.setPreferredSize(new Dimension(600, 100));
    btnNewButton_6.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_8 = new JButton("SELECT OUTPUT FILE");
    btnNewButton_8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);

        }
    });
    panel_2.add(btnNewButton_8);
    btnNewButton_8.setPreferredSize(new Dimension(600, 100));
    btnNewButton_8.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_7 = new JButton("ENCRYPT!");
    panel_2.add(btnNewButton_7);
    btnNewButton_7.setPreferredSize(new Dimension(600, 100));
    btnNewButton_7.setFont(new Font("Arial", Font.BOLD, 40));

    JLabel lblSelectShiftAmount_2 = new JLabel("SELECT SHIFT AMOUNT");
    panel_2.add(lblSelectShiftAmount_2);

    textField_2 = new JTextField();
    panel_2.add(textField_2);
    textField_2.setColumns(10);

    JPanel panel_3 = new JPanel();
    panel_3.setBackground(Color.LIGHT_GRAY);
    tabbedPane.addTab("Substitution Decrypt", null, panel_3, null);
    panel_3.setBorder(new TitledBorder(
            new LineBorder(Color.yellow, 2, true)));

    JButton btnNewButton_10 = new JButton("LOAD INPUT FILE");
    btnNewButton_10.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);
        }
    });
    panel_3.add(btnNewButton_10);
    btnNewButton_10.setPreferredSize(new Dimension(600, 100));
    btnNewButton_10.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_11 = new JButton("SELECT OUTPUT FILE");
    btnNewButton_11.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JFrame frame = new JFrame("File Browser");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setControlButtonsAreShown(false);
            frame.getContentPane().add(fileChooser, BorderLayout.CENTER);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser theFileChooser = (JFileChooser) actionEvent
                            .getSource();
                    String command = actionEvent.getActionCommand();
                    if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                        File selectedFile = theFileChooser
                                .getSelectedFile();
                        System.out.println(selectedFile.getParent());
                        System.out.println(selectedFile.getName());
                    } else if (command
                            .equals(JFileChooser.CANCEL_SELECTION)) {
                        System.out.println(JFileChooser.CANCEL_SELECTION);
                    }
                }
            };
            fileChooser.addActionListener(actionListener);
            frame.pack();
            frame.setVisible(true);

        }
    });
    panel_3.add(btnNewButton_11);
    btnNewButton_11.setPreferredSize(new Dimension(600, 100));
    btnNewButton_11.setFont(new Font("Arial", Font.BOLD, 40));

    JButton btnNewButton_9 = new JButton("DECRYPT!");
    panel_3.add(btnNewButton_9);
    btnNewButton_9.setPreferredSize(new Dimension(600, 100));
    btnNewButton_9.setFont(new Font("Arial", Font.BOLD, 40));

    JLabel lblSelectShiftAmount_3 = new JLabel("SELECT SHIFT AMOUNT");
    panel_3.add(lblSelectShiftAmount_3);

    textField_3 = new JTextField();
    panel_3.add(textField_3);
    textField_3.setColumns(10);
}

}

好一个盖子

1 个答案:

答案 0 :(得分:3)

你的问题有点宽泛和模糊,因为你只展示借来的代码而且没有显示你自己在哪里尝试合成的东西,所以我只能张贴大致推荐的内容。如果您需要更具体的帮助,请向我们出示 您的 代码,然后告诉我们您的代码可能遇到的问题。

  1. 创建您的JButton
  2. 将ActionListener添加到JButton
  3. 在ActionListener的actionPerformed代码中,打开你的JFileChooser。
  4. 如果用户按下JFileChooser上的“打开”按钮(通过检查从文件选择器的showOpenDialg(...)方法返回的int进行测试),从JFileChooser获取所选文件。
  5. 完成。
  6. 教程将为您提供详细信息,然后您将要编写代码。

    链接:


    修改
    给出JButton所需动作的关键是向它添加一个ActionListener。我经常使用一个匿名内部类来调用外部类的另一个方法。例如:

    myButton.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
          myButtonAction();
       }
    });
    
    private void myButtonAction() {
       // here put in code that you want to happen when the button is pressed
    }
    

    但是,再一次,您无法替代您阅读教程并尝试使用代码,编写大量代码,查看哪些有效,哪些无效,您喜欢什么。