我正在尝试在GUI顶部添加一个标签,上面写着“选择注册选项”,但是在弄乱setBounds之后,它似乎根本没有移动标签。这就是它的样子。 / p>
我希望显示“选择注册选项”的标签在程序的顶部中心。从更简单的意义上讲,我只想移动标签,同时保持其他所有东西都与现在相同的样子。我只想将标签向上移动并居中。这样,我的General和Student单选按钮就可以像以前一样恢复原状。
但是,我尝试的所有内容都无法正确编译,或者在编译时无法正常工作。我可以在这里得到帮助吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SignupGUI extends JFrame{
private final int DINNER_PRICE = 30;
private final int[] FEES = {295, 295, 395, 395};
private final String[] WORKSHOPS = {"Introduction to E-Commerce","The Future of the Web",
"Advanced Java Programming","Network Security"};
private final String[] REGISTRATION_TYPES = {"General", "Student"};
private final int REGISTRATION1 = 895, REGISTRATION2 = 495;
private JCheckBox dinnerCheck;
private JList workshopList;
//private JComboBox registrationBox;
private JRadioButton studentButton;
private JRadioButton generalButton;
private ButtonGroup group;
private JButton calcButton;
private JButton clearButton;
private JButton exitButton;
private JLabel label;
public SignupGUI() {
setTitle("Conference Registration System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
pack();
setVisible(true);
}
private void buildPanel()
{
JPanel smallCombo = new JPanel();
JPanel subPanel = new JPanel();
Font f1 = new Font("Serif", Font.BOLD, 24);
label = new JLabel("Select Registration Options");
label.setFont(f1);
label.setBounds(50, 50, 100, 100);
smallCombo.add(label, BorderLayout.NORTH);
workshopList = new JList(WORKSHOPS);
//registrationBox = new JComboBox(REGISTRATION_TYPES);
dinnerCheck = new JCheckBox("Dinner and Keynote Speech");
calcButton = new JButton("Calculate price");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
//registrationBox.setBorder(BorderFactory.createTitledBorder("Registration type"));
//registrationBox.setPreferredSize(new Dimension(160,45));
studentButton = new JRadioButton("Student");
generalButton = new JRadioButton("General", true);
workshopList.setBorder(BorderFactory.createTitledBorder("Workshops"));
//smallCombo.add(registrationBox);
group = new ButtonGroup();
group.add(studentButton);
group.add(generalButton);
add(smallCombo, BorderLayout.NORTH);
add(dinnerCheck, BorderLayout.EAST);
add(workshopList, BorderLayout.CENTER);
/*add(calcButton, BorderLayout.SOUTH);
add(clearButton, BorderLayout.SOUTH);*/
subPanel.add(calcButton);
subPanel.add(clearButton);
subPanel.add(exitButton);
add(subPanel, BorderLayout.SOUTH);
//studentButton.addActionListener(new CalcListen());
//generalButton.addActionListener(new CalcListen());
calcButton.addActionListener(new CalcListen());
clearButton.addActionListener(new ClearListen());
exitButton.addActionListener(new ExitListen());
smallCombo.add(generalButton);
smallCombo.add(studentButton);
}
private class CalcListen implements ActionListener{
public void actionPerformed(ActionEvent e)
{
int totalPrice = 0;
if(dinnerCheck.isSelected())
{
totalPrice+=DINNER_PRICE;
}
int workshops = getWorkshops();
totalPrice+=workshops;
int regType = getRegistrationType();
totalPrice+=regType;
JOptionPane.showMessageDialog(null, "$" + totalPrice);
}
}
private class ClearListen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
workshopList.clearSelection();
group.clearSelection();
dinnerCheck.setSelected(false);
}
}
private class ExitListen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
private int getWorkshops(){
int workShopCost = 0;
int[] selected = workshopList.getSelectedIndices();
int index = workshopList.getSelectedIndex();
for(int s:selected)
workShopCost+=FEES[s];
if (index == -1)
{
JOptionPane.showMessageDialog(null, "Please select type of workshop.");
index = workshopList.getSelectedIndex();
}
return workShopCost;
}
private int getRegistrationType()
{
int registrationCost = 0;
//int selected = group.isSelected();
//registrationCost = REGISTRATION[selected];
if(generalButton.isSelected())
{
registrationCost = REGISTRATION1;
}
else if(studentButton.isSelected())
{
registrationCost = REGISTRATION2;
}
else
{
JOptionPane.showMessageDialog(null, "Please select type of registration.");
}
return registrationCost;
}
}