yegane8
چهارشنبه 21 تیر 1391, 18:07 عصر
سلام باید کجای این برنامه رز تغییر بدهم تا radioButton ها در یک buttonGroup
add شوند
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class BoxLayOut {
public BoxLayOut(){
ButtonGroup buttonGroup = new ButtonGroup();
}
public static void addComponentsToPane(Container pane) {
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
addALabel("Question",pane);
addAButton("Button 1", pane);
addAButton("Button 2", pane);
addAButton("Button 3", pane);
addAButton("Button 4", pane);
}
private static void addAButton(String text, Container container) {
JRadioButton button = new JRadioButton(text);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(button);
button.setAlignmentX(Component.RIGHT_ALIGNMENT);
container.add(button);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void addALabel(String text, Container container) {
JLabel label = new JLabel(text);
label.setAlignmentX(Component.RIGHT_ALIGNMENT);
container.add(label);
}
}
add شوند
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class BoxLayOut {
public BoxLayOut(){
ButtonGroup buttonGroup = new ButtonGroup();
}
public static void addComponentsToPane(Container pane) {
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
addALabel("Question",pane);
addAButton("Button 1", pane);
addAButton("Button 2", pane);
addAButton("Button 3", pane);
addAButton("Button 4", pane);
}
private static void addAButton(String text, Container container) {
JRadioButton button = new JRadioButton(text);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(button);
button.setAlignmentX(Component.RIGHT_ALIGNMENT);
container.add(button);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void addALabel(String text, Container container) {
JLabel label = new JLabel(text);
label.setAlignmentX(Component.RIGHT_ALIGNMENT);
container.add(label);
}
}