java之如何垂直设置 JRadioButtons

linjiqin 阅读:86 2024-05-29 10:23:45 评论:0

我正在使用具有许多组件的 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); 
            } 
        }); 
    } 
} 


标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号