java记事本时间代码 java记事本代码运行( 六 )


p2.add(btn1);btn1.setBounds(360,0,75,25);
p3.setLayout( null);p3.add(btn2);btn2.setBounds(360,10,75,25);p3.add(btn3);btn3.setBounds(160,10,75,25);
dialog.add(p4);p4.setBackground(Color.yellow);p4.setBounds(5,120,440,138);
dialog1.add(ta1);
//设置字体的下拉情况
MenuItem Font=new MenuItem("字体");//给字体加监听器
Font.addActionListener(this);
MenuItem Replace=new MenuItem("替换");MenuItem Seek=new MenuItem("查找");
//首要添加组件的情况
mb.add(file);mb.add(edit);mb.add(pattern);mb.add(time);mb.add(help);
file.add(renew);file.addSeparator();file.add(open);time.add(tdate);
file.addSeparator(); file.add(exit);file.addSeparator();file.add(save);
edit.add(copy); edit.addSeparator();edit.add(paste);edit.addSeparator();edit.add(cut);
pattern.add(Font);pattern.add(Replace);
pattern.add(Seek);help.add(helptopic);
//给必要的组件加上监听器
renew.addActionListener( this);tdate.addActionListener(this);
open.addActionListener(this); save.addActionListener(this);exit.addActionListener(this);
cut.addActionListener(this);copy.addActionListener(this);paste.addActionListener(this);
ce1.addItemListener(this);ce2.addItemListener(this);ce3.addItemListener(this);
//添加必要的组件
add(ta,"Center");ta.setBackground(new Color(249,255,234));
setSize(700,500);
setVisible(true);
this.validate() ;
}
public void itemStateChanged(ItemEvent e) {
if (ce2.getSelectedItem().equals("粗体"))
{ k=1;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
if (ce2.getSelectedItem().equals("斜体"))
{ k=2;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
if (ce2.getSelectedItem().equals("常规"))
{ k=3;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("新建"))
{System.out.println("new ");
ta.setText("");}
if(e.getActionCommand().equals("退出"))
{ System.exit(0);}
try
{
if(e.getActionCommand().equals("打开"))
openText();
if(e.getActionCommand().equals("保存"))
saveText();
}catch(IOException e1){}
if(e.getActionCommand().equals("复制"))
{ str=ta.getSelectedText();}
if(e.getActionCommand().equals("粘贴"))
{ta.insert(str,ta.getCaretPosition());}
if(e.getActionCommand().equals("剪切"))
{str=ta.getSelectedText();ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
if(e.getActionCommand().equals("字体"))
{dialog.setVisible(true);}
if(e.getActionCommand().equals("主题"))
{dialog1.setVisible(true);}
if(e.getActionCommand().equals("颜色"))
{
Color clr=JColorChooser.showDialog(this,"颜色对话框",null);
ta.setForeground(clr);
}
if(e.getSource()==btn2)
dialog.setVisible(false);
if(e.getSource()==btn1)
{
if(k==1)
{ ta.setFont(new Font(st1,Font.BOLD,Integer.parseInt(st2)));}
if(k==2)
{ ta.setFont(new Font(st1,Font.ITALIC,Integer.parseInt(st2)));}
if(k==3)
{ ta.setFont(new Font(st1,Font.PLAIN,Integer.parseInt(st2)));}
dialog.setVisible(false);
}
//if (e.getActionCommand().equals("日期/时间"))
//ta.setText(ta.getText()+""+date);
if(e.getActionCommand().equals("日期/时间"))
{
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;
ta.setText(ta.getText()+""+(y+"年"+m2+"月"+d+"日"+h+":"+m1));
}
}
public void openText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);

推荐阅读