java - 如何垂直设置 JRadioButtons

标签 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);

}


}

最佳答案

您可以使用 Box 设置 vetically

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

关于java - 如何垂直设置 JRadioButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20431719/

相关文章:

当存在多个类时,Java 中 ArrayList 出现问题并帮助修复代码

java - JLabel 和 JRadiobutton 之间差距巨大?

java - 生成随机长用户 ID

java - 开发应用程序来检测网页更改

java - 我有一个 JMenuItem 实例(比如说 TEMP)。我想知道添加了 TEMP 的 JMenu 的名称是什么。如何?

java - Swing 计时器问题 - 停止计时器

java - 通过单个连接进行 JDBC 选择查询的效率是否低于包含这些查询的过程?

java - Android AccountManager.getUserData() 返回 null

java - JLabel 中的图标

java - 在 Java 中重置单选按钮