JAVA中菜单组件的实例:文本编辑器

JAVA中菜单组件的实例:文本编辑器

1.       使用菜单的两种方式

菜单通常有两种使用方式:窗口菜单和快捷菜单

1)  窗口菜单

窗口菜单式相对于窗口的,它出现在窗口的标题栏下,总是与窗口同时出现。

JAVA的窗口菜单有菜单栏JMenuBar、菜单JMenu和菜单项JMenuItem等组件共同完成。窗口上添加菜单栏,菜单栏中添加菜单,菜单中添加菜单项或子菜单,这样就形成了窗口菜单的多层结构。菜单栏添加在窗口上方,不受布局管理器的控制。

2)  快捷菜单

快捷菜单则是相对于某个制定组件的,当鼠标指向某组件并单击右键时,则会弹出一个菜单,这个菜单称为快捷菜单。快捷菜单也有若干菜单项组成,快捷菜单的结构比较简单,一般只有二级子菜单。

JAVA的快捷菜单是JPopupMenu组件。任何Component组件都可以调用add()方法添加快捷菜单,快捷菜单也不受布局管理器的控制。显示快捷菜单时,必须为其指定位置。

2.       文本编辑器的实例

本例是一个简化的文本编辑器,可实现对文本区选中字符串进行剪切、复制、粘贴等操作,可以改变文本区文字的颜色,大小,字体,当字号值出错或不合适时,弹出提示对话框。演示了文本区、复选框组件的使用方法,演示菜单、快捷菜单的使用方法,其中使用了java.awt.Font字体类。程序运行的结果如下:

程序如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

 

public class EditJFrame extends JFrame implements ActionListener, ItemListener, MouseListener {

 

private JTextField textSize;

private JCheckBox boxBold,boxItalic;

private JButton buttonCut,buttonCopy,buttonPaste;

private JTextArea textArea;

private JPopupMenu popupMenu;

private JDialog dialog;

private JLabel labelDialog;

private JRadioButtonMenuItem red,blue,green;

private JCheckBoxMenuItem CboxBold,CboxItalic;

public EditJFrame()

{

           super("文本编辑器");

           this.setSize(500,300);

           this.setLocation(300, 240);

           this.setDefaultCloseOperation(EXIT_ON_CLOSE);

          

           textArea = new JTextArea("北京北京我爱你!");

           textArea.addMouseListener(this);

           this.add(textArea);

          

           JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

           this.add(panel,"North");

           textSize = new JTextField("12",10);

           panel.add(textSize);

           textSize.addActionListener(this);

          

           boxBold = new JCheckBox("粗体");

           panel.add(boxBold);

           boxBold.addItemListener(this);

          

           boxItalic = new JCheckBox("斜体");

           panel.add(boxItalic);

           boxItalic.addItemListener(this);

          

           this.addmyMenu();

           this.setVisible(true);

}

private void addmyMenu() {

           // TODO Auto-generated method stub

           JMenuBar menuBar = new JMenuBar();

           this.setJMenuBar(menuBar);

           JMenu menuFile = new JMenu("文件");

           menuBar.add(menuFile);

           menuFile.add(new JMenuItem("打开"));

           menuFile.add(new JMenuItem("保存"));

           menuFile.addSeparator();

          

           JMenuItem menuExit = new JMenuItem("退出");

           menuFile.add(menuExit);

           menuExit.addActionListener(this);

          

           JMenu menuEdit = new JMenu("编辑");

           menuBar.add(menuEdit);

          

           JMenu menuStyle = new JMenu("字形");

           CboxBold =new JCheckBoxMenuItem("粗体");

           CboxItalic = new JCheckBoxMenuItem("斜体");

           menuStyle.add(CboxBold);

           menuStyle.add(CboxItalic);

           CboxBold.addItemListener(this);

           CboxItalic.addItemListener(this);

           menuEdit.add(menuStyle);

          

          

           JMenu menuColor = new JMenu("颜色");

           menuEdit.add(menuColor);

          

           ButtonGroup buttonGroup = new ButtonGroup();

           red = new JRadioButtonMenuItem("",false);

           buttonGroup.add(red);

           menuColor.add(red);

           red.addItemListener(this);

          

           green = new JRadioButtonMenuItem("绿",false);

           buttonGroup.add(green);

           menuColor.add(green);

           green.addItemListener(this);

          

           blue = new JRadioButtonMenuItem("",false);

           buttonGroup.add(blue);

           menuColor.add(blue);

           blue.addItemListener(this);

          

           menuBar.add(new JMenu("帮助"));

          

           popupMenu = new JPopupMenu();

          

           JMenuItem itemCut = new JMenuItem("剪切");

           itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));

           popupMenu.add(itemCut);

           itemCut.addActionListener(this);

          

           JMenuItem itemCopy = new JMenuItem("复制");

           itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));

           popupMenu.add(itemCopy);

           itemCopy.addActionListener(this);

          

           JMenuItem itemPaste = new JMenuItem("粘贴");

           itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));

           popupMenu.add(itemPaste);

           itemPaste.addActionListener(this);

          

           textArea.add(popupMenu);

          

           dialog = new JDialog(this,"提示");

           dialog.setSize(240,100);

           labelDialog = new JLabel("",JLabel.CENTER);

           dialog.add(labelDialog);

           dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);   

}

