Java by API/javax.swing/JRadioButton — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:19, 31 мая 2010
Содержание
JRadioButton: getModel()
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JRadioButton;
public class Main {
public static void main(String[] argv) throws Exception {
JRadioButton radioButton = new JRadioButton();
ButtonGroup group = new ButtonGroup();
ButtonModel model = radioButton.getModel();
group.setSelected(model, true);
}
}
JRadioButton: 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);
}
}
JRadioButton: setMnemonic(int mnemonic)
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rb1 = new JRadioButton("Male");
rb1.setMnemonic(KeyEvent.VK_M);
rb1.setActionCommand("Male");
rb1.setSelected(true);
JRadioButton rb2 = new JRadioButton("Female");
rb2.setMnemonic(KeyEvent.VK_F);
rb2.setActionCommand("Female");
rb2.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
JPanel jp = new JPanel();
jp.add(rb1);
jp.add(rb2);
getContentPane().add(jp);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MainClass("RadioButton Demo");
}
}
JRadioButton: setSelected(boolean b)
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MainClass extends JFrame {
MainClass(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rb1 = new JRadioButton("Male");
rb1.setMnemonic(KeyEvent.VK_M);
rb1.setActionCommand("Male");
rb1.setSelected(true);
JRadioButton rb2 = new JRadioButton("Female");
rb2.setMnemonic(KeyEvent.VK_F);
rb2.setActionCommand("Female");
rb2.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
JPanel jp = new JPanel();
jp.add(rb1);
jp.add(rb2);
getContentPane().add(jp);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MainClass("RadioButton Demo");
}
}
new JRadioButton(String text, boolean selected)
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MainClass extends JPanel {
public MainClass() {
JRadioButton radMarriedYes = new JRadioButton("Yes?", true);
JRadioButton radMarriedNo = new JRadioButton("No?", false);
JRadioButton radGolfYes = new JRadioButton("Yes?", false);
JRadioButton radGolfNo = new JRadioButton("No?", true);
ButtonGroup radioGroup1 = new ButtonGroup();
ButtonGroup radioGroup2 = new ButtonGroup();
setLayout(null);
add(radMarriedYes);
add(radMarriedNo);
add(radGolfYes);
add(radGolfNo);
radioGroup1.add(radMarriedYes);
radioGroup1.add(radMarriedNo);
radioGroup2.add(radGolfYes);
radioGroup2.add(radGolfNo);
radMarriedYes.setBounds(30, 50, 50, 20);
radMarriedNo.setBounds(30, 80, 50, 20);
radGolfYes.setBounds(150, 50, 50, 20);
radGolfNo.setBounds(150, 80, 50, 20);
}
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);
}
}