欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

高级UI组件之文件对话框

程序员文章站 2022-03-30 11:40:51
...

  在对话框的基础模式上添加JFileChooser。读取文件,显示在文字编辑框中。并可以保存编辑框中的内容,选择指定文件位置。

package Night;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.GridLayout;

public class FileChooserDemo {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileChooserDemo window = new FileChooserDemo();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileChooserDemo() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 553, 371);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label = new JLabel("\u8BF7\u9009\u62E9\u6587\u4EF6\uFF1A");
		frame.getContentPane().add(label, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 1, 0, 0));
		
		JTextArea textArea = new JTextArea();
		panel.add(textArea);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("打开");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfc = new JFileChooser();
				int value = jfc.showOpenDialog(frame);
				if(value==JFileChooser.APPROVE_OPTION)//选择打开
				{
					File fileName = jfc.getSelectedFile();
					try {
						FileReader fread = new FileReader(fileName);
						char[] ch = new char[1024];
						int hasData=0;
						try {
							if((hasData=fread.read(ch))>=0) {
								textArea.append(new String(ch));
							}
							fread.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
			}
		});
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("保存");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfc = new JFileChooser();
				int value = jfc.showOpenDialog(frame);
				if(value==JFileChooser.APPROVE_OPTION) {
				File filename = jfc.getSelectedFile();
				try {
					FileWriter fw = new FileWriter(filename);
					char[] ch = new char[1024];
					int hasData=0;
					fw.write(textArea.getText());
					fw.close();
					
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}}
		});
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("清除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textArea.setText("");
			}
		});
		panel_1.add(btnNewButton_2);
	}

}
高级UI组件之文件对话框
相关标签: Swing