我正在制作一个GUI程序,但得到一个奇怪的错误

时间:2011-02-10 17:35:46

标签: java compiler-construction constructor

我为我的作业制作了这个程序,我必须为另一个添加一个GUI选项。

两个选项的总和未出现在每学期总费用按钮中。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**

        The Main class creates the GUI for the Dorm and 
        Meal charges.
 */

public class Main extends JFrame
{
private JPanel dormPanel;
private JComboBox dormBox;
private JPanel mealPanel;
private JComboBox mealBox;
private JPanel totalChargesPanel;
private JPanel selectedMealPanel;
private JPanel buttonPanel;
private JButton calcButton;
private JLabel label1;
private JTextField totalCharges;

private String[] dorm = { "Allen Hall: $1,500 per semester", 
        "Pike Hall: $1,600 per semester", 
        "Farthing Hall: $1,200 per semester", 
"University Suites: $1,800 pe  r semester"};

private String[] meal = { "7 meals per week: $650 per semester", 
        "14 meals per week: $1,095 per semester", 
"Unlimited meals: $1,500 per semester"};

/**
    Constructor
 */
public Main()
{
    super("Dormitory and Meal Plan");

    // Specify an action for the close button.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create a BorderLayout manager.
    setLayout(new BorderLayout());

    // Create the dorm and meal panel.      
    buildDormPanel();
    buildMealPanel();
    buildSelectedTotalChargesPanel();
    buildButtonPanel();

    // Add the components to the content pane.   
    add(dormPanel, BorderLayout.WEST);
    add(mealPanel, BorderLayout.EAST);
    add(totalChargesPanel, BorderLayout.SOUTH);
    add(buttonPanel, BorderLayout.NORTH);

    // Pack the contents of the window and display it.    
    pack();
    setVisible(true);
}

// The buildDormPanel method builds the dorm panel. 
private void buildDormPanel()
{
    // Create the dorm panel.      
    dormPanel = new JPanel();
    dormBox = new JComboBox(dorm);

    // Register the action listener.      
    dormBox.addActionListener(new ComboBoxListener());

    // Add the dorm panel to the panel.
    dormPanel.add(dormBox);
}

// The buildMealPanel method builds the meal panel. 
private void buildMealPanel()
{
    // Create the meal panel.      
    mealPanel = new JPanel();
    mealBox = new JComboBox(meal);

    // Register the action listener.
    mealBox.addActionListener(new ComboBoxListener());

    // Add the meal panel to the panel.
    mealPanel.add(mealBox);
}

// The buttonPanel method builds the bottun panel.
private void buildButtonPanel()
{
    // Create a panel.
    buttonPanel = new JPanel();

    // Create a button.
    calcButton = new JButton("Calculate");

    // Register an action listener with the button.
    calcButton.addActionListener(new ButtonListener());

    // Add the button to the panel.
    buttonPanel.add(calcButton);
}

// The buildSelectedDormPanel builds the selected totalCharges panel.
private void buildSelectedTotalChargesPanel()
{
    // Create the totalChargesPanel for the label.      
    totalChargesPanel = new JPanel();
    label1 = new JLabel("Total charges per semester: ");

    // Create the totalCharges textfield.        
    totalCharges = new JTextField (25);
    totalCharges.setEditable(false);

    // Add the totalChargesPanel to the panel.      
    totalChargesPanel.add(label1);
    totalChargesPanel.add(totalCharges);
}

/** Private inner class that handles the event when the user 
     selects the dorm and meal boxes.
 */
private class ComboBoxListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        // Variables to hold the dorm, meal, and total charges.        
        String dorm = (String) dormBox.getSelectedItem();
        String meal = (String) mealBox.getSelectedItem();

        // Calculates the total.
        totalCharges.setText(meal + dorm);
    }
}

private class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) 
    {
        // Add code below
    }
}

public static void main(String[] args)
{
    new Main();
}
}

1 个答案:

答案 0 :(得分:2)

在您的动作侦听器中,我找到以下代码:

// Variables to hold the dorm, meal, and total charges.        
String dorm = (String) dormBox.getSelectedItem();
String meal = (String) mealBox.getSelectedItem();

// Calculates the total.
totalCharges.setText(meal + dorm);

您从每个组合框中读取字符串,然后尝试计算总数。不幸的是,术语meal+dorm并不意味着如果应用于字符串而是字符串连接则添加。

要解决此问题,您可以选择一个正确的数据结构,为结合框中的每个条目保存显示文本+值,然后计算actionlistener中的值的总和,然后再将该总数再次设置为文本格式。