有关ComboBox的更多信息

时间:2012-05-06 22:31:30

标签: java swing combobox awt

我有一个决赛即将到来,我的教授强调的一点是ComboBoxes。我已经完成了一项家庭作业,我也通过这里发布了一个问题,我理解它。他希望我们遵循这种模式:ComboBox code correct?

现在,我开始寻找自己的实践中的其他问题,我遇到了一些麻烦。这有点像我的家庭作业,但更多的是它。问题是:

  

大学有以下宿舍:

     
      
  • Allan Hall:每学期$ 1,500
  •   
  • Pike Hall:每学期$ 1,600
  •   
  • 父亲大厅:每学期1,200美元
  •   
  • 大学套房:$ 1,800
  •   
     

大学还提供以下膳食计划:

     
      
  • 每周7餐:每学期560美元
  •   
  • 每周14餐:1,095美元
  •   
  • 无限餐:每学期$ 1,800
  •   
     

使用2 ComboBox 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
    }
}}

1 个答案:

答案 0 :(得分:4)

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

很多琐碎的错误。

1)函数调用后的no()

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)观察拼写的情况......你将lowerCase与小写和小写混合

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 - 请先学习,然后再编码。