迷你记事本
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
public class EditorJFrame extends JFrame implements ActionListener,ItemListener,MouseListener
{
private JTextField text_size;
private JCheckBox checkbox_bold,checkbox_italic;
private JTextArea textarea;
private JPopupMenu popupmenu;
private JDialog dialog;
private JLabel label_dialog;
private JButton button_cut,button_copy,button_paste;
private JPanel panel;
private JMenuBar menubar;
private JMenu menu_file,menu_edit,menu_help,menu_color,menu_style;
private JMenuItem menuitem_open,menuitem_save,menuitem_exit,menuitem_cut,menuitem_copy,menuitem_paste;
private JCheckBoxMenuItem checkboxmenuitem_bold,checkboxmenuitem_italic;
private ButtonGroup buttongroup;
private JRadioButtonMenuItem rbmi_red,rbmi_blue,rbmi_green;
public EditorJFrame()
{
this.setTitle("迷你记事本");
this.setSize(500,300);
this.setLocation(300,240);
this.setResizable(true);
this.setBackground(Color.BLUE);
this.setDefaultCloseOperation(3);
this.menubar=new JMenuBar();
this.setJMenuBar(menubar);
this.menu_file=new JMenu("文件");
this.menubar.add(menu_file);
this.menuitem_open=new JMenuItem("打开");
this.menuitem_save=new JMenuItem("保存");
this.menuitem_exit=new JMenuItem("退出");
this.menu_file.add(menuitem_open);
this.menu_file.addSeparator();
this.menu_file.add(menuitem_save);
this.menu_file.addSeparator();
this.menu_file.add(menuitem_exit);
this.menu_file.addSeparator();
this.menuitem_exit.addActionListener(this);
this.menu_edit=new JMenu("编辑");
this.menubar.add(menu_edit);
this.menu_style=new JMenu("字形");
this.menu_edit.add(menu_style);
this.checkboxmenuitem_bold=new JCheckBoxMenuItem("粗体");
this.menu_style.add(checkboxmenuitem_bold);
this.checkboxmenuitem_italic=new JCheckBoxMenuItem("斜体");
this.menu_style.add(checkboxmenuitem_italic);
this.menu_color=new JMenu("颜色");
this.menu_edit.add(menu_color);
this.buttongroup=new ButtonGroup();
this.rbmi_red=new JRadioButtonMenuItem("红",true);
this.rbmi_blue=new JRadioButtonMenuItem("蓝",true);
this.rbmi_green=new JRadioButtonMenuItem("绿",true);
this.buttongroup.add(rbmi_red);
this.menu_color.add(rbmi_red);
this.buttongroup.add(rbmi_blue);
this.menu_color.add(rbmi_blue);
this.buttongroup.add(rbmi_green);
this.menu_color.add(rbmi_green);
this.menu_help=new JMenu("帮助");
this.menubar.add(menu_help);
this.textarea=new JTextArea("");
this.textarea.setEditable(true);
this.add(textarea);
this.textarea.addMouseListener(this);
this.panel=new JPanel();
this.panel.setLayout(new FlowLayout());
this.add(panel,"North");
this.text_size=new JTextField("20",10);
this.panel.add(text_size);
this.text_size.addActionListener(this);
this.checkbox_bold=new JCheckBox("粗体");
this.panel.add(checkbox_bold);
this.checkbox_bold.addItemListener(this);
this.checkbox_italic=new JCheckBox("斜体");
this.panel.add(checkbox_italic);
this.checkbox_italic.addItemListener(this);
this.popupmenu=new JPopupMenu("编辑");
this.menuitem_copy=new JMenuItem("复制");
this.menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
this.popupmenu.add(menuitem_copy);
this.menuitem_copy.addActionListener(this);
this.menuitem_cut=new JMenuItem("剪切");
this.menuitem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
this.popupmenu.add(menuitem_cut);
this.menuitem_cut.addActionListener(this);
this.menuitem_paste=new JMenuItem("粘贴");
this.menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
this.popupmenu.add(menuitem_paste);
this.menuitem_paste.addActionListener(this);
this.textarea.add(popupmenu);
this.dialog=new JDialog(this,"提示");
this.dialog.setSize(240,80);
this.label_dialog=new JLabel("",JLabel.CENTER);
this.dialog.add(label_dialog);
this.dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ if(e.getActionCommand()=="退出")
System.exit(0);
if(e.getActionCommand()=="剪切")
this.textarea.cut();
if(e.getActionCommand()=="复制")
this.textarea.copy();
if(e.getActionCommand()=="粘贴")
this.textarea.paste();
if(e.getSource()==text_size)
{
int size=0;
try
{
size=Integer.parseInt(this.text_size.getText());
if(size<=0||size>72)
throw new Exception("SizeException");
Font font=this.textarea.getFont();
this.textarea.setFont(new Font(font.getName(),font.getStyle(),size));
}
catch(NumberFormatException nfe)
{ this.label_dialog.setText("\""+this.text_size.getText()+"\"不能转换成整数,请重新输入");
this.dialog.setLocation(this.getX()+100,this.getY()+100);
this.dialog.setVisible(true);
}
catch(Exception ex)
{
if(ex.getMessage()=="SizeException")
{
this.label_dialog.setText(size+"字号不合适,请重新输入");
this.dialog.setLocation(this.getX()+100,this.getY()+100);
this.dialog.setVisible(true);
}
}
finally{}
}
}
public void itemStateChanged(ItemEvent e)
{
Font font=this.textarea.getFont();
int style=font.getStyle();
if(e.getSource()==this.checkbox_bold)
style=style^1;
if(e.getSource()==this.checkbox_italic)
style=style^2;
this.textarea.setFont(new Font(font.getName(),style,font.getSize()));
}
public void mouseClicked(MouseEvent mec)
{
if(mec.getModifiers()==mec.BUTTON3_MASK)
this.popupmenu.show(textarea,mec.getX(),mec.getY());
}
public void mousePressed(MouseEvent mep){}
public void mouseReleased(MouseEvent mer){}
public void mouseEntered(MouseEvent mee){}
public void mouseExited(MouseEvent mex){}
public void mouseDragged(MouseEvent med){}
public static void main(String []args)
{ new EditorJFrame();
}
}