1

我有一个期末考试,我的教授强调的一点是 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
    }
}}
4

1 回答 1

4

OP 的代码不是有效的 Java,如果不首先解释如何在 OOP 中编码,就无法使其工作。

很多琐碎的错误。

1) 函数调用后没有()

buildSelectedDormPanel;->buildSelectedDormPanel();

2) 类/c-tors 的错误方法

public void ComboBoxWindow1() {

应该是,例如

class ComboBoxWindow1 extends JFrame { 
  public void ComboBoxWindow1() {

等等,甚至使用匿名类覆盖,即

JFrame f = new JFrame() {

等等

3) ComboBox 是通用类型,所以请不要使用原始版本 - 使用 ComboBox<String> 代替

4)在需要的地方添加@Override注解到你的代码中

5)注意你的拼写大小写......你将小写字母与小写字母和小写字母混合

6)修复你的括号(不仅是圆形的,而且在你的代码中也错位了)

7)您将“”误认为是字符串,而将“”误认为是字符

8) 如果您想评论您的代码(即代码中的错误,请使用 COMMENT /* */ 或 //,而不是在尝试正确格式化您的代码之前我必须删除的一些星号,

9)有太多的空白和错误,一些未使用的变量,一些字段(全局变量?无法猜测意图)没有任何明确的类附件 - 总而言之,修复代码本身是不可能的 -

10)(接着是拖钓)你似乎意识到你学得还不够,学期快结束了,所以你试着做功课,产生了一些损坏/难以辨认的代码,惊慌失措,无法修复它,然后你意识到有 StackOverflow。坏小狗,没有骨头。

11) 修复最明显错误后的代码【注意由于main()为空,代码无效】

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<String> 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" };

  protected class ComboBoxWindow1 extends JFrame {
    protected ComboBoxWindow1() {
      super( "Dorm plan" );
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      setLayout( new BorderLayout() );
      buildDormPanel();
      buildSelectedDormPanel();
      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 {
    @Override
    public void actionPerformed( ActionEvent e ) {
      String selection = (String) DormBox.getSelectedItem();
      selectedDorm.setText( selection );
    }
  }
  private JPanel mealPanel;
  private JPanel selectedMealPanel;
  private JComboBox<String> 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"
  };

  protected class ComboBoxWindow2 extends JFrame {
    protected ComboBoxWindow2() {
      super( "Meal plan" );
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      setLayout( new BorderLayout() );

      buildMealPanel();
      buildSelectedMealPanel();
      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" );
    calcbutton.addActionListener( new ActionListener() {
      @Override
      public void actionPerformed( ActionEvent e ) {
        throw new UnsupportedOperationException( "Not supported yet." );
      }
    } );
  }

  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 {
    @Override
    public void actionPerformed( ActionEvent e ) {
      String selection = (String) DormBox.getSelectedItem();
      selectedDorm.setText( selection );
      JOptionPane.showMessageDialog( null, "Total is" + dorm + meal );
    }
  }

  static public void main( String[] args ) {
    // do your thing
  }
}

TL;DR - 请先学习,然后再编码。

于 2012-05-06T22:53:08.713 回答