试图写一个简单的程序,我想要一个按钮,当点击打开文件浏览器窗口,以便用户可以选择一个文件上传到程序...如何链接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;
公共类片延伸的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);
}
}
尼斯一个盖子
您已经**选择了**文件。当你说“打开文件”时,你是什么意思? – 2014-09-01 13:13:39