Я хочу создать простую систему заказа еды, сейчас я создаю интерфейс формы заказа.Я использовал 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();
}
}
Нажмите здесь, чтобы просмотреть вывод