JRadioButton 未正确显示

我想创建一个简单的食品订购系统,现在我正在创建订单表单的界面。我使用 GridBagLayout 来创建表单布局,我的问题是当我想在同一行中分配 3 个单选按钮时,它只显示 1 个按钮......我希望有人可以帮助我......


这是我的java代码:


import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.*;


/**

 *

 * @author user

 */

public class ChickenChopOrderingSystem

{

    JFrame frame;

    JPanel mainPanel, p1, p2, p3, p4;

    JLabel lblTitle, lblName, lblPhoneNum, lblFlavour, lblChickenPart;

    JTextField txtName, txtPhoneNum;

    String flavour[] = {"Black Pepper Sauce", "Hainanese", "Grilled", "Lemon"};

    JComboBox box;

    ButtonGroup bg = new ButtonGroup();

    JRadioButton btnWhole, btnHalf, btnQuarter;

    JButton btnDone, btnExit;


    public ChickenChopOrderingSystem()

    {

        frame = new JFrame("Chicken Chop Ordering System");

        mainPanel = new JPanel();

        mainPanel.setPreferredSize(new Dimension(700,700));

        mainPanel.setBackground(Color.yellow);


        lblName = new JLabel("Customer's Name: ");

            txtName = new JTextField(20);


        lblPhoneNum = new JLabel("Phone Number: ");

            txtPhoneNum = new JTextField(11);


        lblChickenPart = new JLabel("Select Part of Chicken: ");

            btnWhole = new JRadioButton("Whole");

                btnWhole.addItemListener(new OperationListener());

            btnHalf = new JRadioButton("Half");

                btnHalf.addItemListener(new OperationListener());

            btnQuarter = new JRadioButton("Quarter");

                btnQuarter.addItemListener(new OperationListener());

            bg.add(btnWhole);

            bg.add(btnHalf);


    }


单击此处查看输出


紫衣仙女
浏览 79回答 1
1回答

MYYA

对于这样的事情:使用此代码:import java.awt.*;import javax.swing.*;public class ChickenChopOrderingSystem {    JFrame frame;    JPanel mainPanel, p1, p2, p3, p4;    JLabel lblTitle, lblName, lblPhoneNum, lblFlavour, lblChickenPart;    JTextField txtName, txtPhoneNum;    String flavour[] = {"Black Pepper Sauce", "Hainanese", "Grilled", "Lemon"};    JComboBox box;    ButtonGroup bg = new ButtonGroup();    JRadioButton btnWhole, btnHalf, btnQuarter;    JButton btnDone, btnExit;    public ChickenChopOrderingSystem() {        frame = new JFrame("Chicken Chop Ordering System");        mainPanel = new JPanel();        // GUESSWORK!         //mainPanel.setPreferredSize(new Dimension(700,700));        mainPanel.setBackground(Color.yellow);        lblName = new JLabel("Customer's Name: ");        txtName = new JTextField(20);        lblPhoneNum = new JLabel("Phone Number: ");        txtPhoneNum = new JTextField(11);        lblChickenPart = new JLabel("Select Part of Chicken: ");        btnWhole = new JRadioButton("Whole");        btnHalf = new JRadioButton("Half");        btnQuarter = new JRadioButton("Quarter");        bg.add(btnWhole);        bg.add(btnHalf);        bg.add(btnQuarter);        lblFlavour = new JLabel("Select a flavour: ");        box = new JComboBox(flavour);        btnDone = new JButton("Done");        btnExit = new JButton("Exit");        //GridBaglayout        mainPanel.setLayout(new GridBagLayout());        GridBagConstraints gbc = new GridBagConstraints();        int s = 20;        gbc.insets = new Insets(s,s,s,s);        //Label        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 0;        gbc.gridy = 0;        gbc.weightx = 0.5;        gbc.weighty = 0.5;        mainPanel.add(lblName, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 0;        gbc.gridy = 1;        gbc.weightx = 0.5;        mainPanel.add(lblPhoneNum, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 0;        gbc.gridy = 2;        gbc.weightx = 0.5;        mainPanel.add(lblChickenPart, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 0;        gbc.gridy = 3;        gbc.weightx = 0.5;        mainPanel.add(lblFlavour, gbc);        //TextField        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 1;        gbc.gridy = 0;        gbc.gridwidth = 3;        mainPanel.add(txtName, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 1;        gbc.gridy = 1;        gbc.gridwidth = 3;        mainPanel.add(txtPhoneNum, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 1;        gbc.gridy = 2;        gbc.gridwidth = 1;        gbc.weightx = 1d/6d;        mainPanel.add(btnWhole, gbc);        gbc.gridx = 2;        gbc.gridy = 2;        mainPanel.add(btnHalf, gbc);        gbc.gridx = 3;        gbc.gridy = 2;        mainPanel.add(btnQuarter, gbc);        gbc.fill = GridBagConstraints.HORIZONTAL;        gbc.gridx = 1;        gbc.gridy = 3;        gbc.gridwidth = 3;        mainPanel.add(box, gbc);        //frame setting        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setLayout(new GridBagLayout());        frame.add(mainPanel, new GridBagConstraints());        // GUESSWORK!         //frame.setSize(new Dimension(1000, 1000));        frame.pack();        frame.setVisible(true);    }    public static void main(String[] args) {        ChickenChopOrderingSystem run = new ChickenChopOrderingSystem();    }}原来的代码存在很多问题。(拖网记忆..)最后一个元素的约束没有设置回网格宽度 3,这让布局管理器感到困惑。他们ItemListener在移除组件时做了一些奇怪的事情,不要这样做。面板的首选尺寸和框架的尺寸都是猜测。用于pack()计算正确的尺寸。Inserts(向空白的初始约束添加标准。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java