【问题标题】:Making radio buttons appear next to each other in a GridLayout使单选按钮在 GridLayout 中彼此相邻显示
【发布时间】:2020-04-01 21:32:55
【问题描述】:

我在我的 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 和右侧栏中的三个单选按钮 all。请帮忙。

【问题讨论】:

标签: java swing user-interface


【解决方案1】:

按照camickr 的建议,用JPanel 包装3 个单选按钮并将其添加到“右”:

    JPanel rButtons = new JPanel(); //uses FlowLayout by default
    rButtons.add(radioButton);
    rButtons.add(radioButton_1);
    rButtons.add(radioButton_2);
    right.add(rButtons);

不要设置单选按钮(或任何其他组件边界。让布局管理器来做。 也不要设置框架(或任何其他组件)大小(frame.setSize(550,600);)。而是在设置 JFrame 可见之前添加 frame.pack() 并让布局管理器完成其工作。

解决方案mre

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class GUI {

    public GUI() {

        JFrame frame = new JFrame(); // making the JFrame
        frame.setTitle("Donate Today!"); // sets JFrame title

        // Make the content pane with a set layout
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout(5,5)); // makes a layout for aesthetics/organization

        // make right-hand panel where the donation form will appear
        JPanel right = new JPanel(new GridLayout(3,2,5,5));
        right.setPreferredSize(new Dimension(400,160));

        JLabel lblamount = new JLabel("Donation");
        JLabel lblgender = new JLabel("Gender");
        JTextField amount = new JTextField();

        JRadioButton radioButton = new JRadioButton("Female");
        JRadioButton radioButton_1 = new JRadioButton("Male");
        JRadioButton radioButton_2 = new JRadioButton("Other");

        ButtonGroup b1 = new ButtonGroup();
        b1.add(radioButton);
        b1.add(radioButton_1);
        b1.add(radioButton_2);

        right.add(lblamount);
        right.add(amount);
        right.add(lblgender);
        JPanel rButtons = new JPanel();
        rButtons.add(radioButton);
        rButtons.add(radioButton_1);
        rButtons.add(radioButton_2);
        right.add(rButtons);

        // makes the clear button
        JButton btnClear = new JButton("Clear");
        btnClear.setPreferredSize(new Dimension(10,10));

        // makes the submit button
        JButton btnSubmit = new JButton("Submit");
        btnSubmit.setPreferredSize(new Dimension(10,10));
        // adds both buttons to the form JPanel
        right.add(btnClear);
        right.add(btnSubmit);

        // make left-hand panel where the button menu selections will appear
        JPanel left = new JPanel(new GridLayout(3,1,5,5)); // specifies a grid layout for theh buttons
        left.setPreferredSize(new Dimension(195,225));

        String arr[] = new String[] {"Budgeting", "About Us", "View Graph"};
        for (int i=0; i<arr.length; i++) { // loops through the above array
            left.add(new JButton(arr[i])); //add button to lower pane
        }

        // adds everything to the JFrame
        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.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new GUI();
    }
}

【讨论】:

    【解决方案2】:

    我有三个单选按钮(在同一个按钮组中)只需要添加到 GridLayout 中的一个单元格中。

    您创建一个JPanel 并将三个JRadioButton 中的每一个添加到面板中。然后使用GridLayout 将面板添加到面板中的单元格。

    这是实现更复杂布局的方法。您可以使用不同的布局管理器嵌套面板以实现所需的布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2016-02-23
      • 2021-02-20
      相关资源
      最近更新 更多