• Java基础之创建窗口——向窗口中添加菜单(Sketcher)


    控制台程序。

    JMenuBar对象表示放在窗口顶部的菜单栏。可以为JMenuBar对象添加JMenu或JMenuItem对象,它们都显示在菜单栏上。JMenu对象是带有标签的菜单,单击就可以显示一列菜单项。JMenuItem对象表示菜单中带有标签的菜单项,单击就会执行某个程序动作。

     1 // Main window for the Sketcher application
     2 import javax.swing.*;
     3 import static java.awt.event.InputEvent.*;                             // For modifier constants
     4 
     5 @SuppressWarnings("serial")
     6 public class SketcherFrame extends JFrame {
     7   // Constructor
     8   public SketcherFrame(String title) {
     9     setTitle(title);                                                   // Set the window title
    10     setDefaultCloseOperation(EXIT_ON_CLOSE);
    11 
    12     setJMenuBar(menuBar);                                              // Add the menu bar to the window
    13 
    14     JMenu fileMenu = new JMenu("File");                                // Create File menu
    15     JMenu elementMenu = new JMenu("Elements");                         // Create Elements menu
    16     fileMenu.setMnemonic('F');                                         // Create shortcut
    17     elementMenu.setMnemonic('E');                                      // Create shortcut
    18 
    19     // Construct the file drop-down menu
    20     newItem = fileMenu.add("New");                                      // Add New item
    21     openItem = fileMenu.add("Open");                                    // Add Open item
    22     closeItem = fileMenu.add("Close");                                  // Add Close item
    23     fileMenu.addSeparator();                                            // Add separator
    24     saveItem = fileMenu.add("Save");                                    // Add Save item
    25     saveAsItem = fileMenu.add("Save As...");                            // Add Save As item
    26     fileMenu.addSeparator();                                            // Add separator
    27     printItem = fileMenu.add("Print");                                  // Add Print item
    28 
    29     // Add File menu accelerators
    30     newItem.setAccelerator(KeyStroke.getKeyStroke('N',CTRL_DOWN_MASK ));
    31     openItem.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
    32     saveItem.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
    33     printItem.setAccelerator(KeyStroke.getKeyStroke('P', CTRL_DOWN_MASK));
    34 
    35     // Construct the Element drop-down menu
    36     elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    37     elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    38     elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    39     elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    40     ButtonGroup types = new ButtonGroup();
    41     types.add(lineItem);
    42     types.add(rectangleItem);
    43     types.add(circleItem);
    44     types.add(curveItem);
    45 
    46     // Add element type accelerators
    47     lineItem.setAccelerator(KeyStroke.getKeyStroke('L', CTRL_DOWN_MASK));
    48     rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', CTRL_DOWN_MASK));
    49     circleItem.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
    50     curveItem.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
    51 
    52     elementMenu.addSeparator();
    53     JMenu colorMenu = new JMenu("Color");                              // Color submenu
    54     elementMenu.add(colorMenu);                                        // Add the submenu
    55     colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    56     colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    57     colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    58     colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
    59 
    60     // Add element color accelerators
    61     redItem.setAccelerator(KeyStroke.getKeyStroke('R', CTRL_DOWN_MASK));
    62     yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', CTRL_DOWN_MASK));
    63     greenItem.setAccelerator(KeyStroke.getKeyStroke('G', CTRL_DOWN_MASK));
    64     blueItem.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
    65 
    66     menuBar.add(fileMenu);                                             // Add the file menu
    67     menuBar.add(elementMenu);                                          // Add the element menu
    68   }
    69 
    70   private JMenuBar menuBar = new JMenuBar();                           // Window menu bar
    71 
    72   // File menu items
    73   private JMenuItem newItem,  openItem,   closeItem,
    74                     saveItem, saveAsItem, printItem;
    75 
    76   // Element menu items
    77   private JRadioButtonMenuItem lineItem,  rectangleItem, circleItem,   // Types
    78                                curveItem, textItem;
    79   private JCheckBoxMenuItem    redItem,   yellowItem,                  // Colors
    80                                greenItem, blueItem ;
    81 }
     1 // Sketching application
     2 import javax.swing.*;
     3 import java.awt.*;
     4 
     5 import javax.swing.SwingUtilities;
     6 
     7 public class Sketcher {
     8   public static void main(String[] args) {
     9     SwingUtilities.invokeLater(new Runnable() {
    10             public void run() {
    11                 createWindow();
    12             }
    13         });
    14   }
    15 
    16   public static void createWindow(){
    17     window = new SketcherFrame("Sketcher");                            // Create the app window
    18     Toolkit theKit = window.getToolkit();                              // Get the window toolkit
    19     Dimension wndSize = theKit.getScreenSize();                        // Get screen size
    20 
    21     // Set the position to screen center & size to half screen size
    22     window.setSize(wndSize.width/2, wndSize.height/2);                 // Set window size
    23     window.setLocationRelativeTo(null);                                // Center window
    24     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    25 
    26     window.setVisible(true);
    27   }
    28 
    29   private static SketcherFrame window;                                 // The application window
    30 }
  • 相关阅读:
    Qt全局坐标和相对坐标
    坐标系统
    qt坐标系统见解
    Qt中QGraphics类坐标映射关系详解
    对QT多线程以及事件投递的理解
    Qt中mouseMoveEvent无效
    递归、尾递归
    末日重生
    201314
    归并排序
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3466657.html
Copyright © 2020-2023  润新知