Swing JCheckboxMenuItem類及例子
JCheckBoxMenuItem類代表一個複選框,可以包含在一個菜單。選中該複選框菜單更改控制的狀態中,從開到關或從關到開。
類聲明
以下是聲明 javax.swing.JCheckBoxMenuItem類:
public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants, Accessible
類構造函數
S.N.
構造函數 & 描述
1
JCheckboxMenuItem()
Creates an initially unselected check box menu item with no set text or icon.
2
JCheckboxMenuItem(Action a)
Creates a menu item whose properties are taken from the Action supplied.
3
JCheckboxMenuItem(Icon icon)
Creates an initially unselected check box menu item with an icon.
4
JCheckboxMenuItem(String text)
Creates an initially unselected check box menu item with text.
5
JCheckboxMenuItem(String text, boolean b)
Creates a check box menu item with the specified text and selection state.
6
JCheckboxMenuItem(String text, Icon icon)
Creates an initially unselected check box menu item with the specified text and icon.
7
JCheckboxMenuItem(String text, Icon icon, boolean b)
Creates a check box menu item with the specified text, icon, and selection state.
類方法
S.N.
方法 & 描述
1
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JCheckBoxMenuItem.
2
Object[] getSelectedObjects()
Returns an array (length 1) containing the check box menu item label or null if the check box is not selected.
3
boolean getState()
Returns the selected-state of the item.
4
String getUIClassID()
Returns the name of the L&F class that renders this component.
5
protected String paramString()
Returns a string representation of this JCheckBoxMenuItem.
6
void setState(boolean b)
Sets the selected-state of the item.
方法繼承
這個類從以下類繼承的方法:
javax.swing.JMenuItem
javax.swing.JAbstractButton
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
JCheckboxMenuItem 例子
選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >
SwingMenuDemo.java
package com.yiibai.gui; import java.awt.*; import java.awt.event.*; public class SwingMenuDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingMenuDemo(){ prepareGUI(); } public static void main(String[] args){ SwingMenuDemo swingMenuDemo = new SwingMenuDemo(); swingMenuDemo.showMenuDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); 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("File"); JMenu editMenu = new JMenu("Edit"); final JMenu aboutMenu = new JMenu("About"); final JMenu linkMenu = new JMenu("Links"); //create menu items JMenuItem newMenuItem = new JMenuItem("New"); newMenuItem.setMnemonic(KeyEvent.VK_N); newMenuItem.setActionCommand("New"); JMenuItem openMenuItem = new JMenuItem("Open"); openMenuItem.setActionCommand("Open"); JMenuItem saveMenuItem = new JMenuItem("Save"); saveMenuItem.setActionCommand("Save"); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.setActionCommand("Exit"); JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.setActionCommand("Cut"); JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.setActionCommand("Copy"); JMenuItem pasteMenuItem = new JMenuItem("Paste"); 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("Show About", 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("Show Links", 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."); } } }
編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。
D:SWING>javac comyiibaiguiSwingMenuDemo.java
如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。
D:SWING>java com.yiibai.gui.SwingMenuDemo
驗證下面的輸出。(點擊文件菜單中取消選取「Show About」菜單項。)