国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

filewriter與filereader類實例15.04

2019-11-14 10:04:18
字體:
來源:轉載
供稿:網友
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 java.awt.event.ActionListener() {						// 按鈕的單擊事件						public void actionPerformed(ActionEvent e) {							// 創建文件對象							File file = new File("Word.txt");							try {								// 創建FileWriter對象								FileWriter out = new FileWriter(file);								// 獲取文本域中文本								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 java.awt.event.ActionListener() {						// 按鈕的單擊事件						public void actionPerformed(ActionEvent e) {							File file = new File("word.txt"); // 創建文件對象							try {								// 創建FileReader對象								FileReader in = new FileReader(file);								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();	}		private 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) { // 主方法		Ftest thisClass = new Ftest(); // 創建本類對象		thisClass.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);		thisClass.setVisible(true); // 設置該窗體為顯示狀態	}	/**	 * @return	 */	protected JScrollPane getScrollPane() {		if (scrollPane == null) {			scrollPane = new JScrollPane();			scrollPane.setViewportView(getJTextArea());		}		return scrollPane;	}}
上一篇:JS多重繼承

下一篇:文件夾和文件壓縮

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安县| 焉耆| 德钦县| 石河子市| 洪湖市| 赤水市| 綦江县| 二手房| 博白县| 大洼县| 恩施市| 南昌市| 镇远县| 阿巴嘎旗| 淳安县| 泰州市| 辉南县| 襄城县| 台北市| 增城市| 邳州市| 旅游| 临颍县| 丹巴县| 巴彦淖尔市| 阳高县| 浦江县| 云龙县| 射阳县| 乐陵市| 荆州市| 绍兴市| 上犹县| 新宁县| 西盟| 岗巴县| 德州市| 汉川市| 安新县| 荥经县| 松桃|