我有一个期末考试,我的教授强调的一点是 ComboBoxes。我已经完成了一项家庭作业,我也通过here将其作为问题发布,我理解。他希望我们遵循这种模式:ComboBox 代码正确吗?.
现在,我开始为自己的练习寻找其他问题,但遇到了一些麻烦。这有点像我的家庭作业,但有更多内容。问题:
一所大学有以下宿舍:
- 艾伦大厅:每学期$ 1,500
- 派克大厅:每学期 1,600 美元
- 育儿堂:每学期 1,200 美元
- 大学套房:1,800 美元
该大学还提供以下膳食计划:
- 每周 7 餐:每学期 560 美元
- 每周 14 餐:1,095 美元
- 无限制用餐:每学期 1,800 美元
ComboBox
使用 2 个es创建一个应用程序。一个应该保存宿舍的名称,另一个应该保存膳食计划。用户应选择宿舍和膳食计划,应用程序应显示总费用。
我开始编写一些代码,但我不知道如何将两个组合框一个接一个地放置,然后如何创建一个单独的“计算”按钮ActionListener
。
这是代码;我知道这是一团糟。谁能告诉我如何正确写这个?我只想做好准备。请多多包涵,感谢您的耐心和帮助。我的代码(我将 4****
放在编译器出现红色的部分代码上):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class DormAndMealPlanCalc extends JFrame
{
private JPanel dormPanel;
private JPanel selectedDormPanel;
private JComboBox DormBox;
private JLabel label;
private JTextField selectedDorm;
private String[] dorm = {"Allen Hall: $1,500 per semester","Pike Hall: $1,100 per" +
"semester","Fathering Hall: 1,200 per semester","Universty Suites: $1,800"};
public void ComboBoxWindow1()
{
**super("Dorm plan");** wrong placement?
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildDormPanel();
**buildSelectedDormPanel;** Variable error
add(dormPanel,BorderLayout.CENTER);
add(selectedDormPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildDormPanel()
{
dormPanel = new JPanel();
DormBox = new JComboBox(dorm);
DormBox.addActionListener(new ComboBoxListener());
dormPanel.add(DormBox);
}
private void buildSelectedDormPanel()
{
selectedDormPanel = new JPanel();
label = new JLabel("You selected: ");
selectedDorm = new JTextField(10);
selectedDorm.setEditable(false);
selectedDormPanel.add(label);
selectedDormPanel.add(selectedDorm);
}
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) DormBox.getSelectedItem();
selectedDorm.setText(selection);
}
}
public void main(String[] args)
{
**new ComboBoxWindow2();**
}
}
private JPanel mealPanel;
private JPanel selectedMealPanel;
private JComboBox MealBox;
private JLabel label1;
private JTextField selectedMeal;
private JButton calcbutton;
private String[] Meal = {"7 meals per week: $560 per semester","14 meals per week:" +
"$1,095 per semester","Unlimited meals: $1,500 per semester"};
public void ComboBoxWindow()
{
**super("Meal plan");**
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildMealPanel();
**BuildselectedMealPanel;** variable problem
add(mealPanel,BorderLayout.CENTER);
add(selectedMealPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildMealPanel()
{
mealPanel = new JPanel();
MealBox = new JComboBox(meal);
MealBox.addActionListener(new ComboBoxListener());
mealPanel.add(MealBox);
**calcbutton = new JButton("Calculate");** syntax/token error
}
calacbutton.addActionListener(new calcbuttonListener);
private void buildSelectedMealPanel()
{
selectedMealPanel = new JPanel();
label = new JLabel("You selected: ");
selectedMeal = new JTextField(10);
selectedMeal.setEditable(false);
selectedMealPanel.add(label);
selectedMealPanel.add(selectedDorm);
}
private class ComboBoxListener1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str;
String selection = (String) DormBox.getSelectedItem();
selectedDorm.setText(selection);
**JOptionPane.showMessageDialog('Total is' + dorm + meal);** argument error
}
}}