Java by API/javax.swing/JRadioButtonMenuItem — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
JRadioButtonMenuItem: setAccelerator(KeyStroke key)
import java.awt.Event;
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.KeyStroke;
public class MainClass extends JFrame {
public MainClass() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu elementMenu = new JMenu("Elements");
fileMenu.setMnemonic("F");
elementMenu.setMnemonic("E");
// Construct the file pull down menu
newItem = fileMenu.add("New");
openItem = fileMenu.add("Open");
closeItem = fileMenu.add("Close");
fileMenu.addSeparator();
saveItem = fileMenu.add("Save");
saveAsItem = fileMenu.add("Save As...");
JMenu colorMenu = new JMenu("Color");
fileMenu.addSeparator();
printItem = fileMenu.add("Print");
newItem.setAccelerator(KeyStroke.getKeyStroke("N", Event.CTRL_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke("O", Event.CTRL_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke("S", Event.CTRL_MASK));
printItem.setAccelerator(KeyStroke.getKeyStroke("P", Event.CTRL_MASK));
elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
elementMenu
.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
ButtonGroup types = new ButtonGroup();
types.add(lineItem);
types.add(rectangleItem);
types.add(circleItem);
types.add(curveItem);
lineItem.setAccelerator(KeyStroke.getKeyStroke("L", Event.CTRL_MASK));
rectangleItem.setAccelerator(KeyStroke.getKeyStroke("E", Event.CTRL_MASK));
circleItem.setAccelerator(KeyStroke.getKeyStroke("I", Event.CTRL_MASK));
curveItem.setAccelerator(KeyStroke.getKeyStroke("V", Event.CTRL_MASK));
elementMenu.addSeparator();
elementMenu.add(colorMenu); // Add the sub-menu
colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
// Add element color accelerators
redItem.setAccelerator(KeyStroke.getKeyStroke("R", Event.CTRL_MASK));
yellowItem.setAccelerator(KeyStroke.getKeyStroke("Y", Event.CTRL_MASK));
greenItem.setAccelerator(KeyStroke.getKeyStroke("G", Event.CTRL_MASK));
blueItem.setAccelerator(KeyStroke.getKeyStroke("B", Event.CTRL_MASK));
menuBar.add(fileMenu);
menuBar.add(elementMenu);
}
private JMenuBar menuBar = new JMenuBar();
// File menu items
private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem,
printItem;
// Element menu items
private JRadioButtonMenuItem lineItem, rectangleItem, circleItem, curveItem,
textItem;
private JCheckBoxMenuItem redItem, yellowItem, greenItem, blueItem;
public static void main(String[] args) {
JFrame aWindow = new MainClass();
aWindow.setSize(200,200);
aWindow.setVisible(true);
}
}
JRadioButtonMenuItem: setFont(Font font)
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class Main extends JFrame {
public static final String FontName[] = { "Serif", "SansSerif", "Courier" };
private Font[] fonts = new Font[FontName.length];
protected JMenuItem[] fontMenus;
public Main() {
super();
setSize(450, 350);
for (int k = 0; k < FontName.length; k++)
fonts[k] = new Font(FontName[k], Font.PLAIN, 12);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
protected JMenuBar createMenuBar() {
final JMenuBar menuBar = new JMenuBar();
JMenu mFont = new JMenu("Font");
mFont.setMnemonic("o");
ButtonGroup group = new ButtonGroup();
fontMenus = new JMenuItem[FontName.length];
for (int k = 0; k < FontName.length; k++) {
int m = k + 1;
fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontName[k]);
boolean selected = (k == 0);
fontMenus[k].setSelected(selected);
fontMenus[k].setMnemonic("1" + k);
fontMenus[k].setFont(fonts[k]);
fontMenus[k].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JComponent) arg0.getSource())
.getFont());
}
});
group.add(fontMenus[k]);
mFont.add(fontMenus[k]);
}
menuBar.add(mFont);
return menuBar;
}
public static void main(String argv[]) {
new Main();
}
}
JRadioButtonMenuItem: setMnemonic(int mnemonic)
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class Main extends JFrame {
public static final String FontName[] = { "Serif", "SansSerif", "Courier" };
private Font[] fonts = new Font[FontName.length];
protected JMenuItem[] fontMenus;
public Main() {
super();
setSize(450, 350);
for (int k = 0; k < FontName.length; k++)
fonts[k] = new Font(FontName[k], Font.PLAIN, 12);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
protected JMenuBar createMenuBar() {
final JMenuBar menuBar = new JMenuBar();
JMenu mFont = new JMenu("Font");
mFont.setMnemonic("o");
ButtonGroup group = new ButtonGroup();
fontMenus = new JMenuItem[FontName.length];
for (int k = 0; k < FontName.length; k++) {
int m = k + 1;
fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontName[k]);
boolean selected = (k == 0);
fontMenus[k].setSelected(selected);
fontMenus[k].setMnemonic("1" + k);
fontMenus[k].setFont(fonts[k]);
fontMenus[k].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JComponent) arg0.getSource())
.getFont());
}
});
group.add(fontMenus[k]);
mFont.add(fontMenus[k]);
}
menuBar.add(mFont);
return menuBar;
}
public static void main(String argv[]) {
new Main();
}
}
JRadioButtonMenuItem: setSelected(boolean b)
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class Main extends JFrame {
public static final String FontName[] = { "Serif", "SansSerif", "Courier" };
private Font[] fonts = new Font[FontName.length];
protected JMenuItem[] fontMenus;
public Main() {
super();
setSize(450, 350);
for (int k = 0; k < FontName.length; k++)
fonts[k] = new Font(FontName[k], Font.PLAIN, 12);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
protected JMenuBar createMenuBar() {
final JMenuBar menuBar = new JMenuBar();
JMenu mFont = new JMenu("Font");
mFont.setMnemonic("o");
ButtonGroup group = new ButtonGroup();
fontMenus = new JMenuItem[FontName.length];
for (int k = 0; k < FontName.length; k++) {
int m = k + 1;
fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontName[k]);
boolean selected = (k == 0);
fontMenus[k].setSelected(selected);
fontMenus[k].setMnemonic("1" + k);
fontMenus[k].setFont(fonts[k]);
fontMenus[k].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JComponent) arg0.getSource())
.getFont());
}
});
group.add(fontMenus[k]);
mFont.add(fontMenus[k]);
}
menuBar.add(mFont);
return menuBar;
}
public static void main(String argv[]) {
new Main();
}
}
new JRadioButtonMenuItem(String text)
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JRadioButtonMenuItem Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Options");
menu.setMnemonic(KeyEvent.VK_O);
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("A");
group.add(menuItem);
menu.add(menuItem);
menuItem = new JRadioButtonMenuItem("B");
group.add(menuItem);
menu.add(menuItem);
menuItem = new JRadioButtonMenuItem("C");
group.add(menuItem);
menu.add(menuItem);
bar.add(menu);
f.setJMenuBar(bar);
f.setSize(300, 200);
f.setVisible(true);
}
}