jfilechooserdemo.java

来自「java程序设计与编程的利用与实现 java程序设计与编程的利用与实现」· Java 代码 · 共 33 行

JAVA
33
字号
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 + =
减小字号Ctrl + -
显示快捷键?