⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menuexample.java

📁 java swing 开发代码
💻 JAVA
字号:
//  MenuExample.java// A simple example of constructing and using menus.//package	jswing.ch14;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class MenuExample extends JPanel {    public JTextPane pane;    public JMenuBar menuBar;    public MenuExample() {        menuBar = new JMenuBar();        JMenu formatMenu = new JMenu("Justify");        formatMenu.setMnemonic('J');        MenuAction leftJustifyAction = new MenuAction("Left",                                           new ImageIcon("left.gif"));        MenuAction rightJustifyAction = new MenuAction("Right",                                           new ImageIcon("right.gif"));        MenuAction centerJustifyAction = new MenuAction("Center",                                           new ImageIcon("center.gif"));        MenuAction fullJustifyAction = new MenuAction("Full",                                           new ImageIcon("full.gif"));        JMenuItem item;        item = formatMenu.add(leftJustifyAction);        item.setMnemonic('L');        item = formatMenu.add(rightJustifyAction);        item.setMnemonic('R');        item = formatMenu.add(centerJustifyAction);        item.setMnemonic('C');        item = formatMenu.add(fullJustifyAction);        item.setMnemonic('F');        menuBar.add(formatMenu);        menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));    }    class MenuAction extends AbstractAction {           public MenuAction(String text, Icon icon) {            super(text,icon);        }        public void actionPerformed(ActionEvent e) {            try { pane.getStyledDocument().insertString(0 ,                  "Action ["+e.getActionCommand()+"] performed!\n", null);            } catch (Exception ex) { ex.printStackTrace(); }         }    }    public static void main(String s[]) {        MenuExample example = new MenuExample();        example.pane = new JTextPane();        example.pane.setPreferredSize(new Dimension(250, 250));        example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));        JFrame frame = new JFrame("Menu Example");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setJMenuBar(example.menuBar);        frame.getContentPane().add(example.pane, BorderLayout.CENTER);        frame.pack();        frame.setVisible(true);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -