JAVA: 메뉴 창 간단하게 추가 (swing)

20290 단어
  1 package com.le.menu;
  2 
  3 import java.awt.Color;
  4 import java.awt.Container;
  5 import java.awt.FlowLayout;
  6 import java.awt.GridLayout;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.awt.event.ItemEvent;
 10 import java.awt.event.ItemListener;
 11 import java.awt.event.KeyEvent;
 12 
 13 import javax.swing.JFrame;
 14 import javax.swing.JLabel;
 15 import javax.swing.JMenu;
 16 import javax.swing.JMenuBar;
 17 import javax.swing.JMenuItem;
 18 import javax.swing.JPanel;
 19 import javax.swing.JRadioButtonMenuItem;
 20 
 21 public class MenuManagerShow {
 22     private MenuManagerShow() {
 23         init();
 24     }
 25 
 26     public static void main(String[] args) {
 27         MenuManagerShow menuManager = new MenuManagerShow();
 28         menuManager.show();
 29     }
 30 
 31     private void show() {
 32 
 33     }
 34 
 35     class MenuItemListener implements ActionListener {
 36         @Override
 37         public void actionPerformed(ActionEvent e) {
 38             statusLabel.setText("JMenu Item clicked:" + e.getActionCommand() + ".");
 39         }
 40     }
 41 
 42     /**
 43      *          
 44      * 
 45      * @param mainFrame
 46      *               
 47      * @see   :JFrame           (FlowLayout)           ,
48 * , 。
49 * : JFrame , 。
50 */ 51 private void initFrameBgColor(JFrame mFrame) { 52 Container pane = mFrame.getContentPane(); 53 pane.setBackground(Color.orange); 54 } 55 56 /** 57 * 58 */ 59 private static final String MENU_MANAGER_TITILE = "Manager Tool"; 60 61 private JFrame mainFrame; 62 private JLabel headerLabel; 63 private JLabel statusLabel; 64 private JPanel controlPanel; 65 66 private void init() { 67 // 68 mainFrame = new JFrame(); 69 // 70 mainFrame.setTitle(MENU_MANAGER_TITILE); 71 // 72 mainFrame.setSize(400, 400); 73 // 74 mainFrame.setLocation(400, 200); 75 // (3) 76 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 77 /* 78 * mainFrame.addWindowListener(new WindowAdapter() { public void 79 * windowClosing(WindowEvent windowEvent) { System.exit(0); } }); 80 */ 81 // 82 mainFrame.setResizable(true); 83 mainFrame.setLayout(new GridLayout(3, 1)); 84 // Frame , initFrameBgColor() 85 // mainFrame.setBackground(Color.blue); 86 initFrameBgColor(mainFrame); 87 88 headerLabel = new JLabel("11", JLabel.CENTER); 89 statusLabel = new JLabel("22", JLabel.CENTER); 90 controlPanel = new JPanel(); 91 controlPanel.setLayout(new FlowLayout()); 92 controlPanel.setBackground(Color.gray); 93 94 mainFrame.add(headerLabel); 95 mainFrame.add(controlPanel); 96 mainFrame.add(statusLabel); 97 98 initMenuBar(mainFrame); 99 100 mainFrame.setVisible(true); 101 } 102 103 /** 104 * 105 * 106 * @param mainFrame 107 */ 108 private void initMenuBar(JFrame mFrame) { 109 // 110 final JMenuBar menuBar = new JMenuBar(); 111 112 // 113 JMenu fileMenu = new JMenu("File"); 114 JMenu editMenu = new JMenu("Edit"); 115 final JMenu aboutMenu = new JMenu("About"); 116 final JMenu linkMenu = new JMenu("Links"); 117 118 // 119 JMenuItem newMenuItem = new JMenuItem("New"); 120 newMenuItem.setMnemonic(KeyEvent.VK_N); 121 newMenuItem.setActionCommand("New"); 122 123 // : 124 final JRadioButtonMenuItem showLinksMenu = new JRadioButtonMenuItem("Show Links", true); 125 showLinksMenu.addItemListener(new ItemListener() { 126 public void itemStateChanged(ItemEvent e) { 127 // state, 1 , 2 128 // System.out.println(e.getStateChange()); 129 // 130 if (menuBar.getMenu(3) != null) { 131 menuBar.remove(linkMenu); 132 // 133 mainFrame.repaint(); 134 } else { 135 menuBar.add(linkMenu); 136 mainFrame.repaint(); 137 } 138 } 139 }); 140 141 // 142 MenuItemListener menuItemListener = new MenuItemListener(); 143 144 // 145 newMenuItem.addActionListener(menuItemListener); 146 147 fileMenu.add(newMenuItem); 148 fileMenu.add(showLinksMenu); 149 150 menuBar.add(fileMenu); 151 menuBar.add(editMenu); 152 menuBar.add(aboutMenu); 153 menuBar.add(linkMenu); 154 // 155 mFrame.setJMenuBar(menuBar); 156 } 157 }

좋은 웹페이지 즐겨찾기