📄 test.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Test extends JFrame {
JFileChooser chooser = new JFileChooser();
JDialog dialog;
JButton button = new JButton("show file chooser ...");
public Test() {
super("Simple File Chooser Application");
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title = chooser.getDialogTitle();
if(title == null)
chooser.getUI().getDialogTitle(chooser);
dialog = new JDialog((Frame)null, title, false);
Container dialogContentPane =
dialog.getContentPane();
dialogContentPane.setLayout(new BorderLayout());
dialogContentPane.add(chooser,
BorderLayout.CENTER);
dialog.setTitle("Non-Modal File Chooser");
dialog.pack();
dialog.setLocationRelativeTo(Test.this);
dialog.setVisible(true);
}
});
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String state = (String)e.getActionCommand();
if(state.equals(JFileChooser.APPROVE_SELECTION)) {
File file = chooser.getSelectedFile();
JOptionPane.showMessageDialog(
null, file.getPath());
}
else if(
state.equals(JFileChooser.CANCEL_SELECTION)) {
JOptionPane.showMessageDialog(
null, "Canceled");
}
// JFileChooser action listeners are notified
// when one either the approve button or
// cancel button is activated
dialog.setVisible(false);
}
});
}
public static void main(String args[]) {
JFrame f = new Test();
f.setBounds(300,300,350,100);
f.setVisible(true);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -