Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将Jbutton与打开文件浏览器以选择文件的代码链接?_Java_Swing_Button_Jbutton - Fatal编程技术网

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

Java 如何将Jbutton与打开文件浏览器以选择文件的代码链接?,java,swing,button,jbutton,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.JFileC

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

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

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

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里吗?我需要做它的考试,我不知道怎么做

导入java.awt.BorderLayout

公共类选项卡扩展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);
}
}


很好的一个盖子

你的问题有点宽泛和模糊,因为你只展示了借用的代码,而没有展示你自己在哪里尝试过把东西综合在一起,所以我只能给出推荐内容的大致轮廓。如果您需要更具体的帮助,请向我们展示您的代码,然后告诉我们您的代码可能存在哪些问题

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

链接:

编辑 为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
}

但同样,没有什么可以替代您阅读教程和试验代码,编写大量代码,看看哪些有效,哪些无效,您喜欢什么。

您已经选择了该文件。你说“打开文件”是什么意思?这是我按钮的代码,我已经有了。已联机找到文件浏览器的代码。单击按钮时,我希望打开文件浏览器。我只是不知道如何将文件浏览器代码链接到按钮。JButton btnNewButton_1=新JButtonLOAD输入文件;panel.AddBtnewButton_1;btnNewButton_1.设置首选尺寸新尺寸600100;btnNewButton_1.setFontnew字体Arial,Font.BOLD,40@itchyhippo:请注意,当在注释中发布代码时,会放松格式,因此,大多数代码都应该在您的问题中发布。您可以编辑您的问题并将此代码放在其中。但更重要的是,我的主要建议是,您先看看教程链接,然后尝试合成自己的代码来解决这个问题。如果这不起作用,那么请将此代码发布为对您的问题的编辑,让我们来处理它。学习编程意味着学习使用一般信息并将其应用于您的具体情况,所以请继续。@itchyhippo:最重要的是,我在您的注释中没有看到您在代码中的任何地方尝试向按钮添加ActionListener。请先试试这个。你认为自己做不到这一点是在侮辱自己,我强烈认为你有这个能力,所以请证明我是对的。@itchyhippo:例如,请参见编辑以回答。继续编码,写很多很多的代码。这就是你学习的方式。