标签:
菜单栏组件
菜单按钮(MenuComponent)是一种特殊组建,只能被放在一种称为“菜单容器”的组件里。
1.菜单栏
菜单栏(MenuBar)是窗口中的主菜单,用来包容一组子菜单,它只有一个构造方法:
MuneBar()
菜单栏不支持事件监听程序,在菜单栏区域产生的所有事件都会被菜单栏自动处理。
2.菜单
菜单(Menu)中最基本的是下拉菜单。下拉菜单可以分为两种类型,一种是tear-off菜单,另一种是非tea-off菜单。二者的区别在于:当鼠标键被释放后,tear-off菜单的内容仍然显示在屏幕上。
菜单有三种构造方式:
(1)public Menu()构造一个不带标签的非tear-off菜单。
(2)public Menu(String label)构造一个带指定标签的非tear-off标签,菜单的标签由String型参数指定。
(3)public Menu(String label,boolean tearOff)构造一个带指定标签的菜单,菜单的标签由String型参数指定。boolean型参数指定菜单是否为tear-off型菜单。
菜单可以被加入菜单栏或者另一个菜单中。通过MenuBar类中定义的setHelpMenu()方法可以指定某个菜单为帮助菜单。
3.菜单项
如果将整个菜单系统看作一棵树,那么菜单项(MenuItem)就是这棵树的叶子。菜单项通常被加入菜单中,它有三种构造方式:
(1)public MenuItem()创造一个没有标签和快捷方式的菜单项
(2)public MenuItem(String label)创建一个带标签但是没有快捷方式的菜单项
(3)public MenuItem(String label,MenuShortcut s)创建一个带标签和快捷方式的菜单项
当菜单中的菜单项被选中时,将会引发一个ActionEvent事件,因此通常需要为菜单项注册ActionListener以便对事件作出反应。
1 * @author slinm wolf 2 * 3 */ 4 package imageViewer; 5 import java.awt.*; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.awt.event.KeyEvent; 9 import javax.swing.*; 10 public class ImageViewer{ 11 private JFrame frame; 12 13 public ImageViewer(){ 14 makeFrame(); 15 } 16 17 private void makeFrame(){ 18 frame = new JFrame("我不愿作程序猿"); 19 makeMenuBar(frame); 20 Container contentPane = frame.getContentPane(); 21 contentPane.setLayout(new BorderLayout(5,5)); 22 23 24 JPanel panelWest = new JPanel (new FlowLayout(FlowLayout.LEFT)); 25 JPanel panelInWest = new JPanel(new GridLayout(3,1)); 26 JButton a = new JButton("迷茫"); 27 JButton b = new JButton("放弃"); 28 JButton c = new JButton("NEVER GIVE UP"); 29 panelInWest.add(a); 30 panelInWest.add(b); 31 panelInWest.add(c); 32 panelWest.add(panelInWest); 33 contentPane.add(panelWest,BorderLayout.WEST); 34 35 36 37 JLabel statusLabel1 =new JLabel("我是学渣"); 38 contentPane.add(statusLabel1,BorderLayout.SOUTH); 39 40 Frame frm = null; 41 final JDialog dlg1 = new JDialog(frm,"结局1"); 42 final JDialog dlg2 = new JDialog(frm,"结局2"); 43 final JDialog dlg3 = new JDialog(frm,"结局3"); 44 dlg1.setBounds(470,220,260,150); 45 dlg1.setLayout(null); 46 dlg1.setModal(true); 47 dlg2.setBounds(470,220,260,150); 48 dlg2.setLayout(null); 49 dlg2.setModal(true); 50 dlg3.setBounds(470,220,260,150); 51 dlg3.setLayout(null); 52 dlg3.setModal(true); 53 54 a.addActionListener(new ActionListener(){ 55 @Override 56 public void actionPerformed(ActionEvent e) { 57 // TODO Auto-generated method stub 58 dlg1.setVisible(true); 59 60 }}); 61 62 JButton btn1 = new JButton("You will be a loser."); 63 64 65 btn1.setBounds(30,50,160,30); 66 btn1.addActionListener(new ActionListener(){ 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 // TODO Auto-generated method stub 70 dlg1.setVisible(false); 71 }}); 72 dlg1.add(btn1); 73 74 b.addActionListener(new ActionListener(){ 75 @Override 76 public void actionPerformed(ActionEvent e) { 77 // TODO Auto-generated method stub 78 dlg2.setVisible(true); 79 80 }}); 81 82 JButton btn2 = new JButton("You always are a loser."); 83 84 85 btn2.setBounds(30,50,200,30); 86 btn2.addActionListener(new ActionListener(){ 87 @Override 88 public void actionPerformed(ActionEvent e) { 89 // TODO Auto-generated method stub 90 dlg2.setVisible(false); 91 }}); 92 dlg2.add(btn2); 93 94 95 c.addActionListener(new ActionListener(){ 96 @Override 97 public void actionPerformed(ActionEvent e) { 98 // TODO Auto-generated method stub 99 dlg3.setVisible(true); 100 101 }}); 102 103 JButton btn3 = new JButton("You will succeed."); 104 105 106 btn3.setBounds(30,50,160,30); 107 btn3.addActionListener(new ActionListener(){ 108 @Override 109 public void actionPerformed(ActionEvent e) { 110 // TODO Auto-generated method stub 111 dlg3.setVisible(false); 112 }}); 113 dlg3.add(btn3); 114 115 116 117 frame.setSize(260, 300); 118 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 119 frame.setLocation(d. width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2); 120 frame.setVisible(true); 121 } 122 123 124 125 private void makeMenuBar(JFrame frame) { 126 final int SHORTCUT_MASK = 127 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 128 JMenuBar menubar = new JMenuBar(); 129 frame.setJMenuBar(menubar); 130 131 // create the File menu 132 JMenu menuFile = new JMenu("菜单"); 133 menubar.add(menuFile); 134 135 JMenuItem itemOpen = new JMenuItem("打开"); 136 137 itemOpen.addActionListener(new ActionListener(){ 138 @Override 139 public void actionPerformed(ActionEvent arg0) { 140 // TODO Auto-generated method stub 141 openFile(); 142 } 143 private void openFile() { 144 // TODO Auto-generated method stub 145 146 }}); 147 menuFile.add(itemOpen); 148 JMenuItem itemClose = new JMenuItem("关闭"); 149 menuFile.add(itemClose); 150 menuFile.addSeparator(); 151 152 JMenuItem itemQuit = new JMenuItem("退出"); 153 itemQuit.addActionListener(new ActionListener(){ 154 @Override 155 public void actionPerformed(ActionEvent arg0) { 156 // TODO Auto-generated method stub 157 System.exit(0); 158 }}); 159 menuFile.add(itemQuit); 160 161 } 162 163 164 public static void main(String[] args){ 165 new ImageViewer(); 166 } 167 168 }
标签:
原文地址:http://www.cnblogs.com/dw978228116/p/5300040.html