本实例创建Swing窗体,单击窗体中的“写入文件”按钮实现写入功能,单击“读取文件”按钮实现从文件中读取信息显示在文本框中

package com.lzw;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;


//本实例创建Swing窗体,单击窗体中的“写入文件”按钮实现将文本框中是数据写入到磁盘文件中,
//单击“读取文件”按钮,系统将磁盘文件中的信息显示在文本框中。
import java.awt.*; //*表示所有包
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


public class Ftest extends JFrame { // 创建类,继承JFrame类
	private JScrollPane scrollPane;
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null; // 创建面板对象
	private JTextArea jTextArea = null; // 创建文本域对象
	private JPanel controlPanel = null; // 创建面板对象
	private JButton openButton = null; // 创建按钮对象
	private JButton closeButton = null; // 创建按钮对象


	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}


	private JPanel getControlPanel() {
		if (controlPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(1);
			controlPanel = new JPanel();
			controlPanel.setLayout(flowLayout);
			controlPanel.add(getOpenButton(), null);
			controlPanel.add(getCloseButton(), null);
		}
		return controlPanel;
	}


	private JButton getOpenButton() {
		if (openButton == null) {
			openButton = new JButton();
			openButton.setText("写入文件"); // 修改按钮的提示信息
			openButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) { // 按钮的单击事件
					File file = new File("word.txt"); // 创建文件对象
					try {
						FileWriter out = new FileWriter(file); // 创建FileWriter对象
						String s = jTextArea.getText(); // 获取文本域中的文本
						out.write(s); // 将信息写入磁盘文件
						out.close(); // 将流关闭
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return openButton;
	}


	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("读取文件"); // 修改按钮的提示信息
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) { // 按钮的单击事件
					File file = new File("word.txt"); // 创建文件对象
					try {
						FileReader in = new FileReader(file); // 创建FileReader对象
						char byt[] = new char[1024]; // 创建char型数组
						int len = in.read(byt); // 将字节读入数组
						jTextArea.setText(new String(byt, 0, len)); // 设置文本域的显示信息
						in.close(); // 关闭流
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return closeButton;
	}


	public Ftest() {
		super();
		initialize();
	}


	public void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}


	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getControlPanel(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}


	public static void main(String[] args) { // main主方法
		Ftest thisClass = new Ftest(); // 创建本类对象
		thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thisClass.setVisible(true); // 设置该窗体未显示状态
	}


	protected JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getJTextArea());
		}
		return scrollPane;
	}
}
效果如下:


  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值