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