1,GUI+异常处理+文件=》简易记事本(面向对象实现)
面向对象其实就可以把一个函数用类和对象实现,把传给函数的值传给类的构造,并执行函数
操作,实现功能。岂不美哉!
2,做项目首先想好思路,先实现基本功能再扩充再细化再优化,一步一步来不可多想逾越
1,GUI实现形态
2,文件实现功能(存储,写入,读出)
3,GUI实现创建文件
4,优化附加功能
5,用面向对象方式重写
6,变成.exe文件
面向对象其实就可以把一个函数用类和对象实现,把传给函数的值传给类的构造,并执行函数操作实现功能。岂不美哉!
****************************************************************************************************************************************************
GUI
package Try;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Gui extends JFrame implements ActionListener {
public static String s,s1;
JPanel jp=new JPanel();
JButton jb1=new JButton("文件");
JTextPane jtf=new JTextPane();
JMenuBar jm=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu way=new JMenu("格式");
JMenu help=new JMenu("帮助");
JMenuItem open=new JMenuItem("打开");
JMenuItem new1=new JMenuItem("新建");
JMenuItem save=new JMenuItem("保存");
JMenuItem color=new JMenuItem("颜色");
JMenuItem size=new JMenuItem("型号");
JMenuItem word=new JMenuItem("字形");
JMenuItem about=new JMenuItem("关于记事本");
JScrollPane scrollPane;
public Gui()
{this.setTitle("讷铮记事本");
this.setBounds(100,100,600,400);
this.setVisible(true);
this.add(jp);
jp.setLayout(null);
jp.add(jtf);
jtf.setBounds(5,22,575,370);
jtf.setSelectedTextColor(Color.RED);
/*
jtf.setLineWrap(true); //激活自动换行功能
jtf.setWrapStyleWord(true);
*/
jtf.setSelectionColor(Color.red);
jp.add(jm);
jm.setBounds(5,0,120,20);
jm.add(file);
jm.add(way);
jm.add(help);
file.add(open);
file.add(new1);
file.add(save);
way.add(color);
way.add(size);
way.add(word);
help.add(about);
open.addActionListener(this);
save.addActionListener(this);
new1.addActionListener(this);
color.addActionListener(this);
size.addActionListener(this);
word.addActionListener(this);
about.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// 判断最初发生Event事件的对象
if (e.getSource() == save) {
// 获得容器
JFrame jf=new JFrame();
int i=0;
i=JOptionPane.showConfirmDialog(jf, "确定保存?","这是一个确认对话框",JOptionPane.OK_CANCEL_OPTION);
if(i==0)
{
System.out.println("已成功保存");
JFrame jf1=new JFrame();
s=JOptionPane.showInputDialog(jf1, "重命名","这是一个输入对话框",JOptionPane.INFORMATION_MESSAGE);
System.out.println(s);