Swing-文件选择对话框

test1:

 test2:

 

 test3:

 

 设计路线:

例如test1:选择打开文件:

1.、创建一个文件选择器

2、创建一个文件后缀名过滤器

3、设置文件选择器的过滤器

4、设置打开对话框

5、如果操作了打开,就创建一个文件对象,显示到文本框

 

类:MyFrame

package swing02;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MyFrame extends JFrame{
	
	JTextField textfield=new JTextField(20);
	
	public MyFrame(String title)
	{
		super(title);
		
		JPanel root=new JPanel();
		this.setContentPane(root);
		root.setLayout(new FlowLayout());
		
		JButton button=new JButton("选择");
		root.add(new JLabel("文件"));
		root.add(textfield);
		root.add(button);
		
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				test1();
			}
			
		});
		
	}
	
	//选择打开文件
	private void test1()
	{
		//创建一个文件选择器
		JFileChooser chooser=new JFileChooser();
		
		//FileNameExtensionFilter:文件名后缀过滤器
		FileNameExtensionFilter filter=new FileNameExtensionFilter("图片文件", "jpg","jpeg","png");
		chooser.setFileFilter(filter);
		
		//显示对话框
		int ret=chooser.showOpenDialog(this);
		//获取用户选择的结果
		if(ret==JFileChooser.APPROVE_OPTION)
		{
			//结果为:已经存在的一个文件
			File file=chooser.getSelectedFile();
			textfield.setText(file.getAbsolutePath());
		}
	}
	
	//选择保存文件
	private void test2()
	{
		JFileChooser chooser=new JFileChooser();
		
		//FileNameExtensionFilter:文件名后缀过滤器
		FileNameExtensionFilter filter=new FileNameExtensionFilter("XML文件", "xml");
		chooser.setFileFilter(filter);
		
		//显示对话框
		int ret=chooser.showSaveDialog(this);
		//获取用户选择的结果
		if(ret==JFileChooser.APPROVE_OPTION)
		{
			//结果为:用户要保存的文件路径
			File file=chooser.getSelectedFile();
			textfield.setText(file.getAbsolutePath());
		}
		
	}
	
	//选择目录
	private void test3()
	{
		JFileChooser chooser=new JFileChooser();
		
		//设置模式,仅选择目录
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		
		//显示对话框
		int ret=chooser.showOpenDialog(this);
		//获取用户选择的结果
		if(ret==JFileChooser.APPROVE_OPTION)
		{
			//结果为:已经存在的一个目录
			File dir=chooser.getSelectedFile();
			textfield.setText(dir.getAbsolutePath());
		}
	}

}

注意:如下代码为获取当前系统界面格式并同步

try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

类:MyDemo

package swing02;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MyDemo
{
	private static void createGUI()
	{
		// JFrame指一个窗口,构造方法的参数为窗口标题
		JFrame frame = new MyFrame("打开");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
				// 设置窗口的其他参数,如窗口大小
		frame.setSize(400, 300);
		
		// 显示窗口
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		//设置界面样式 Look And Feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});

	}
}

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值