Java Tutorial/Swing/JCheckBoxMenuItem
Содержание
- 1 Check if a JCheckBoxMenuItem is selected in its item change listener
- 2 Customizing JCheckBoxMenuItem Look and Feel
- 3 Initialize JCheckBoxMenuItem with checked option
- 4 Listening to JCheckBoxMenuItem Events with an ActionListener
- 5 Listening to JCheckBoxMenuItem with an ItemListener
- 6 Using JCheckBoxMenuItem: setMnemonic
Check if a JCheckBoxMenuItem is selected in its item change listener
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main extends JFrame {
JCheckBoxMenuItem check = new JCheckBoxMenuItem("Check");
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Checkable");
bar.add(menu);
menu.add(check);
check.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("Checked? " + check.isSelected());
}
});
setJMenuBar(bar);
getContentPane().add(new JLabel("A placeholder"));
pack();
setSize(300, 300);
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}
Customizing JCheckBoxMenuItem Look and Feel
Property StringObject TypeCheckBoxMenuItem.acceleratorFontFontCheckBoxMenuItem.acceleratorForegroundColorCheckBoxMenuItem.acceleratorSelectionForegroundColorCheckBoxMenuItem.actionMapActionMapCheckBoxMenuItem.arrowIconIconCheckBoxMenuItem.backgroundColorCheckBoxMenuItem.borderBorderCheckBoxMenuItem.borderPaintedBooleanCheckBoxMenuItem.checkIconIconCheckBoxMenuItem.rumandSoundStringCheckBoxMenuItem.disabledForegroundColorCheckBoxMenuItem.fontFontCheckBoxMenuItem.foregroundColorCheckBoxMenuItem.gradientListCheckBoxMenuItem.marginInsetsCheckBoxMenuItem.opaqueBooleanCheckBoxMenuItem.selectionBackgroundColorCheckBoxMenuItem.selectionForegroundColorCheckBoxMenuItemUIString
Initialize JCheckBoxMenuItem with checked option
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;
public class SwingWinMenus extends JFrame {
public static int WIDTH = 300;
public static int HEIGHT = 300;
public static String TITLE = "SwingWin";
Container frameContainer;
JTextField textField = new JTextField(50);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem fileNew = new JMenuItem("New");
JMenuItem fileOpen = new JMenuItem("Open");
JMenuItem fileSave = new JMenuItem("Save");
JMenuItem fileExit = new JMenuItem("Exit");
JMenu editMenu = new JMenu("Edit");
JMenuItem editCut = new JMenuItem("Cut");
JMenuItem editCopy = new JMenuItem("Copy");
JMenuItem editPaste = new JMenuItem("Paste");
JMenu specialMenu = new JMenu("Special");
JCheckBoxMenuItem specialCheck1 = new JCheckBoxMenuItem("Check 1");
JCheckBoxMenuItem specialCheck2 = new JCheckBoxMenuItem("Check 2", true);
JSeparator separator = new JSeparator();
JRadioButtonMenuItem specialRadio1 = new JRadioButtonMenuItem("Radio 1");
JRadioButtonMenuItem specialRadio2 = new JRadioButtonMenuItem("Radio 2");
ButtonGroup buttonGroup = new ButtonGroup();
public SwingWinMenus() {
super("");
fileMenu.add(fileNew);
fileMenu.add(fileOpen);
fileMenu.add(fileSave);
fileMenu.add(fileExit);
editMenu.add(editCut);
editMenu.add(editCopy);
editMenu.add(editPaste);
specialMenu.add(specialCheck1);
specialMenu.add(specialCheck2);
specialMenu.add(separator);
buttonGroup.add(specialRadio1);
buttonGroup.add(specialRadio2);
specialMenu.add(specialRadio1);
specialMenu.add(specialRadio2);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(specialMenu);
setJMenuBar(menuBar);
frameContainer = getContentPane();
frameContainer.setLayout(null);
textField.setBounds(100, 100, 100, 20);
frameContainer.add(textField);
setupEventHandlers();
setSize(450, 450);
setVisible(true);
}
void setupEventHandlers() {
addWindowListener(new WindowHandler());
fileNew.addActionListener(new MenuItemHandler());
fileOpen.addActionListener(new MenuItemHandler());
fileSave.addActionListener(new MenuItemHandler());
fileExit.addActionListener(new MenuItemHandler());
editCut.addActionListener(new MenuItemHandler());
editCopy.addActionListener(new MenuItemHandler());
editPaste.addActionListener(new MenuItemHandler());
specialCheck1.addItemListener(new ItemHandler());
specialCheck2.addItemListener(new ItemHandler());
specialRadio1.addItemListener(new ItemHandler());
specialRadio2.addItemListener(new ItemHandler());
}
public static void main(String[] args) {
SwingWinMenus app = new SwingWinMenus();
}
public class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public class MenuItemHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Exit"))
System.exit(0);
else
textField.setText(cmd);
}
}
public class ItemHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
AbstractButton button = (AbstractButton) e.getItem();
String label = button.getText();
if (button.isSelected())
label += " true";
else
label += " false";
textField.setText(label);
}
}
}
Listening to JCheckBoxMenuItem Events with an ActionListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JCheckBoxMenuItemActionListener {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
ActionListener aListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
AbstractButton aButton = (AbstractButton) event.getSource();
boolean selected = aButton.getModel().isSelected();
String newLabel;
Icon newIcon;
if (selected) {
newLabel = "A";
} else {
newLabel = "B";
}
aButton.setText(newLabel);
}
};
caseMenuItem.addActionListener(aListener);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Listening to JCheckBoxMenuItem with an ItemListener
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JCheckBoxMenuItemActionListener {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
ItemListener iListener = new ItemListener() {
public void itemStateChanged(ItemEvent event) {
Icon girlIcon = new ImageIcon("girl-r.jpg");
Icon boyIcon = new ImageIcon("boy-r.jpg");
AbstractButton aButton = (AbstractButton)event.getSource();
int state = event.getStateChange();
String newLabel;
Icon newIcon;
if (state == ItemEvent.SELECTED) {
newLabel = "Girl";
newIcon = girlIcon;
} else {
newLabel = "Boy";
newIcon = boyIcon;
}
aButton.setText(newLabel);
aButton.setIcon(newIcon);
}
};
caseMenuItem.addItemListener(iListener);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
Using JCheckBoxMenuItem: setMnemonic
import java.awt.event.KeyEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JCheckBoxMenuItemDemo {
public static void main(final String args[]) {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
// File->New, N - Mnemonic
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
caseMenuItem.setMnemonic(KeyEvent.VK_C);
fileMenu.add(caseMenuItem);
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}