Java 如何垂直设置JRadioButtons
我在有很多组件的java应用程序中工作,但我对JRadioButton有一个问题,它不是垂直的,有没有其他方法可以使它垂直而不使用JPanel 我能用什么样的布局使代码更容易呢Java 如何垂直设置JRadioButtons,java,swing,jradiobutton,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{ Equipme
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 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);
}
});
}
}
非常简单,只需使用GridLayout:
JPanel panel = new JPanel(new GridLayout(3,1));
panel.add(RButton1);
panel.add(RButton2);
panel.add(RButton3);
使用
setBounds
不是一个好主意,让版面管理员来做这项工作吧!我可以在一个类中使用多个布局吗?例如,对按钮使用边框布局,对jradiobuttonies使用greaid布局。实际上,我将布局设置为一个容器,如包含按钮等组件的jpanel。看看这里,我可以设置文本框的布局,例如使用面板,将它们添加到面板,然后设置布局否,将布局设置到面板,例如BoxLayout,然后添加按钮。。。按钮将自动添加到面板上,一个接一个现在,我们正在讨论:)+1