如何垂直设置JRadioButtons

时间:2013-12-06 19:09:16

标签: java swing jradiobutton

我在java应用程序中工作,它有很多组件,但我有一个JRadioButton的问题,它不是垂直的方式,还有其他任何方法使它垂直使用JPanel

我可以使用哪种最佳布局使这段代码比

更容易
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author isslam
 */
public class MyFrameMain extends JFrame{
    Equipment newq = new Equipment();
    private final JLabel ILabel;
    private final JLabel NLabel;
    private final JTextField IJTextField;
    private final JTextField NJTextField;
    private final JTextField SwTextField;
    private final JTextField HwTextField;
    private final JLabel JitemCounter;
    private final JTextArea resoulte;
    private final JButton addButton;
    private final JButton showButton;
    private final JButton copyButton;
    private final JButton exitButton;

public MyFrameMain(String title){
//setSize(500,500);

setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);

IJTextField = new JTextField();
NJTextField = new JTextField();
SwTextField = new JTextField();
HwTextField = new JTextField();
NLabel = new JLabel("ID: ");
ILabel = new JLabel("Name: ");
JitemCounter = new JLabel();

resoulte = new JTextArea();
resoulte.setEditable(false);

addButton = new  JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");


JRadioButton RButton1 = new JRadioButton("SW Version",false);

JRadioButton RButton2 = new JRadioButton("HW Type",false);

JRadioButton RButton3 = new JRadioButton("General",true);



JPanel panel = new JPanel();
panel.add(RButton1);
RButton1.setVerticalTextPosition(JPanel.BOTTOM);
panel.add(RButton2);
panel.add(RButton3);



 ButtonGroup BGroup = new ButtonGroup();
 BGroup.add(RButton1);
 BGroup.add(RButton2);
 BGroup.add(RButton3);


 Container pane = getContentPane();
 pane.setLayout(null);
 pane.add(panel);
 pane.add(ILabel);
 pane.add(NLabel);
 pane.add(addButton);
 pane.add(showButton);
 pane.add(copyButton);
 pane.add(exitButton);
 pane.add(IJTextField);
 pane.add(NJTextField);
 pane.add(SwTextField);
 pane.add(HwTextField);



 Insets insets = pane.getInsets();
 setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);

 Dimension size = ILabel.getPreferredSize();
 ILabel.setBounds(0 + insets.left, 30 + insets.top,size.width, size.height);

 size = NLabel.getPreferredSize();
 NLabel.setBounds(0 + insets.left, 5 + insets.top,size.width, size.height);

 size = addButton.getPreferredSize();
 addButton.setBounds(0 + insets.left, 409 + insets.top,70+size.width, size.height);

 size = showButton.getPreferredSize();
 showButton.setBounds(0 + insets.left, 435 + insets.top,65+size.width, size.height);

 size = copyButton.getPreferredSize();
 copyButton.setBounds(250 + insets.left, 409 + insets.top,91+size.width, size.height);

 size = exitButton.getPreferredSize();
 exitButton.setBounds(250 + insets.left, 435 + insets.top,171+size.width, size.height);

 size = IJTextField.getPreferredSize();
 IJTextField.setBounds(250 + insets.left, 30 + insets.top,230+size.width, size.height);

 size = NJTextField.getPreferredSize();
 NJTextField.setBounds(250 + insets.left, 5 + insets.top,230+size.width, size.height);

 size = panel.getPreferredSize();
 panel.setBounds(0 + insets.left, 90 + insets.top,230+size.width, size.height);

 size = SwTextField.getPreferredSize();
 SwTextField.setBounds(250 + insets.left, 55 + insets.top,230+size.width, size.height);

 size = HwTextField.getPreferredSize();
 HwTextField.setBounds(250 + insets.left, 80 + insets.top,230+size.width, size.height);

}


}

2 个答案:

答案 0 :(得分:4)

您可以使用Box设置

Box verticalBox = Box.createVerticalBox();
verticalBox.add(jRadioButtonA);
verticalBox.add(jRadioButtonB);
verticalBox.add(jRadioButtonC);

请参阅此可运行示例

import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class MyPanel extends JPanel {

    public MyPanel(){
        JRadioButton jrb1 = new JRadioButton("Button 1");
        JRadioButton jrb2 = new JRadioButton("Button 2");
        JRadioButton jrb3 = new JRadioButton("Button 3");
        Box box1 = Box.createVerticalBox();
        box1.add(jrb1);
        box1.add(jrb2);
        box1.add(jrb3);

        JRadioButton jrb4 = new JRadioButton("Button 4");
        JRadioButton jrb5 = new JRadioButton("Button 5");
        JRadioButton jrb6 = new JRadioButton("Button 6");
        Box box2 = Box.createVerticalBox();
        box2.add(jrb4);
        box2.add(jrb5);
        box2.add(jrb6);

        JRadioButton jrb7 = new JRadioButton("Button 7");
        JRadioButton jrb8 = new JRadioButton("Button 8");
        JRadioButton jrb9 = new JRadioButton("Button 9");
        Box box3 = Box.createVerticalBox();
        box3.add(jrb7);
        box3.add(jrb8);
        box3.add(jrb9);

        setLayout(new GridLayout(1, 3));
        add(box1);
        add(box2);
        add(box3);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MyPanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.setSize(300, 300);
                frame.setVisible(true);
            }
        });
    }
}

enter image description here

答案 1 :(得分:3)

非常简单,只需使用GridLayout:

JPanel panel = new JPanel(new GridLayout(3,1));
panel.add(RButton1);
panel.add(RButton2);
panel.add(RButton3);