记事本改字体的代码java_利用Java做的一个仿Windows记事本程序代码

这是一个基于Java编写的简易记事本程序,实现了文件的新建、打开、保存和另存功能,同时允许用户改变字体颜色。程序还包含一个实时更新的当前时间显示。虽然部分功能如QQ小程序尚未实现,但源代码可供进一步完善。
摘要由CSDN通过智能技术生成

今天忙着完成老师布置的作业,刚学习了I/O,老师让做一个输入输出,这不就是记事本嘛,就自己做了一个。实现的功能: 1.另存,保存在任意目录。 2.文件的新建和打开。 3.自由变换字体的颜色。 4.用多线程做了一个动态当前时间的显示。 5.下栏显示文件是否保存和保存路径还有一些功能没有实现,老师急着让做一个QQ小程序,所以,可以给大家源码,大家另加完善的哦!

05a64f9136eb0375993072adeea8c0e8.gif

[CODE=java]

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.*;

import javax.swing.event.*;

public class TestGui extends JFrame implements Runnable{

private JMenu TimeShow = new JMenu(); //时间工具

private boolean ifBaoCun = false;

JFileChooser Jc=new JFileChooser();

public TestGui(){

/*主框架*/

this.setTitle("我的最简易记事本");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(800,600);

this.setResizable(false);

/*界面居中*/

Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize=this.getSize();

int w=(screenSize.width-frameSize.width)/2;

int h=(screenSize.height-frameSize.height)/2;

setLocation(w, h);

/*组件定义*/

/*添加工具条*/

JPanel jp1 = new JPanel();

JMenuBar menu=new JMenuBar();

JMenu CaoZuo=new JMenu("操作");

JMenuItem BaoCun=new JMenuItem("点我保存");

JMenuItem LingCun=new JMenuItem("点我另存");

CaoZuo.add(BaoCun);

CaoZuo.add(LingCun);

JMenu file=new JMenu("文件");

JMenuItem New=new JMenuItem("新建");

JMenuItem Open=new JMenuItem("打开");

file.add(New);

file.add(Open);

JMenu geshi=new JMenu("格式");

JMenuItem fontColor=new JMenuItem("字体颜色");

geshi.add(fontColor);

menu.add(CaoZuo);

menu.add(file);

menu.add(geshi);

menu.add(TimeShow);

this.setJMenuBar(menu);

/*下部状态栏*/

final JLabel tx2 =new JLabel("文档状态:暂无内容");

/*文本区域*/

final JTextArea tx =new JTextArea(29,10);

JScrollPane js=new JScrollPane(tx);

jp1.add(js);

jp1.setLayout(new GridLayout(1,1));

this.add("North",jp1);

this.add("South",tx2);

/*组件监听事件*/

/*新建--事件*/

New.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

ifBaoCun=false;

tx.setText("");

}

});

/*字体颜色--事件*/

fontColor.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

Color c;

c = JColorChooser.showDialog(((Component)e.getSource( )).getParent( ),

"Demo", Color.blue);

tx.setForeground(c);

}

});

/*保存--事件*/

BaoCun.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(ifBaoCun==false){

Jc.showSaveDialog(null);

Jc.getSelectedFile();

try {

FileOutputStream out = new FileOutputStream(Jc.getSelectedFile()+".txt");

PrintWriter pw = new PrintWriter(out);

pw.print(tx.getText());

pw.flush();

pw.close();

tx2.setText("文档状态:已保存"+" "+"保存路径为:"+Jc.getSelectedFile()+".txt");

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

ifBaoCun=true;

}

}

});

/*另存--事件*/

LingCun.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

JFileChooser Jc=new JFileChooser();

Jc.showSaveDialog(null);

Jc.getSelectedFile();

try {

FileOutputStream out = new FileOutputStream(Jc.getSelectedFile()+".txt");

PrintWriter pw = new PrintWriter(out);

pw.print(tx.getText());

pw.flush();

pw.close();

tx2.setText("文档状态:已保存");

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

}

});

/*是否保存状态--事件*/

tx.addCaretListener(new CaretListener() {

public void caretUpdate(CaretEvent e) {

tx2.setText("文档状态:未保存");

}

});

/*打开--事件*/

Open.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

JFileChooser Jc=new JFileChooser();

Jc.showOpenDialog(null);

Jc.getSelectedFile();

System.out.print(Jc.getSelectedFile());

try {

FileInputStream in = new FileInputStream(Jc.getSelectedFile());

int into = in.read();

String xx = into +" ";

tx.setText(xx);

while(into!=1){

}

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (IOException e2) {

e2.printStackTrace();

}

}

});

/*设置窗体可见*/

this.setVisible(true);

/*开启时间多线程*/

setupTime();

}

public void setupTime()

{

Thread thread1=new Thread(this);

thread1.start();

}

public void run()

{

while(true)

{

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

TimeShow.setText("当前时间:"+sdf.format(new Date()));

}

}

public static void main(String ares[]){

new TestGui();

}

}

[/CODE]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值