JMenuBar
類提供選單欄的實現。
以下是javax.swing.JMenuBar
類的宣告 -
public class JMenuBar
extends JComponent
implements Accessible, MenuElement
編號 | 建構函式 | 描述 |
---|---|---|
1 | JMenuBar() |
建立一個新的選單欄。 |
以下是Swing JMenuBar類中的方法列表。
該類繼承以下類中的方法 -
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
使用編輯器建立以下Java程式:JMenuBarDemo.java
package com.yiibai.menu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JMenuBarDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public JMenuBarDemo() {
prepareGUI();
}
public static void main(String[] args) {
JMenuBarDemo swingMenuDemo = new JMenuBarDemo();
swingMenuDemo.showMenuDemo();
}
private void prepareGUI() {
mainFrame = new JFrame("Java SWING JMenuBar範例(tw511.com)");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showMenuDemo() {
// create a menu bar
final JMenuBar menuBar = new JMenuBar();
// create menus
JMenu fileMenu = new JMenu("檔案");
JMenu editMenu = new JMenu("編輯");
final JMenu aboutMenu = new JMenu("關於");
final JMenu linkMenu = new JMenu("連結");
// create menu items
JMenuItem newMenuItem = new JMenuItem("新建");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New");
JMenuItem openMenuItem = new JMenuItem("開啟");
openMenuItem.setActionCommand("Open");
JMenuItem saveMenuItem = new JMenuItem("儲存");
saveMenuItem.setActionCommand("Save");
JMenuItem exitMenuItem = new JMenuItem("退出");
exitMenuItem.setActionCommand("Exit");
JMenuItem cutMenuItem = new JMenuItem("剪下");
cutMenuItem.setActionCommand("Cut");
JMenuItem copyMenuItem = new JMenuItem("複製");
copyMenuItem.setActionCommand("Copy");
JMenuItem pasteMenuItem = new JMenuItem("貼上");
pasteMenuItem.setActionCommand("Paste");
MenuItemListener menuItemListener = new MenuItemListener();
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
pasteMenuItem.addActionListener(menuItemListener);
final JCheckBoxMenuItem showWindowMenu = new JCheckBoxMenuItem("顯示關於", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (showWindowMenu.getState()) {
menuBar.add(aboutMenu);
} else {
menuBar.remove(aboutMenu);
}
}
});
final JRadioButtonMenuItem showLinksMenu = new JRadioButtonMenuItem("顯示連結", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (menuBar.getMenu(3) != null) {
menuBar.remove(linkMenu);
mainFrame.repaint();
} else {
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
});
// add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
// add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu);
// add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand() + " JMenuItem clicked.");
}
}
}
執行上面範例程式碼,得到以下結果: