gpt4 book ai didi

java - 如何在同一行显示两个单选按钮?

转载 作者:行者123 更新时间:2023-11-30 06:14:24 24 4
gpt4 key购买 nike

我是Java初学者。我被分配的任务是创建一个 Java 窗口,任何用户都可以在其中输入他们的个人数据(姓名、性别等),然后最后单击提交按钮。

为了创建这样的窗口,我使用 GridLayout 添加了一个 BorderLayout 和四个面板。我的程序的主要问题是我无法将两个单选按钮放在同一行中,这让我陷入困境。我尝试在 JPanel 中使用 GridLayoutTableLayout 来解决问题,但没有成功。

这是它的外观截图:

如图所示,“F”单选按钮应放置在“M”单选按钮旁边,而不是下方。

这是我的代码:

import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Finestra extends JFrame

{

public Finestra ()

{
super("Dati Utente");
this.setBounds(300,50,400,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());

Panel panel1 = new Panel (new GridLayout(1,1));
Panel panel2 = new Panel (new GridLayout(1,1));
Panel panel3 = new Panel (new GridLayout(5,1));
Panel panel4 = new Panel (new GridLayout(5,1));

panel1.setPreferredSize(new Dimension(100,50));
panel2.setPreferredSize(new Dimension(100,50));
panel3.setPreferredSize(new Dimension (100,200));
panel4.setPreferredSize(new Dimension (260,200));

c.add(panel1, BorderLayout.NORTH);
c.add(panel2, BorderLayout.SOUTH);
c.add(panel3, BorderLayout.WEST);
c.add(panel4, BorderLayout.EAST);

JLabel label1 = new JLabel ("Personal Data");
label1.setHorizontalAlignment(SwingConstants.CENTER);
label1.setFont(new Font ("Open Sans",Font.BOLD,20));
panel1.add(label1);

JLabel label2 = new JLabel ("Name");
label2.setFont(new Font ("Open Sans", Font.PLAIN,14));
label2.setBorder(new EmptyBorder(0,15,0,0));

JLabel label3 = new JLabel ("Last Name");
label3.setFont(new Font ("Open Sans", Font.PLAIN,14));
label3.setBorder(new EmptyBorder(0,15,0,0));

JLabel label4 = new JLabel ("Gender");
label4.setFont(new Font ("Open Sans", Font.PLAIN,14));
label4.setBorder(new EmptyBorder(0,15,0,0));

JLabel label5 = new JLabel ("Hobby");
label5.setFont(new Font ("Open Sans", Font.PLAIN,14));
label5.setBorder(new EmptyBorder(0,15,0,0));

JLabel label6 = new JLabel ("Nationality");
label6.setFont(new Font ("Open Sans", Font.PLAIN,14));
label6.setBorder(new EmptyBorder(0,15,0,0));

panel3.add(label2);
panel3.add(label3);
panel3.add(label4);
panel3.add(label5);
panel3.add(label6);

JTextField f1 = new JTextField();
JTextField f2 = new JTextField();

f1.setPreferredSize(new Dimension(180,30));
f2.setPreferredSize(new Dimension(180,30));

JPanel fp1 = new JPanel (), fp2 = new JPanel ();

fp1.setBorder(new EmptyBorder(15,0,0,0));
fp2.setBorder(new EmptyBorder(15,0,0,0));

fp1.add(f1);
fp2.add(f2);

panel4.add(fp1);
panel4.add(fp2);


JRadioButton button1 = new JRadioButton("M"),button2 = new JRadioButton(("F"));


button1.setHorizontalAlignment(SwingConstants.CENTER); button2.setHorizontalAlignment(SwingConstants.CENTER);

ButtonGroup bgroup1 = new ButtonGroup();

bgroup1.add(button1); bgroup1.add(button2);

panel4.add(button1); panel4.add(button2);


setVisible(true);
}

如何在同一行显示两个单选按钮?

最佳答案

不要将 button1button2 添加到 panel4,而是将它们添加到具有水平布局的新 JPanel ,并将新的 JPanel 添加到 panel4

JRadioButton button1 = new JRadioButton("M"),button2 = new JRadioButton(("F"));
button1.setHorizontalAlignment(SwingConstants.CENTER); button2.setHorizontalAlignment(SwingConstants.CENTER);
ButtonGroup bgroup1 = new ButtonGroup();
bgroup1.add(button1); bgroup1.add(button2);
JPanel radiobuttonpanel = new JPanel();
BoxLayout radiobuttonpanellayout = new BoxLayout(radiobuttonpanel, BoxLayout.X_AXIS);
radiobuttonpanel.setLayout(radiobuttonpanellayout);
radiobuttonpanel.add(button1); radiobuttonpanel.add(button2);
panel4.add(radiobuttonpanel);

关于java - 如何在同一行显示两个单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49542757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com