Java实验(十)

版权声明:本文为Trinity原创文章,未经Trinity允许不得转载 https://blog.csdn.net/Caoyang_He/article/details/84503444

图形化界面

工具:Eclipse
参考资料
外观设计
在这里插入图片描述
代码设计

package test9;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Label;
import java.awt.TextField;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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;

public class fileFrame extends JFrame {

	private JPanel contentPane;
	private JFileChooser jfc;

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

	/**
	 * Create the frame.
	 */
	public fileFrame() {
		jfc = new JFileChooser();
		FileFilter filter = new FileNameExtensionFilter("文本文件","txt");
		jfc.setFileFilter(filter);
		//创建文件选择器
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		Label label = new Label("\u6587\u4EF6\u540D");
		label.setBounds(10, 10, 44, 23);
		contentPane.add(label);
		
		final TextField textField = new TextField();
		textField.setBounds(60, 10, 281, 23);
		contentPane.add(textField);
		
		Button button = new Button("\u6253\u5F00");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String FileName = "";
				try {
					if(jfc.showOpenDialog(contentPane)==JFileChooser.APPROVE_OPTION)
					{
						FileName = jfc.getSelectedFile().getAbsolutePath();
					}	
					else
					{
						return ;
					}
					
					File fi = new File(FileName);
					if(!fi.exists())
					{
						throw new Exception("文件不存在");
					}//判断文件是否存在
					if(!fi.isFile())
					{
						throw new Exception("选择的不是文件");
					}//判断是否是文件
					BufferedReader br = new BufferedReader(new FileReader(FileName));
					//创建一个读文件对象
					String line = br.readLine();
					String strContent = "";
					boolean isFirst = true;
					while(line != null)
					{
						if(!isFirst)
						{
							strContent += "\n";
						}
						isFirst = false;
						strContent += line;
						line = br.readLine();
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(contentPane,e1.getMessage());
				}
			}
		});
		button.setBounds(348, 10, 76, 23);
		contentPane.add(button);
		
		final TextArea textArea = new TextArea();
		textArea.setBounds(10, 39, 414, 178);
		contentPane.add(textArea);
		
		Button button_1 = new Button("\u786E\u8BA4");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String FileName = textField.getText();
				try {
					if(FileName.equals("")){
						if(jfc.showSaveDialog(contentPane)==JFileChooser.APPROVE_OPTION){
							FileName=jfc.getSelectedFile().getAbsolutePath();
						}
						else{
							return;
						}
					}
					String strContent=textArea.getText();
					//获取到写入文件的内容
					if(strContent.equals("")){
						throw new Exception("请输入内容");
					}
					strContent=strContent.replace("\n", "\r\n");
					//替换换行符
					BufferedWriter  br = new BufferedWriter(new FileWriter(FileName));
					//创建一个写文件的对象
					br.write(strContent);
					//写入数据
					br.close();
					//关闭对象
					textField.setText(FileName);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(contentPane,e1.getMessage());
				} 
			}
		});
		button_1.setBounds(348, 223, 76, 23);
		contentPane.add(button_1);
	}
}

猜你喜欢

转载自blog.csdn.net/Caoyang_He/article/details/84503444