java的一个简单记事本程序设计
java的字体Font类、GraphicsEnvironment类编程问题:利用Font类、画图环境类,把系统字体全部抽出来,包括颜色、字体。意思即是:设计一个java程...
java的字体Font类、GraphicsEnvironment类编程问题:
利用Font类、画图环境类,把系统字体全部抽出来,包括颜色、字体。
意思即是:设计一个java程序,通过按钮“字体”、“颜色”设置,可以把里面的文本格式改变,相当一个“记事本”吧!
注意:不用功能太强大,满足满足设置字体、颜色即可!
Ps:不明白的问我啦!
希望高手能完成,多谢先! 展开
利用Font类、画图环境类,把系统字体全部抽出来,包括颜色、字体。
意思即是:设计一个java程序,通过按钮“字体”、“颜色”设置,可以把里面的文本格式改变,相当一个“记事本”吧!
注意:不用功能太强大,满足满足设置字体、颜色即可!
Ps:不明白的问我啦!
希望高手能完成,多谢先! 展开
展开全部
是记事本的样式吗?就要这些要求吗?
下面这些代码的功能挺全的,可以参考一下,不用的可以删掉。
import java.io.*;
import java.util.Calendar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class NoteBook extends JFrame implements ActionListener,ItemListener,WindowListener,MouseListener
{ Container c = this.getContentPane();
JMenuBar jmb = new JMenuBar();
JColorChooser jcl = new JColorChooser();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JMenu jm1 = new JMenu("文件(F)");
JMenu jm2 = new JMenu("编辑(E)");
JMenu jm4 = new JMenu("格式(O)");
JMenu jm5 = new JMenu("查看(V)");
JMenu jm3 = new JMenu("帮助(H)");
JMenuItem jmi1 = new JMenuItem("新建(N)");
JMenuItem jmi2 = new JMenuItem("打开(O)");
JMenuItem jmi3 = new JMenuItem("保存(S)");
JMenuItem jmi4 = new JMenuItem("退出(X)");
JMenu jmd4=new JMenu("颜色");
JMenu jmd0=new JMenu("字体");
JMenuItem aa=new JMenuItem("复制(C)");
JMenuItem bb=new JMenuItem("剪切(X)");
JMenuItem cc=new JMenuItem("粘贴(V)");
JCheckBoxMenuItem jcuti1 = new JCheckBoxMenuItem("粗体");
JCheckBoxMenuItem jxieti1 = new JCheckBoxMenuItem("斜体");
JMenuItem jmp1 = new JMenuItem("日期和时间");
JMenuItem jmp2 = new JMenuItem("背景颜色(H)");
JMenuItem jmi5 = new JMenuItem("删除内容(S)");
JMenuItem jmi7=new JMenuItem("关于自己");
JTextArea jta = new JTextArea(23,15);
JScrollPane jsp = new JScrollPane(jta);
JMenuItem jmdb1=new JMenuItem("黄色");//定义各种颜色
JMenuItem jmdb2=new JMenuItem("红色");
JMenuItem jmdb3=new JMenuItem("绿色");
JMenuItem jmdb4=new JMenuItem("蓝色");
JMenuItem jmdb5=new JMenuItem("黑色");
NoteBook()
{ this.addWindowListener(this);
setBounds(200,200,200,200);
c.add(jmb,BorderLayout.NORTH);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("记事本");
jmb.add(jm1);//文件
jm1.setMnemonic('f');
jm1.addActionListener(this);
jmb.add(jm2);//编辑
jm2.setMnemonic('e');
jmb.add(jm4);//格式
jm4.setMnemonic('o');
jm4.addActionListener(this);
jmb.add(jm5);
jm5.setMnemonic('v'); //查看
jmb.add(jm3);//帮助
jm3.setMnemonic('h');
jmb.setLocation(0,0);
jm1.add(jmi1);
jmi1.addActionListener(this);//新建
jmi1.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK,false));
jm1.addSeparator();
jm1.add(jmi2);
jmi2.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));
jmi2.addActionListener(this);//打开
jm1.addSeparator();
jm1.add(jmi3);
jmi3.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));
jmi3.addActionListener(this);//保存
jm1.addSeparator();
jm1.add(jmi4);
jmi1.setMnemonic('n');
jmi2.setMnemonic('o');
jmi3.setMnemonic('s');
jmi4.setMnemonic('x');
jmi4.addActionListener(this);//退出
jm5.add(jmi5); //删除
jm5.addSeparator();
jmi5.addActionListener(this);
jm4.addSeparator();
jm4.add(jmd4);
jm4.add(jmd0);
jmd4.add(jmdb1);
jmd4.add(jmdb2);
jmd4.add(jmdb3);
jmd4.add(jmdb4);
jmd4.add(jmdb5);
jmd4.addActionListener(this); //添加监视器
jmdb1.addActionListener(this);
jmdb2.addActionListener(this);
jmdb3.addActionListener(this);
jmdb4.addActionListener(this);
jmdb5.addActionListener(this);
jm5.add(jmp1);
jm5.addSeparator();
jm5.add(jmp2);
jmp1.addActionListener(this);//日期时间
jmp2.addActionListener(this);//背景颜色
jm2.add(aa);
jm2.addSeparator();
jm2.add(bb);
jm2.addSeparator();
jm2.add(cc);
jm2.addSeparator();
jmd0.add(jcuti1);
jmd0.addSeparator();
jmd0.add(jxieti1);
aa.setMnemonic('C');
aa.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
bb.setMnemonic('x');
bb.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
cc.setMnemonic('V');
cc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
aa.addActionListener(this);//复制
bb.addActionListener(this);//剪切
cc.addActionListener(this);//粘贴
jmd4.addActionListener(this);
jcuti1.addItemListener(this);//粗体
jxieti1.addItemListener(this);//斜体
jm3.add(jmi7);
jmi7.addActionListener(this);//关于自己
c.add(jToolBar,BorderLayout.CENTER); //工具条
jButton2.setIcon(new ImageIcon("image\\new.JPG"));//新建
jToolBar.add(jButton2);
jButton2.setToolTipText("新建");
jButton2.addActionListener(this);
jButton1.setIcon(new ImageIcon("image\\open.JPG"));//打开
jButton1.setToolTipText("打开");
jToolBar.add(jButton1);
jButton1.addActionListener(this);
jButton3.setIcon(new ImageIcon("image\\save.JPG"));//保存
jToolBar.add(jButton3);
jButton3.setToolTipText("保存");
jButton3.addActionListener(this);
jButton4.setIcon(new ImageIcon("image\\qq.JPG"));
jButton4.addActionListener(this); //退出
jToolBar.add(jButton4);
jButton4.setToolTipText("退出");
c.add(jsp,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
jmdb1.addActionListener(new ActionListener() //黄色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.yellow);
} } );
jmdb2.addActionListener(new ActionListener() //红色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.red);
} } );
jmdb3.addActionListener(new ActionListener() //绿色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.green);
} } );
jmdb4.addActionListener(new ActionListener() //蓝色
{
public void actionPerformed(ActionEvent e)
{
jta.setForeground(Color.blue);
} } );
jmdb5.addActionListener(new ActionListener() //黑色
{
public void actionPerformed(ActionEvent e)
{
jta.setForeground(Color.black);
} } );
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==jmi1)//新建
{jta.setText("");
this.setTitle("无标题 - 记事本");
}
if(e.getSource()==jmi2)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
{ try {
f1 = jfc1.getSelectedFile();
this.setTitle(f1.getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
{jta.setText(str);}
fr.close();
br.close(); }
catch(FileNotFoundException e1)
{e1.printStackTrace();}
catch(IOException e2){e2.printStackTrace();}
}}
if(e.getSource()==jmi3)//保存
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}}}
if(e.getSource()==jmi4)//退出
{
int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
this.dispose();
}}}
if(e.getSource()==jmp1)//日期时间
{
Calendar c1 =Calendar.getInstance();
int y = c1.get(Calendar.YEAR);
int m = c1.get(Calendar.MONTH);
int d = c1.get(Calendar.DATE);
int h = c1.get(Calendar.HOUR);
int m1 = c1.get(Calendar.MINUTE);
int m2 = m+1;
jta.setText(y+"年"+m2+"月"+d+"日"+h+":"+m1);
}
if(e.getSource()==jmp2)//背景色
{ Color ccc = JColorChooser.showDialog(this,"color",Color.BLACK);
jta.setSelectedTextColor(ccc);
jta.setBackground(ccc);
}
if(e.getSource()==jButton1)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
{
try
{
f1 = jfc1.getSelectedFile();
this.setTitle(f1.getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
{jta.setText(str);}
fr.close();
br.close(); }
catch(FileNotFoundException e1)
{e1.printStackTrace();}
catch(IOException e2){e2.printStackTrace();}
} }
if(e.getSource()==jButton2)
{jta.setText("");
this.setTitle("文本编辑器");
}
if(e.getSource()==jmi5){
jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd()); //删除
}
if(e.getSource()==jButton3)
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}}}
if(e.getSource()==aa) //复制
{jta.copy();}
else if(e.getSource()==bb)//剪切
{ jta.cut(); }
else if(e.getSource()==cc)//粘贴
{jta.paste();}
if(e.getSource()==jButton4)
{int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
this.dispose();
}}}
//if(e.getSource()==jmi7)//关于我
//{About1 a=new About1();//实现跳转
// a.setVisible(true);
//}
}
public void itemStateChanged(ItemEvent e) //字体
{Font c=new Font("Serif",Font.PLAIN,14);
if(e.getItemSelectable()==jcuti1)
{if(jcuti1.getState()==true)
{ Font f=new Font("Serif",Font.BOLD,20); //粗体
jta.setFont(f);}
else{jta.setFont(c);}
}
else if(e.getItemSelectable()==jxieti1)
{if(jxieti1.getState()==true)
{ Font b=new Font("Serif",Font.ITALIC,20);//斜体
jta.setFont(b);}
else{jta.setFont(c);
} } }
public static void main(String[] args)//入口
{ new NoteBook(); }
public void windowOpened(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
if(a==2){}
}
}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
下面这些代码的功能挺全的,可以参考一下,不用的可以删掉。
import java.io.*;
import java.util.Calendar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class NoteBook extends JFrame implements ActionListener,ItemListener,WindowListener,MouseListener
{ Container c = this.getContentPane();
JMenuBar jmb = new JMenuBar();
JColorChooser jcl = new JColorChooser();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JMenu jm1 = new JMenu("文件(F)");
JMenu jm2 = new JMenu("编辑(E)");
JMenu jm4 = new JMenu("格式(O)");
JMenu jm5 = new JMenu("查看(V)");
JMenu jm3 = new JMenu("帮助(H)");
JMenuItem jmi1 = new JMenuItem("新建(N)");
JMenuItem jmi2 = new JMenuItem("打开(O)");
JMenuItem jmi3 = new JMenuItem("保存(S)");
JMenuItem jmi4 = new JMenuItem("退出(X)");
JMenu jmd4=new JMenu("颜色");
JMenu jmd0=new JMenu("字体");
JMenuItem aa=new JMenuItem("复制(C)");
JMenuItem bb=new JMenuItem("剪切(X)");
JMenuItem cc=new JMenuItem("粘贴(V)");
JCheckBoxMenuItem jcuti1 = new JCheckBoxMenuItem("粗体");
JCheckBoxMenuItem jxieti1 = new JCheckBoxMenuItem("斜体");
JMenuItem jmp1 = new JMenuItem("日期和时间");
JMenuItem jmp2 = new JMenuItem("背景颜色(H)");
JMenuItem jmi5 = new JMenuItem("删除内容(S)");
JMenuItem jmi7=new JMenuItem("关于自己");
JTextArea jta = new JTextArea(23,15);
JScrollPane jsp = new JScrollPane(jta);
JMenuItem jmdb1=new JMenuItem("黄色");//定义各种颜色
JMenuItem jmdb2=new JMenuItem("红色");
JMenuItem jmdb3=new JMenuItem("绿色");
JMenuItem jmdb4=new JMenuItem("蓝色");
JMenuItem jmdb5=new JMenuItem("黑色");
NoteBook()
{ this.addWindowListener(this);
setBounds(200,200,200,200);
c.add(jmb,BorderLayout.NORTH);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("记事本");
jmb.add(jm1);//文件
jm1.setMnemonic('f');
jm1.addActionListener(this);
jmb.add(jm2);//编辑
jm2.setMnemonic('e');
jmb.add(jm4);//格式
jm4.setMnemonic('o');
jm4.addActionListener(this);
jmb.add(jm5);
jm5.setMnemonic('v'); //查看
jmb.add(jm3);//帮助
jm3.setMnemonic('h');
jmb.setLocation(0,0);
jm1.add(jmi1);
jmi1.addActionListener(this);//新建
jmi1.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK,false));
jm1.addSeparator();
jm1.add(jmi2);
jmi2.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));
jmi2.addActionListener(this);//打开
jm1.addSeparator();
jm1.add(jmi3);
jmi3.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));
jmi3.addActionListener(this);//保存
jm1.addSeparator();
jm1.add(jmi4);
jmi1.setMnemonic('n');
jmi2.setMnemonic('o');
jmi3.setMnemonic('s');
jmi4.setMnemonic('x');
jmi4.addActionListener(this);//退出
jm5.add(jmi5); //删除
jm5.addSeparator();
jmi5.addActionListener(this);
jm4.addSeparator();
jm4.add(jmd4);
jm4.add(jmd0);
jmd4.add(jmdb1);
jmd4.add(jmdb2);
jmd4.add(jmdb3);
jmd4.add(jmdb4);
jmd4.add(jmdb5);
jmd4.addActionListener(this); //添加监视器
jmdb1.addActionListener(this);
jmdb2.addActionListener(this);
jmdb3.addActionListener(this);
jmdb4.addActionListener(this);
jmdb5.addActionListener(this);
jm5.add(jmp1);
jm5.addSeparator();
jm5.add(jmp2);
jmp1.addActionListener(this);//日期时间
jmp2.addActionListener(this);//背景颜色
jm2.add(aa);
jm2.addSeparator();
jm2.add(bb);
jm2.addSeparator();
jm2.add(cc);
jm2.addSeparator();
jmd0.add(jcuti1);
jmd0.addSeparator();
jmd0.add(jxieti1);
aa.setMnemonic('C');
aa.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
bb.setMnemonic('x');
bb.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
cc.setMnemonic('V');
cc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
aa.addActionListener(this);//复制
bb.addActionListener(this);//剪切
cc.addActionListener(this);//粘贴
jmd4.addActionListener(this);
jcuti1.addItemListener(this);//粗体
jxieti1.addItemListener(this);//斜体
jm3.add(jmi7);
jmi7.addActionListener(this);//关于自己
c.add(jToolBar,BorderLayout.CENTER); //工具条
jButton2.setIcon(new ImageIcon("image\\new.JPG"));//新建
jToolBar.add(jButton2);
jButton2.setToolTipText("新建");
jButton2.addActionListener(this);
jButton1.setIcon(new ImageIcon("image\\open.JPG"));//打开
jButton1.setToolTipText("打开");
jToolBar.add(jButton1);
jButton1.addActionListener(this);
jButton3.setIcon(new ImageIcon("image\\save.JPG"));//保存
jToolBar.add(jButton3);
jButton3.setToolTipText("保存");
jButton3.addActionListener(this);
jButton4.setIcon(new ImageIcon("image\\qq.JPG"));
jButton4.addActionListener(this); //退出
jToolBar.add(jButton4);
jButton4.setToolTipText("退出");
c.add(jsp,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
jmdb1.addActionListener(new ActionListener() //黄色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.yellow);
} } );
jmdb2.addActionListener(new ActionListener() //红色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.red);
} } );
jmdb3.addActionListener(new ActionListener() //绿色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.green);
} } );
jmdb4.addActionListener(new ActionListener() //蓝色
{
public void actionPerformed(ActionEvent e)
{
jta.setForeground(Color.blue);
} } );
jmdb5.addActionListener(new ActionListener() //黑色
{
public void actionPerformed(ActionEvent e)
{
jta.setForeground(Color.black);
} } );
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==jmi1)//新建
{jta.setText("");
this.setTitle("无标题 - 记事本");
}
if(e.getSource()==jmi2)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
{ try {
f1 = jfc1.getSelectedFile();
this.setTitle(f1.getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
{jta.setText(str);}
fr.close();
br.close(); }
catch(FileNotFoundException e1)
{e1.printStackTrace();}
catch(IOException e2){e2.printStackTrace();}
}}
if(e.getSource()==jmi3)//保存
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}}}
if(e.getSource()==jmi4)//退出
{
int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
this.dispose();
}}}
if(e.getSource()==jmp1)//日期时间
{
Calendar c1 =Calendar.getInstance();
int y = c1.get(Calendar.YEAR);
int m = c1.get(Calendar.MONTH);
int d = c1.get(Calendar.DATE);
int h = c1.get(Calendar.HOUR);
int m1 = c1.get(Calendar.MINUTE);
int m2 = m+1;
jta.setText(y+"年"+m2+"月"+d+"日"+h+":"+m1);
}
if(e.getSource()==jmp2)//背景色
{ Color ccc = JColorChooser.showDialog(this,"color",Color.BLACK);
jta.setSelectedTextColor(ccc);
jta.setBackground(ccc);
}
if(e.getSource()==jButton1)//打开
{File f1;
JFileChooser jfc1 = new JFileChooser();
int num1 = jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
{
try
{
f1 = jfc1.getSelectedFile();
this.setTitle(f1.getName());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
{jta.setText(str);}
fr.close();
br.close(); }
catch(FileNotFoundException e1)
{e1.printStackTrace();}
catch(IOException e2){e2.printStackTrace();}
} }
if(e.getSource()==jButton2)
{jta.setText("");
this.setTitle("文本编辑器");
}
if(e.getSource()==jmi5){
jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd()); //删除
}
if(e.getSource()==jButton3)
{File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}}}
if(e.getSource()==aa) //复制
{jta.copy();}
else if(e.getSource()==bb)//剪切
{ jta.cut(); }
else if(e.getSource()==cc)//粘贴
{jta.paste();}
if(e.getSource()==jButton4)
{int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
this.dispose();
}}}
//if(e.getSource()==jmi7)//关于我
//{About1 a=new About1();//实现跳转
// a.setVisible(true);
//}
}
public void itemStateChanged(ItemEvent e) //字体
{Font c=new Font("Serif",Font.PLAIN,14);
if(e.getItemSelectable()==jcuti1)
{if(jcuti1.getState()==true)
{ Font f=new Font("Serif",Font.BOLD,20); //粗体
jta.setFont(f);}
else{jta.setFont(c);}
}
else if(e.getItemSelectable()==jxieti1)
{if(jxieti1.getState()==true)
{ Font b=new Font("Serif",Font.ITALIC,20);//斜体
jta.setFont(b);}
else{jta.setFont(c);
} } }
public static void main(String[] args)//入口
{ new NoteBook(); }
public void windowOpened(WindowEvent arg0) {}
public void windowClosing(WindowEvent arg0) {
int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jta.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
if(a==2){}
}
}
public void windowClosed(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个是反编译的吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个功能要全些!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dbswing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
JMenuItem jMenuItem9 = new JMenuItem();
JMenuItem jMenuItem10 = new JMenuItem();
JMenuItem jMenuItem11 = new JMenuItem();
FontChooser fontChooser1 = new FontChooser();
JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
JFileChooser jFileChooser1 = new JFileChooser();
String currentFileName=null;
boolean motified=false;
Document document1;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
document1 = jTextArea1.getDocument();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(600, 400));
this.setTitle("文本编辑器");
statusBar.setText(" ");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("帮助");
jMenuHelpAbout.setText("关于");
jMenuHelpAbout.addActionListener(new Frame1_jMenuHelpAbout_ActionAdapter(this));
jTextArea1.setText("");
jMenu1.setText("编辑");
jMenu2.setText("格式");
jMenuItem4.setText("新建");
jMenuItem4.addActionListener(new Frame1_jMenuItem4_actionAdapter(this));
jMenuItem3.setText("打开");
jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this));
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this));
jMenuItem1.setText("另存为");
jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new Frame1_jMenuItem5_actionAdapter(this));
jMenuItem6.setText("复制");
jMenuItem6.addActionListener(new Frame1_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("粘贴");
jMenuItem7.addActionListener(new Frame1_jMenuItem7_actionAdapter(this));
jMenuItem9.setText("字体");
jMenuItem9.addActionListener(new Frame1_jMenuItem9_actionAdapter(this));
jMenuItem10.setText("前景色");
jMenuItem10.addActionListener(new Frame1_jMenuItem10_actionAdapter(this));
jMenuItem11.setText("背景色");
fontChooser1.setFrame(this);
fontChooser1.setTitle("字体");
jCheckBoxMenuItem1.setText("自动换行");
jCheckBoxMenuItem1.addActionListener(new Frame1_jCheckBoxMenuItem1_actionAdapter(this));
document1.addDocumentListener(new Frame1_document1_documentAdapter(this));
jMenuFile.add(jMenuItem4);
jMenuFile.add(jMenuItem3);
jMenuFile.add(jMenuItem2);
jMenuFile.add(jMenuItem1);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
jMenu1.add(jMenuItem5);
jMenu1.add(jMenuItem6);
jMenu1.add(jMenuItem7);
jMenu2.add(jCheckBoxMenuItem1);
jMenu2.add(jMenuItem9);
jMenu2.add(jMenuItem10);
jMenu2.add(jMenuItem11);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
if(giveUp())
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
Frame1_AboutBox dlg = new Frame1_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
//super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
void jMenuItem9_actionPerformed(ActionEvent e) {
fontChooser1.setSelectedFont(jTextArea1.getFont());
if(fontChooser1.showDialog())
jTextArea1.setFont(fontChooser1.getSelectedFont());
this.repaint();
}
void jMenuItem10_actionPerformed(ActionEvent e) {
Color color=JColorChooser.showDialog(this,"前景色",jTextArea1.getForeground());
if(color!=null)
jTextArea1.setForeground(color);
this.repaint();
}
void jCheckBoxMenuItem1_actionPerformed(ActionEvent e) {
jTextArea1.setLineWrap(jCheckBoxMenuItem1.isSelected());
}
void jMenuItem5_actionPerformed(ActionEvent e) {
jTextArea1.cut();
}
void jMenuItem7_actionPerformed(ActionEvent e) {
jTextArea1.paste();
}
void jMenuItem6_actionPerformed(ActionEvent e) {
jTextArea1.copy();
}
void jMenuItem4_actionPerformed(ActionEvent e) {
if(giveUp()){
jTextArea1.setText("");
currentFileName=null;
motified=false;
}
}
void jMenuItem3_actionPerformed(ActionEvent e) {
if(!giveUp()){
return;
}
if(JFileChooser.APPROVE_OPTION==jFileChooser1.showOpenDialog(this)){
openFile(jFileChooser1.getSelectedFile().getPath());
this.repaint();
}
}
void openFile(String fileName){
try{
File file=new File(fileName);
int size=(int)file.length();
int chars_read=0;
FileReader in=new FileReader(file);
char[] data=new char[size];
while(in.ready()){
chars_read+=in.read(data,chars_read,size-chars_read);
}
jTextArea1.setText(new String(data,0,chars_read));
this.currentFileName=fileName;
this.motified=false;
}
catch(IOException e){
jTextArea1.setText("打开文件"+fileName+"错");
}
}
boolean saveFile(){
if(currentFileName==null)
return saveAsFile();
try{
FileWriter out=new FileWriter(currentFileName);
String text=jTextArea1.getText();
out.write(text);
out.close();
this.motified=false;
return true;
}
catch(IOException e){
}
return false;
}
boolean saveAsFile(){
if(JFileChooser.APPROVE_OPTION==jFileChooser1.showSaveDialog(this)){
currentFileName=jFileChooser1.getSelectedFile().getPath();
return saveFile();
}else{
return false;
}
}
void jMenuItem2_actionPerformed(ActionEvent e) {
saveFile();
}
void jMenuItem1_actionPerformed(ActionEvent e) {
saveAsFile();
}
void document1_changedUpdate(DocumentEvent e) {
motified=true;
}
void document1_insertUpdate(DocumentEvent e) {
motified=true;
}
void document1_removeUpdate(DocumentEvent e) {
motified=true;
}
boolean giveUp(){
if(!motified)
return true;
int temp=JOptionPane.showConfirmDialog(this,"保存修改吗?","Text Edit",JOptionPane.YES_NO_CANCEL_OPTION);
switch(temp){
case JOptionPane.YES_OPTION: return saveFile();
case JOptionPane.NO_OPTION: return true;
case JOptionPane.CANCEL_OPTION:
default:
return false;
}
}
}
class Frame1_jMenuFileExit_ActionAdapter implements ActionListener {
Frame1 adaptee;
Frame1_jMenuFileExit_ActionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuFileExit_actionPerformed(e);
}
}
class Frame1_jMenuHelpAbout_ActionAdapter implements ActionListener {
Frame1 adaptee;
Frame1_jMenuHelpAbout_ActionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuHelpAbout_actionPerformed(e);
}
}
class Frame1_jMenuItem9_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem9_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem9_actionPerformed(e);
}
}
class Frame1_jMenuItem10_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem10_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem10_actionPerformed(e);
}
}
class Frame1_jCheckBoxMenuItem1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jCheckBoxMenuItem1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jCheckBoxMenuItem1_actionPerformed(e);
}
}
class Frame1_jMenuItem5_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem5_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem5_actionPerformed(e);
}
}
class Frame1_jMenuItem7_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem7_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem7_actionPerformed(e);
}
}
class Frame1_jMenuItem6_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem6_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem6_actionPerformed(e);
}
}
class Frame1_jMenuItem4_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem4_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem4_actionPerformed(e);
}
}
class Frame1_jMenuItem3_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem3_actionPerformed(e);
}
}
class Frame1_jMenuItem2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class Frame1_jMenuItem1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class Frame1_document1_documentAdapter implements javax.swing.event.DocumentListener {
Frame1 adaptee;
Frame1_document1_documentAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void insertUpdate(DocumentEvent e) {
adaptee.document1_insertUpdate(e);
}
public void removeUpdate(DocumentEvent e) {
adaptee.document1_removeUpdate(e);
}
public void changedUpdate(DocumentEvent e) {
adaptee.document1_changedUpdate(e);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.dbswing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
JMenuItem jMenuItem9 = new JMenuItem();
JMenuItem jMenuItem10 = new JMenuItem();
JMenuItem jMenuItem11 = new JMenuItem();
FontChooser fontChooser1 = new FontChooser();
JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
JFileChooser jFileChooser1 = new JFileChooser();
String currentFileName=null;
boolean motified=false;
Document document1;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
document1 = jTextArea1.getDocument();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(600, 400));
this.setTitle("文本编辑器");
statusBar.setText(" ");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("帮助");
jMenuHelpAbout.setText("关于");
jMenuHelpAbout.addActionListener(new Frame1_jMenuHelpAbout_ActionAdapter(this));
jTextArea1.setText("");
jMenu1.setText("编辑");
jMenu2.setText("格式");
jMenuItem4.setText("新建");
jMenuItem4.addActionListener(new Frame1_jMenuItem4_actionAdapter(this));
jMenuItem3.setText("打开");
jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this));
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this));
jMenuItem1.setText("另存为");
jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new Frame1_jMenuItem5_actionAdapter(this));
jMenuItem6.setText("复制");
jMenuItem6.addActionListener(new Frame1_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("粘贴");
jMenuItem7.addActionListener(new Frame1_jMenuItem7_actionAdapter(this));
jMenuItem9.setText("字体");
jMenuItem9.addActionListener(new Frame1_jMenuItem9_actionAdapter(this));
jMenuItem10.setText("前景色");
jMenuItem10.addActionListener(new Frame1_jMenuItem10_actionAdapter(this));
jMenuItem11.setText("背景色");
fontChooser1.setFrame(this);
fontChooser1.setTitle("字体");
jCheckBoxMenuItem1.setText("自动换行");
jCheckBoxMenuItem1.addActionListener(new Frame1_jCheckBoxMenuItem1_actionAdapter(this));
document1.addDocumentListener(new Frame1_document1_documentAdapter(this));
jMenuFile.add(jMenuItem4);
jMenuFile.add(jMenuItem3);
jMenuFile.add(jMenuItem2);
jMenuFile.add(jMenuItem1);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
jMenu1.add(jMenuItem5);
jMenu1.add(jMenuItem6);
jMenu1.add(jMenuItem7);
jMenu2.add(jCheckBoxMenuItem1);
jMenu2.add(jMenuItem9);
jMenu2.add(jMenuItem10);
jMenu2.add(jMenuItem11);
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
if(giveUp())
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
Frame1_AboutBox dlg = new Frame1_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
//super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
void jMenuItem9_actionPerformed(ActionEvent e) {
fontChooser1.setSelectedFont(jTextArea1.getFont());
if(fontChooser1.showDialog())
jTextArea1.setFont(fontChooser1.getSelectedFont());
this.repaint();
}
void jMenuItem10_actionPerformed(ActionEvent e) {
Color color=JColorChooser.showDialog(this,"前景色",jTextArea1.getForeground());
if(color!=null)
jTextArea1.setForeground(color);
this.repaint();
}
void jCheckBoxMenuItem1_actionPerformed(ActionEvent e) {
jTextArea1.setLineWrap(jCheckBoxMenuItem1.isSelected());
}
void jMenuItem5_actionPerformed(ActionEvent e) {
jTextArea1.cut();
}
void jMenuItem7_actionPerformed(ActionEvent e) {
jTextArea1.paste();
}
void jMenuItem6_actionPerformed(ActionEvent e) {
jTextArea1.copy();
}
void jMenuItem4_actionPerformed(ActionEvent e) {
if(giveUp()){
jTextArea1.setText("");
currentFileName=null;
motified=false;
}
}
void jMenuItem3_actionPerformed(ActionEvent e) {
if(!giveUp()){
return;
}
if(JFileChooser.APPROVE_OPTION==jFileChooser1.showOpenDialog(this)){
openFile(jFileChooser1.getSelectedFile().getPath());
this.repaint();
}
}
void openFile(String fileName){
try{
File file=new File(fileName);
int size=(int)file.length();
int chars_read=0;
FileReader in=new FileReader(file);
char[] data=new char[size];
while(in.ready()){
chars_read+=in.read(data,chars_read,size-chars_read);
}
jTextArea1.setText(new String(data,0,chars_read));
this.currentFileName=fileName;
this.motified=false;
}
catch(IOException e){
jTextArea1.setText("打开文件"+fileName+"错");
}
}
boolean saveFile(){
if(currentFileName==null)
return saveAsFile();
try{
FileWriter out=new FileWriter(currentFileName);
String text=jTextArea1.getText();
out.write(text);
out.close();
this.motified=false;
return true;
}
catch(IOException e){
}
return false;
}
boolean saveAsFile(){
if(JFileChooser.APPROVE_OPTION==jFileChooser1.showSaveDialog(this)){
currentFileName=jFileChooser1.getSelectedFile().getPath();
return saveFile();
}else{
return false;
}
}
void jMenuItem2_actionPerformed(ActionEvent e) {
saveFile();
}
void jMenuItem1_actionPerformed(ActionEvent e) {
saveAsFile();
}
void document1_changedUpdate(DocumentEvent e) {
motified=true;
}
void document1_insertUpdate(DocumentEvent e) {
motified=true;
}
void document1_removeUpdate(DocumentEvent e) {
motified=true;
}
boolean giveUp(){
if(!motified)
return true;
int temp=JOptionPane.showConfirmDialog(this,"保存修改吗?","Text Edit",JOptionPane.YES_NO_CANCEL_OPTION);
switch(temp){
case JOptionPane.YES_OPTION: return saveFile();
case JOptionPane.NO_OPTION: return true;
case JOptionPane.CANCEL_OPTION:
default:
return false;
}
}
}
class Frame1_jMenuFileExit_ActionAdapter implements ActionListener {
Frame1 adaptee;
Frame1_jMenuFileExit_ActionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuFileExit_actionPerformed(e);
}
}
class Frame1_jMenuHelpAbout_ActionAdapter implements ActionListener {
Frame1 adaptee;
Frame1_jMenuHelpAbout_ActionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuHelpAbout_actionPerformed(e);
}
}
class Frame1_jMenuItem9_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem9_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem9_actionPerformed(e);
}
}
class Frame1_jMenuItem10_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem10_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem10_actionPerformed(e);
}
}
class Frame1_jCheckBoxMenuItem1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jCheckBoxMenuItem1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jCheckBoxMenuItem1_actionPerformed(e);
}
}
class Frame1_jMenuItem5_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem5_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem5_actionPerformed(e);
}
}
class Frame1_jMenuItem7_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem7_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem7_actionPerformed(e);
}
}
class Frame1_jMenuItem6_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem6_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem6_actionPerformed(e);
}
}
class Frame1_jMenuItem4_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem4_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem4_actionPerformed(e);
}
}
class Frame1_jMenuItem3_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem3_actionPerformed(e);
}
}
class Frame1_jMenuItem2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class Frame1_jMenuItem1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jMenuItem1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class Frame1_document1_documentAdapter implements javax.swing.event.DocumentListener {
Frame1 adaptee;
Frame1_document1_documentAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void insertUpdate(DocumentEvent e) {
adaptee.document1_insertUpdate(e);
}
public void removeUpdate(DocumentEvent e) {
adaptee.document1_removeUpdate(e);
}
public void changedUpdate(DocumentEvent e) {
adaptee.document1_changedUpdate(e);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询