java - 使单选按钮在 GridLayout 中彼此相邻显示
问题描述
我在我的 Java GUI 中为 JPanel 使用了 GridLayout。我有三个单选按钮(在同一个按钮组中),只需要添加到GridLayout 中的一个单元格中。我确实尝试只将按钮组添加到 GridLayout,但我的编译器不喜欢这样。
'''
public GUI() {
frame = new JFrame(); // making the JFrame
frame.setSize(550,600);
frame.setTitle("Donate Today!"); // sets JFrame title
// Make the content pane with a set layout
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout()); // makes a layout for aesthetics/organization
// make right-hand panel where the donation form will appear
right = new JPanel(new GridLayout(9,2));
right.setPreferredSize(new Dimension(330,500));
// initializing text fields to create the form
JLabel lblfname = new JLabel("First Name");
lblfname.setPreferredSize(new Dimension(330,20));
JLabel lbllname = new JLabel("Last Name");
lbllname.setPreferredSize(new Dimension(200,20));
JLabel lblphone = new JLabel("Phone #");
lblphone.setPreferredSize(new Dimension(200,20));
JLabel lblemail = new JLabel("Email");
lblemail.setPreferredSize(new Dimension(200,20));
JLabel lbladdr = new JLabel("Home Address");
lbladdr.setPreferredSize(new Dimension(200,20));
JLabel lblamount = new JLabel("Donation");
JLabel lblgender = new JLabel("Gender");
fName = new JTextField();
lName = new JTextField();
phone = new JTextField();
email = new JTextField();
addr = new JTextField();
addr.setText("City, State, Zip");
amount = new JTextField();
radioButton = new JRadioButton();
radioButton.setText("Female");
radioButton.setActionCommand("female");
radioButton.setBounds(105,308,186,25);
radioButton_1 = new JRadioButton();
radioButton_1.setText("Male");
radioButton_1.setActionCommand("male");
radioButton_1.setBounds(165,308,186,25);
radioButton_2 = new JRadioButton();
radioButton_2.setBounds(155,308,186,25);
radioButton_2.setText("Other");
radioButton_2.setActionCommand("other");
b1 = new ButtonGroup();
b1.add(radioButton);
b1.add(radioButton_1);
b1.add(radioButton_2);
right.add(lblfname);
right.add(fName);
right.add(lbllname);
right.add(lName);
right.add(lblphone);
right.add(phone);
right.add(lblemail);
right.add(email);
right.add(lbladdr);
right.add(addr);
right.add(lblamount);
right.add(amount);
right.add(lblgender);
right.add(radioButton);
right.add(radioButton_1);
right.add(radioButton_2);
// make top panel where output from the menu selections will appear
topP = new JPanel(new BorderLayout());
topP.setSize(new Dimension(500,150));
// make default text message to be displayed in top panel
output = new JTextArea("Output printed here...", 20, 20);
// styles the text in the textarea
output.setForeground(Color.BLUE);
output.setFont(new Font("Times New Roman", Font.BOLD, 20));
topP.add(output, BorderLayout.NORTH); // add default text to the top panel
right.add(output);
// here's the scrollbar guys
top = new JScrollPane(output); // applies to the textarea
top.setPreferredSize(new Dimension(500,145));
top.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
topP.add(top, BorderLayout.NORTH); // adds scrollbar to the same panel that contains the textarea
// makes the clear button
btnClear = new JButton("Clear");
btnClear.setPreferredSize(new Dimension(10,10));
btnClear.addActionListener(this);
// makes the submit button
btnSubmit = new JButton("Submit");
btnSubmit.setPreferredSize(new Dimension(10,10));
btnSubmit.addActionListener(this);
// adds both buttons to the form JPanel
right.add(btnClear);
right.add(btnSubmit);
// make left-hand panel where the button menu selections will appear
left = new JPanel(new GridLayout(6,1,5,5)); // specifies a grid layout for theh buttons
left.setPreferredSize(new Dimension(195,450));
// populates array with buttons
btn = new JButton[6]; // new JButton array
String arr[] = new String[] {"List", "Why Donate?", "Visit Our Website", "Budgeting", "About Us", "View Graph"};
for (int i=0; i<btn.length; i++) { // loops through the above array
btn[i] = new JButton(arr[i]);
btn[i].addActionListener(this); // when we click on a button, something happens
left.add(btn[i]); //add button to lower pane
}
// adds everything to the JFrame
contentPane.add(topP, BorderLayout.NORTH);
contentPane.add(left, BorderLayout.WEST); //adding panel 1 to the top of the frame
contentPane.add(right, BorderLayout.EAST); //adding panel 2 to the center of the frame
frame.setVisible(true);
}
我需要左侧栏中的“性别”JLabel 和右侧栏中的三个单选按钮。请帮忙。
解决方案
我有三个单选按钮(在同一个按钮组中),只需要添加到 GridLayout 中的一个单元格中。
您创建一个JPanel
并将三个JRadioButton
中的每一个添加到面板中。然后使用 . 将面板添加到面板中的单元格GridLayout
。
这就是您实现更复杂布局的方式。您可以使用不同的布局管理器嵌套面板以实现所需的布局。
推荐阅读
- angular - 从一无所有到 emailjs 到 emailjs-com
- ios - 如果节点具有 currentuser.uid 对象并显示在我的 UITableViewCell 中,则从 Firebase 中的节点检索对象
- javascript - 我写了一个简单的 Javascript 闭包代码,但它没有运行。我哪里错了?
- android - 当用户从按钮上移开手指时如何处理一个动作?
- java - 选中复选框时如何将项目移动到最后一个位置?(RecyclerView, ViewHolder)
- scala - 使用 feeder 使 gatling 为每个请求生成随机数据
- r - 如何在 R/plotROC 中重新调整 ROC 曲线?
- haskell - 当 ghc 建议将构造函数添加到导入列表时,我的数据构造函数导入/使用有什么问题?
- javascript - Base64 插件不适用于 iOS 平台上的 pdf 文件
- laravel - 具有默认选定值的 Laravel 集体选择列表