@Override

public void actionPerformed(ActionEvent e) {

           // TODO Auto-generated method stub

           if(e.getActionCommand() == "退出")

           {

                    System.exit(0);

           }

           if(e.getActionCommand() == "剪切")

           {

                    textArea.cut();

           }

           if(e.getActionCommand() == "复制")

           {

               textArea.copy();

           }

           if(e.getActionCommand() == "粘贴")

           {

                    textArea.paste();

           }

           if(e.getSource() == textSize)

           {

                    int size = 0;

                    try

                    {

                             size = Integer.parseInt(textSize.getText());

                             if (size <= 0 || size >72)

                               throw new Exception("SizeException");

                                       java.awt.Font font = textArea.getFont();

                                       textArea.setFont(new Font(font.getName(),font.getStyle(),size));

                    }

                    catch(NumberFormatException nfe)

                    {

                             labelDialog.setText("/""+textSize.getText()+"/"不能转换成整数,请重新输入!");

                             dialog.setLocation(this.getX()+100,this.getY()+100);

                             dialog.setVisible(true);

                    }

                    catch(Exception ex)

                    {

                             if(ex.getMessage() == "SizeException")

                             {

                                       labelDialog.setText(size + " 字号不合适,请重新输入!");

                                       dialog.setLocation(this.getX()+100,this.getY()+100);

                                       dialog.setVisible(true);

                             }

                    }

                    finally{}             

           }

}

public void itemStateChanged(ItemEvent e)

{

           Font font = textArea.getFont();

           int style = font.getStyle();

           if(e.getSource() == boxBold || e.getSource() == CboxBold)

           {

                    style ^= 1;

           }

           if(e.getSource() == boxItalic || e.getSource() == CboxItalic)

           {

                    style ^= 2;

           }

           if(e.getSource() == red)

           {

                    textArea.setForeground(Color.red);                

           }

           if(e.getSource() == green)

           {

                    textArea.setForeground(Color.green);                     

           }

           if(e.getSource() == blue)

           {

                    textArea.setForeground(Color.blue);              

           }

           textArea.setFont(new Font(font.getName(),style,font.getSize()));

}

//也可以通过适配器实现

public void mouseClicked(MouseEvent mec)

{

           if(mec.getModifiers() == mec.BUTTON3_MASK)

                    popupMenu.show(textArea, mec.getX(), mec.getY());

}

public void mouseEntered(MouseEvent arg0) {}

public void mouseExited(MouseEvent arg0) {}

                  public void mousePressed(MouseEvent arg0) { }

public void mouseReleased(MouseEvent arg0) {    }

public static void main(String[] args) {

           // TODO Auto-generated method stub

           new EditJFrame();

}

}

3.       总结

本实例主要演示了JAVA在界面设计中菜单、文本区、复选框、快捷键等组件的使用方法,这是每一个桌面应用程序都有的,应该掌握。

你可能感兴趣的:(JAVA)