JRadioButton 显示不正确
JRadioButton is not show properly
我想创建一个简单的食品订购系统,现在我正在创建订单界面。我使用 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);
bg.add(btnQuarter);
lblFlavour = new JLabel("Select a flavour: ");
box = new JComboBox(flavour);
btnDone = new JButton("Done");
btnExit = new JButton("Exit");
btnExit.addActionListener(new ButtonListener());
//GridBaglayout
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//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;
mainPanel.add(btnWhole, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
mainPanel.add(btnHalf, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
mainPanel.add(btnHalf, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 3;
mainPanel.add(box, gbc);
//frame setting
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(mainPanel, new GridBagConstraints());
frame.setSize(new Dimension(1000, 1000));
frame.setVisible(true);
}
public class OperationListener implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == btnWhole)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[2]);
}
} if (ie.getSource() == btnHalf)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[0]);
box.addItem(flavour[2]);
box.addItem(flavour[3]);
}
} if (ie.getSource() == btnQuarter)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[0]);
box.addItem(flavour[1]);
box.addItem(flavour[3]);
}
}
}
}
public class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnExit)
{
int s = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?",
"Exit", JOptionPane.YES_NO_OPTION);
if (s == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
}
public static void main(String[] args)
{
ChickenChopOrderingSystem run = new ChickenChopOrderingSystem();
}
}
Click here to view output
对于这样的事情:
使用此代码:
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()
来计算正确的大小。 (在白色 space 的初始约束中添加标准 Inserts
。)
我想创建一个简单的食品订购系统,现在我正在创建订单界面。我使用 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);
bg.add(btnQuarter);
lblFlavour = new JLabel("Select a flavour: ");
box = new JComboBox(flavour);
btnDone = new JButton("Done");
btnExit = new JButton("Exit");
btnExit.addActionListener(new ButtonListener());
//GridBaglayout
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//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;
mainPanel.add(btnWhole, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
mainPanel.add(btnHalf, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
mainPanel.add(btnHalf, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 3;
mainPanel.add(box, gbc);
//frame setting
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(mainPanel, new GridBagConstraints());
frame.setSize(new Dimension(1000, 1000));
frame.setVisible(true);
}
public class OperationListener implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == btnWhole)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[2]);
}
} if (ie.getSource() == btnHalf)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[0]);
box.addItem(flavour[2]);
box.addItem(flavour[3]);
}
} if (ie.getSource() == btnQuarter)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
box.removeAllItems();
box.addItem(flavour[0]);
box.addItem(flavour[1]);
box.addItem(flavour[3]);
}
}
}
}
public class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnExit)
{
int s = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?",
"Exit", JOptionPane.YES_NO_OPTION);
if (s == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
}
public static void main(String[] args)
{
ChickenChopOrderingSystem run = new ChickenChopOrderingSystem();
}
}
Click here to view output
对于这样的事情:
使用此代码:
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()
来计算正确的大小。 (在白色 space 的初始约束中添加标准Inserts
。)