日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java buffer 记事本_Java实现记事本|IO流/GUI

發布時間:2024/4/14 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java buffer 记事本_Java实现记事本|IO流/GUI 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java實現記事本

題目

利用GUI實現一個簡單的記事本(notepad),即打開文件,文字內容顯示在界面上;

允許對文字內容進行編輯,并可以保存到文件。

代碼

class notPadcontainer{

public Boolean visible = false;

//組件定義成屬性

public JFrame notPadFrame;

public JMenuBar notPadMenuBar;

public JMenu firMenu;

public JMenu secMenu;

public JMenu thirMenu;

public JMenu fourMenu;

public JMenuItem buildItem;

public JMenuItem openItem;

public JMenuItem reserveItem;

public JMenuItem paperSetItem;

public JMenuItem clearItem;

public JMenuItem aboutItem;

public JMenuItem fontItem20;

public JMenuItem fontItem40;

public JTextArea textArea;

public JScrollPane textScrollPane;

/*

* 無參構造函數

* 創建組件 初始化組件

*/

notPadcontainer(){

//窗體Frame

this.notPadFrame = new JFrame("notePad by fishers"); //設置窗體 名字為notePad

this.notPadFrame.setLayout(new BorderLayout()); //邊界布局方式

this.notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置關閉框

this.notPadFrame.setSize(500,500); //設置窗口大小

//菜單組件

this.notPadMenuBar = new JMenuBar();

this.firMenu = new JMenu("文件");

this.thirMenu = new JMenu("字體");

this.secMenu = new JMenu("編輯");

this.fourMenu = new JMenu("幫助");

//create JMenuItem for the First menu

this.buildItem = new JMenuItem("新建");

this.openItem = new JMenuItem("打開");

this.reserveItem = new JMenuItem("保存");

this.paperSetItem = new JMenuItem("頁面設置");

//create JMenuItem for the sec thir four menu

this.clearItem = new JMenuItem("清空");

this.aboutItem = new JMenuItem("關于");

this.fontItem20 = new JMenuItem("字體20號");

this.fontItem40 = new JMenuItem("字體40號");

//文本組件

this.textArea = new JTextArea();

this.textScrollPane = new JScrollPane(textArea);

textArea.setFont(new Font("宋體",Font.PLAIN,20)); //默認20號字體

ItemAdd();

runListener();

}

//添加組件

public void ItemAdd(){

//添加JMenu到JMenuBar

notPadMenuBar.add(firMenu);

notPadMenuBar.add(secMenu);

notPadMenuBar.add(thirMenu);

notPadMenuBar.add(fourMenu);

//添加JMenuItem到第一個菜單

firMenu.add(buildItem);

firMenu.add(openItem);

firMenu.add(reserveItem);

firMenu.add(paperSetItem);

secMenu.add(clearItem);

thirMenu.add(fontItem20);

thirMenu.add(fontItem40);

fourMenu.add(aboutItem);

//notPadFrame中添加各個組件

this.notPadFrame.setJMenuBar(notPadMenuBar);

this.notPadFrame.add(textScrollPane,BorderLayout.CENTER);

}

/*

* 事件監聽代碼部分

*/

public void runListener() {

//新建文件 = 清空。。

buildItem.addActionListener( e -> {

textArea.setText("");

});

//打開文件

openItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

//設置彈出框 FileDialog

FileDialog saveFiledialog = new FileDialog(notPadFrame,"打開文件",FileDialog.LOAD);

saveFiledialog.setVisible(true);

String fileDir = saveFiledialog.getDirectory(); //拿到目錄

String fileName = saveFiledialog.getFile(); //拿到文件名

// System.out.println(fileDir);

// System.out.println(fileName);

File openFile = new File(fileDir,fileName); //使用File類創建新文件對象

try {

FileReader freader = new FileReader(openFile); //字符流

StringBuffer tempBuffer = new StringBuffer();//StringBuffer可變

int len = 0; //下面使用read方法讀取

while((len = freader.read()) != -1) {

tempBuffer.append((char)len); //append方法加入StringBuffer

}

String openString = new String(tempBuffer.toString());

textArea.setText(openString);

freader.close();//關閉流

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

//保存文件

reserveItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

FileDialog saveFiledialog = new FileDialog(notPadFrame,"保存文件",FileDialog.SAVE);//父級frame 標題 mode

saveFiledialog.setVisible(true);

String fileDir = saveFiledialog.getDirectory();

String fileName = saveFiledialog.getFile();

System.out.println(fileDir);

System.out.println(fileName);

String ContentString = textArea.getText();

File saveFile = new File(fileDir,fileName);

try {

FileWriter fWriter = new FileWriter(saveFile); //使用字符流寫文件

fWriter.write(ContentString); //寫文件

fWriter.close(); //關閉流

}catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

//清空文件

clearItem.addActionListener( e -> {

textArea.setText("");

});

//監聽關于

aboutItem.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

JFrame aboutFrame = new JFrame("關于"); //新建窗口

aboutFrame.setLayout(new BorderLayout());

aboutFrame.setSize(300,115);

aboutFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//設置隱藏窗口

JPanel panel = new JPanel();

JLabel label = new JLabel("不會換行、中文亂碼的記事本");

JPanel panel2 = new JPanel();

JLabel label2 = new JLabel("Copyright ? 2019 fishers");

panel.add(label);

panel2.add(label2);

aboutFrame.add(panel,BorderLayout.CENTER);

aboutFrame.add(panel2,BorderLayout.PAGE_START);

aboutFrame.setVisible(true);

}

});

fontItem20.addActionListener(e->{

textArea.setFont(new Font("宋體",Font.PLAIN,20));

});

fontItem40.addActionListener(e->{

textArea.setFont(new Font("宋體",Font.PLAIN,40));

});

}

//setVisible:設置窗口顯示

public void setVisible(Boolean visible) {

this.visible = visible;

this.notPadFrame.setVisible(this.visible);

}

}

public class notePad {

public static void main(String[] args) {

notPadcontainer oneNote = new notPadcontainer();

oneNote.setVisible(true);

}

}

總結

以上是生活随笔為你收集整理的java buffer 记事本_Java实现记事本|IO流/GUI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。