| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 813 人关注过本帖
标题:迷你记事本
只看楼主 加入收藏
韩明涛
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-11-18
收藏
 问题点数:0 回复次数:1 
迷你记事本
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();
 }
}
搜索更多相关主题的帖子: 记事本 
2008-01-06 10:28
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
收藏
得分:0 
支持一下

但是我想问一下,这个记事本好像不能自动换行。是不是因为你用的是JTextArea ?
如果想让记事本在输完一行后自动换行,应该用什么呢?

2008-01-21 00:27
快速回复:迷你记事本
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017111 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved