Java by API/javax.swing/JCheckBox
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 JCheckBox: addActionListener(ActionListener lis)
- 2 JCheckBox: addChangeListener(ChangeListener lis)
- 3 JCheckBox: addComponentListener(ComponentListener l)
- 4 JCheckBox: addItemListener(ItemListener lis)
- 5 JCheckBox: isSelected()
- 6 JCheckBox: setBorderPaintedFlat(boolean b)
- 7 JCheckBox: setDisabledIcon(Icon disabledIcon)
- 8 JCheckBox: setDisabledSelectedIcon(Icon disabledSelectedIcon)
- 9 JCheckBox: setEnabled(boolean b)
- 10 JCheckBox: setIcon(Icon defaultIcon)
- 11 JCheckBox: setMnemonic(int mnemonic)
- 12 JCheckBox: setPressedIcon(Icon pressedIcon)
- 13 JCheckBox: setRolloverIcon(Icon i)
- 14 JCheckBox: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
- 15 JCheckBox: setSelected(boolean b)
- 16 JCheckBox: setSelectedIcon(Icon i)
- 17 JCheckBox: setText(String text) (HTML)
- 18 JCheckBox: setToolTipText(String text)
- 19 new JCheckBox(Action a)
- 20 new JCheckBox(String text)
- 21 new JCheckBox(String text, boolean selected)
- 22 new JCheckBox(String text, Icon normal)
JCheckBox: addActionListener(ActionListener lis)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] a) {
final String DESELECTED_LABEL = "Deselected";
final String SELECTED_LABEL = "Selected";
JFrame frame = new JFrame("Selecting CheckBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent
.getSource();
boolean selected = abstractButton.getModel().isSelected();
String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
abstractButton.setText(newLabel);
}
};
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent
.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
};
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);
}
}
};
checkBox.addActionListener(actionListener);
checkBox.addChangeListener(changeListener);
checkBox.addItemListener(itemListener);
checkBox.setMnemonic(KeyEvent.VK_S);
frame.add(checkBox, BorderLayout.NORTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
JCheckBox: addChangeListener(ChangeListener lis)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] a) {
final String DESELECTED_LABEL = "Deselected";
final String SELECTED_LABEL = "Selected";
JFrame frame = new JFrame("Selecting CheckBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent
.getSource();
boolean selected = abstractButton.getModel().isSelected();
String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
abstractButton.setText(newLabel);
}
};
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent
.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
};
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);
}
}
};
checkBox.addActionListener(actionListener);
checkBox.addChangeListener(changeListener);
checkBox.addItemListener(itemListener);
checkBox.setMnemonic(KeyEvent.VK_S);
frame.add(checkBox, BorderLayout.NORTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
JCheckBox: addComponentListener(ComponentListener l)
import java.awt.ruponent;
import java.awt.event.ruponentEvent;
import java.awt.event.ruponentListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkbox = new JCheckBox("Label visible", true);
checkbox.addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent e) {
System.out.println("componentHidden event from " + e.getComponent().getClass().getName());
}
public void componentMoved(ComponentEvent e) {
Component c = e.getComponent();
System.out.println("componentMoved event from " + c.getClass().getName()
+ "; new location: " + c.getLocation().x + ", " + c.getLocation().y);
}
public void componentResized(ComponentEvent e) {
Component c = e.getComponent();
System.out.println("componentResized event from " + c.getClass().getName() + "; new size: "
+ c.getSize().width + ", " + c.getSize().height);
}
public void componentShown(ComponentEvent e) {
System.out.println("componentShown event from " + e.getComponent().getClass().getName());
}
});
frame.add(checkbox, "North");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JCheckBox: addItemListener(ItemListener lis)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox) ie.getItem();
int state = ie.getStateChange();
if (state == ItemEvent.SELECTED)
System.out.println(cb.getText() + " selected");
else
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}
JCheckBox: isSelected()
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
// Get the current state of the checkbox
boolean b = checkbox.isSelected();
// Set the state of the checkbox to off
checkbox.setSelected(false);
// Set the state of the checkbox to on
checkbox.setSelected(true);
}
}
JCheckBox: setBorderPaintedFlat(boolean b)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame("Flat CheckBox Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Pizza Toppings");
panel.setBorder(border);
JCheckBox check = new JCheckBox("Anchovies");
check.setBorderPaintedFlat(true);
panel.add(check);
check = new JCheckBox("Garlic");
panel.add(check);
check = new JCheckBox("Onions");
check.setBorderPaintedFlat(true);
panel.add(check);
check = new JCheckBox("Pepperoni");
panel.add(check);
check = new JCheckBox("Spinach");
check.setBorderPaintedFlat(true);
panel.add(check);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
JCheckBox: setDisabledIcon(Icon disabledIcon)
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
// Set the unselected state icon
Icon unselDisIcon = new ImageIcon("nosel-dis-icon.gif");
checkbox.setDisabledIcon(unselDisIcon);
// Set the selected state icon
Icon selDisIcon = new ImageIcon("sel-dis-icon.gif");
checkbox.setDisabledSelectedIcon(selDisIcon);
}
}
JCheckBox: setDisabledSelectedIcon(Icon disabledSelectedIcon)
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
// Set the unselected state icon
Icon unselDisIcon = new ImageIcon("nosel-dis-icon.gif");
checkbox.setDisabledIcon(unselDisIcon);
// Set the selected state icon
Icon selDisIcon = new ImageIcon("sel-dis-icon.gif");
checkbox.setDisabledSelectedIcon(selDisIcon);
}
}
JCheckBox: setEnabled(boolean b)
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main implements ItemListener {
JCheckBox jcbControl = new JCheckBox("Translate");
JCheckBox jcbOption1 = new JCheckBox("A");
JCheckBox jcbOption2 = new JCheckBox("B");
JCheckBox jcbOption3 = new JCheckBox("C");
Main() {
JFrame jfrm = new JFrame("Check Box Demo");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 200);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jcbOption1.setEnabled(false);
jcbOption2.setEnabled(false);
jcbOption3.setEnabled(false);
jcbControl.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
if (jcbControl.isSelected()) {
jcbOption1.setEnabled(true);
jcbOption2.setEnabled(true);
jcbOption3.setEnabled(true);
System.out.println("enabled.");
} else {
jcbOption1.setEnabled(false);
jcbOption2.setEnabled(false);
jcbOption3.setEnabled(false);
System.out.println("disabled.");
}
}
});
jcbOption1.addItemListener(this);
jcbOption2.addItemListener(this);
jcbOption3.addItemListener(this);
jfrm.add(jcbControl);
jfrm.add(jcbOption1);
jfrm.add(jcbOption2);
jfrm.add(jcbOption3);
jfrm.setVisible(true);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox) ie.getItem();
if (ie.getStateChange() == ItemEvent.SELECTED)
System.out.println(cb.getText() + " selected.");
else
System.out.println(cb.getText() + " cleared.");
if (jcbOption1.isSelected())
System.out.println("1");
else if (jcbOption2.isSelected())
System.out.println("2");
else if (jcbOption3.isSelected())
System.out.println("3");
else
System.out.println("None");
}
public static void main(String args[]) {
new Main();
}
}
JCheckBox: setIcon(Icon defaultIcon)
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
// Set the unselected state icon
Icon unselIcon = new ImageIcon("nosel-icon.gif");
checkbox.setIcon(unselIcon);
// Set the selected state icon
Icon selIcon = new ImageIcon("sel-icon.gif");
checkbox.setSelectedIcon(selIcon);
}
}
JCheckBox: setMnemonic(int mnemonic)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] a) {
final String DESELECTED_LABEL = "Deselected";
final String SELECTED_LABEL = "Selected";
JFrame frame = new JFrame("Selecting CheckBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent
.getSource();
boolean selected = abstractButton.getModel().isSelected();
String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
abstractButton.setText(newLabel);
}
};
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent
.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
}
};
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);
}
}
};
checkBox.addActionListener(actionListener);
checkBox.addChangeListener(changeListener);
checkBox.addItemListener(itemListener);
checkBox.setMnemonic(KeyEvent.VK_S);
frame.add(checkBox, BorderLayout.NORTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
JCheckBox: setPressedIcon(Icon pressedIcon)
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
Icon pressedIcon = new ImageIcon("pres-icon.gif");
checkbox.setPressedIcon(pressedIcon);
}
}
JCheckBox: setRolloverIcon(Icon i)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox) ie.getItem();
int state = ie.getStateChange();
if (state == ItemEvent.SELECTED)
System.out.println(cb.getText() + " selected");
else
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}
JCheckBox: setRolloverSelectedIcon(Icon rolloverSelectedIcon)
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JCheckBox checkBox = new JCheckBox("Check me!");
checkBox.setSelected(true);
// Set default icon for checkbox
checkBox.setIcon(new ImageIcon("icon.png"));
// Set selected icon when checkbox state is selected
checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
// Set disabled icon for checkbox
checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
// Set disabled-selected icon for checkbox
checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
// Set checkbox icon when checkbox is pressed
checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
// Set icon when a mouse is over the checkbox
checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
// Set icon when a mouse is over a selected checkbox
checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));
getContentPane().add(checkBox);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
JCheckBox: setSelected(boolean b)
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
// Get the current state of the checkbox
boolean b = checkbox.isSelected();
// Set the state of the checkbox to off
checkbox.setSelected(false);
// Set the state of the checkbox to on
checkbox.setSelected(true);
}
}
JCheckBox: setSelectedIcon(Icon i)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox) ie.getItem();
int state = ie.getStateChange();
if (state == ItemEvent.SELECTED)
System.out.println(cb.getText() + " selected");
else
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}
JCheckBox: setText(String text) (HTML)
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
JCheckBox checkbox = new JCheckBox();
String label = "<html><table cellpadding=0><tr><td><img src=file:"
+ "icon.gif" + "/></td><td width=" + 3 + "><td>"
// Retrieve the current label text
+ checkbox.getText() + "</td></tr></table></html>";
// Add the icon
checkbox.setText(label);
}
}
JCheckBox: setToolTipText(String text)
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);
}
}
new JCheckBox(Action a)
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBox;
public class Main {
public static void main(String[] argv) throws Exception {
Action action = new AbstractAction("CheckBox Label") {
// called when the button is pressed
public void actionPerformed(ActionEvent evt) {
JCheckBox cb = (JCheckBox) evt.getSource();
// Determine status
boolean isSel = cb.isSelected();
if (isSel) {
// selected
} else {
// deselected
}
}
};
// Create the checkbox from the action
JCheckBox checkBox = new JCheckBox(action);
}
}
new JCheckBox(String text)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class Main {
public static void main(String args[]) {
String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Pizza Toppings");
panel.setBorder(border);
JCheckBox check = new JCheckBox("Anchovies");
panel.add(check);
check = new JCheckBox("Garlic");
panel.add(check);
check = new JCheckBox("Onions");
panel.add(check);
check = new JCheckBox("Pepperoni");
panel.add(check);
check = new JCheckBox("Spinach");
panel.add(check);
JButton button = new JButton("Submit");
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
new JCheckBox(String text, boolean selected)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main extends JFrame implements ActionListener {
public Main() {
JCheckBox checkbox = new JCheckBox("Show Title", true);
checkbox.addActionListener(this);
add(checkbox);
setSize(280, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
public void actionPerformed(ActionEvent e) {
if (this.getTitle() == "") {
this.setTitle("Checkbox example");
} else {
this.setTitle("");
}
}
}
new JCheckBox(String text, Icon normal)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox) ie.getItem();
int state = ie.getStateChange();
if (state == ItemEvent.SELECTED)
System.out.println(cb.getText() + " selected");
else
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}