Java by API/javax.swing/AbstractButton
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 AbstractButton: addActionListener(ActionListener l)
- 2 AbstractButton: addChangeListener(ChangeListener l)
- 3 AbstractButton: addItemListener(ItemListener l)
- 4 AbstractButton: getActionCommand()
- 5 AbstractButton: setActionCommand(String actionCommand)
- 6 AbstractButton: setBackground(Color bg)
- 7 AbstractButton: setForeground(Color fg)
- 8 AbstractButton: setHorizontalAlignment(int alignment)
- 9 AbstractButton: setHorizontalTextPosition(int textPosition)
- 10 AbstractButton: setMnemonic(char mnemonic)
AbstractButton: addActionListener(ActionListener l)
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();
}
}
AbstractButton: addChangeListener(ChangeListener l)
/*
*/
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] args) {
JButton jb = new JButton("Press Me");
jb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
System.out.println("ItemEvent!");
}
});
jb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ev) {
System.out.println("ChangeEvent!");
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jb);
f.pack();
f.setVisible(true);
}
}
AbstractButton: addItemListener(ItemListener l)
/*
*/
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] args) {
JButton jb = new JButton("Press Me");
jb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
System.out.println("ItemEvent!");
}
});
jb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ev) {
System.out.println("ChangeEvent!");
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jb);
f.pack();
f.setVisible(true);
}
}
AbstractButton: getActionCommand()
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class MainClass {
public static void main(String[] args) {
JRadioButton dem = new JRadioButton("Bill", false);
dem.setActionCommand("Bill");
JRadioButton rep = new JRadioButton("Bob", false);
rep.setActionCommand("Bob");
JRadioButton ind = new JRadioButton("Ross", false);
ind.setActionCommand("Ross");
final ButtonGroup group = new ButtonGroup();
group.add(dem);
group.add(rep);
group.add(ind);
class VoteActionListener implements ActionListener {
public void actionPerformed(ActionEvent ex) {
String choice = group.getSelection().getActionCommand();
System.out.println("ACTION Candidate Selected: " + choice);
}
}
class VoteItemListener implements ItemListener {
public void itemStateChanged(ItemEvent ex) {
String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item);
}
}
ActionListener al = new VoteActionListener();
dem.addActionListener(al);
rep.addActionListener(al);
ind.addActionListener(al);
ItemListener il = new VoteItemListener();
dem.addItemListener(il);
rep.addItemListener(il);
ind.addItemListener(il);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
c.setLayout(new GridLayout(4, 1));
c.add(new JLabel("Please Cast Your Vote"));
c.add(dem);
c.add(rep);
c.add(ind);
frame.pack();
frame.setVisible(true);
}
}
AbstractButton: setActionCommand(String actionCommand)
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class MainClass {
public static void main(String[] args) {
JRadioButton dem = new JRadioButton("Bill", false);
dem.setActionCommand("Bill");
JRadioButton rep = new JRadioButton("Bob", false);
rep.setActionCommand("Bob");
JRadioButton ind = new JRadioButton("Ross", false);
ind.setActionCommand("Ross");
final ButtonGroup group = new ButtonGroup();
group.add(dem);
group.add(rep);
group.add(ind);
class VoteActionListener implements ActionListener {
public void actionPerformed(ActionEvent ex) {
String choice = group.getSelection().getActionCommand();
System.out.println("ACTION Candidate Selected: " + choice);
}
}
class VoteItemListener implements ItemListener {
public void itemStateChanged(ItemEvent ex) {
String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item);
}
}
ActionListener al = new VoteActionListener();
dem.addActionListener(al);
rep.addActionListener(al);
ind.addActionListener(al);
ItemListener il = new VoteItemListener();
dem.addItemListener(il);
rep.addItemListener(il);
ind.addItemListener(il);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
c.setLayout(new GridLayout(4, 1));
c.add(new JLabel("Please Cast Your Vote"));
c.add(dem);
c.add(rep);
c.add(ind);
frame.pack();
frame.setVisible(true);
}
}
AbstractButton: setBackground(Color bg)
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Iconizing CheckBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton abstractButton = (AbstractButton)itemEvent.getSource();
Color foreground = abstractButton.getForeground();
Color background = abstractButton.getBackground();
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
abstractButton.setForeground(background);
abstractButton.setBackground(foreground);
}
}
};
aCheckBox4.addItemListener(itemListener);
frame.add(aCheckBox4);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
AbstractButton: setForeground(Color fg)
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Iconizing CheckBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton abstractButton = (AbstractButton)itemEvent.getSource();
Color foreground = abstractButton.getForeground();
Color background = abstractButton.getBackground();
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
abstractButton.setForeground(background);
abstractButton.setBackground(foreground);
}
}
};
aCheckBox4.addItemListener(itemListener);
frame.add(aCheckBox4);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
AbstractButton: setHorizontalAlignment(int alignment)
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jb = new JButton("Ok", new ImageIcon("bullet.gif"));
jb.setHorizontalAlignment(SwingConstants.LEFT);
jb.setMnemonic("O");
getContentPane().add(jb, BorderLayout.CENTER);
jb = new JButton("<html><i>Cancel</i></html>");
jb.setVerticalAlignment(SwingConstants.BOTTOM);
jb.setDefaultCapable(true);
getContentPane().add(jb, BorderLayout.EAST);
getRootPane().setDefaultButton(jb);
setSize(200, 100);
setVisible(true);
}
public static void main(String[] args) {
new MainClass("Button Demo");
}
}
AbstractButton: setHorizontalTextPosition(int textPosition)
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
public class Main extends JPanel {
public JMenuBar menuBar;
public JToolBar toolBar;
public Main() {
menuBar = new JMenuBar();
JMenu justifyMenu = new JMenu("Justify");
ActionListener actionPrinter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Action [" + e.getActionCommand()+ "] performed!\n");
}
};
JCheckBoxMenuItem leftJustify = new JCheckBoxMenuItem("Left",
new ImageIcon("1.gif"));
leftJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
leftJustify.setAccelerator(KeyStroke.getKeyStroke("L", Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask()));
leftJustify.addActionListener(actionPrinter);
JCheckBoxMenuItem rightJustify = new JCheckBoxMenuItem("Right",
new ImageIcon("2.gif"));
rightJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
rightJustify.setAccelerator(KeyStroke.getKeyStroke("R", Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask()));
rightJustify.addActionListener(actionPrinter);
JCheckBoxMenuItem centerJustify = new JCheckBoxMenuItem("Center",
new ImageIcon("3.gif"));
centerJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
centerJustify.setAccelerator(KeyStroke.getKeyStroke("M", Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask()));
centerJustify.addActionListener(actionPrinter);
JCheckBoxMenuItem fullJustify = new JCheckBoxMenuItem("Full",
new ImageIcon("4.gif"));
fullJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
fullJustify.setAccelerator(KeyStroke.getKeyStroke("F", Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask()));
fullJustify.addActionListener(actionPrinter);
justifyMenu.add(leftJustify);
justifyMenu.add(rightJustify);
justifyMenu.add(centerJustify);
justifyMenu.add(fullJustify);
menuBar.add(justifyMenu);
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
}
public static void main(String s[]) {
Main example = new Main();
JFrame frame = new JFrame("Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(example.menuBar);
frame.pack();
frame.setVisible(true);
}
}
AbstractButton: setMnemonic(char mnemonic)
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class Main extends JPanel {
public Main() {
JCheckBox m_chkBold = new JCheckBox("Bold");
m_chkBold.setMnemonic("b");
m_chkBold.setToolTipText("Bold font");
add(m_chkBold);
setBorder(new TitledBorder(new EtchedBorder(), "Effects"));
JCheckBox m_chkItalic = new JCheckBox("Italic");
m_chkItalic.setMnemonic("i");
m_chkItalic.setToolTipText("Italic font");
add(m_chkItalic);
JCheckBox m_chkUnderline = new JCheckBox("Underline");
m_chkUnderline.setMnemonic("u");
m_chkUnderline.setToolTipText("Underline font");
add(m_chkUnderline);
JCheckBox m_chkStrikethrough = new JCheckBox("Strikethrough");
m_chkStrikethrough.setMnemonic("r");
m_chkStrikethrough.setToolTipText("Strikethrough font");
add(m_chkStrikethrough);
JCheckBox m_chkSubscript = new JCheckBox("Subscript");
m_chkSubscript.setMnemonic("t");
m_chkSubscript.setToolTipText("Subscript font");
add(m_chkSubscript);
JCheckBox m_chkSuperscript = new JCheckBox("Superscript");
m_chkSuperscript.setMnemonic("p");
m_chkSuperscript.setToolTipText("Superscript font");
add(m_chkSuperscript);
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Main());
f.pack();
f.setSize(new Dimension(300, 200));
f.setVisible(true);
}
}