📄 jfilechooserdemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class myFrame extends JFrame implements ActionListener{
JButton button1,button2;
public myFrame(){
super("显示文件对话框");
Container c=getContentPane();
button1=new JButton("打开文件");
button1.addActionListener(this);
button2=new JButton("保存文件");
button2.addActionListener(this);
c.setLayout(new FlowLayout());
c.add(button1);
c.add(button2);
}
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser=new JFileChooser();//创建文件对话框对象
//弹出文件对话框
if(e.getSource()==button1)
fileChooser.showOpenDialog(this);//显示文件打开对话框
if(e.getSource()==button2)
fileChooser.showSaveDialog(this);//显示文件保存对话框
}
}
public class JFileChooserDemo{
public static void main(String args[]){
JFrame frame=new myFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(180,100);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -