将Listener添加到jcheckBox时,JApplet不会初始化

时间:2015-10-22 05:28:19

标签: java listener japplet bluej jcheckbox

好吧,因为上次没办法了。我将在这里发布我的完整代码,我希望我能得到一些答复,说明为什么它不起作用。我没有收到任何编译错误,applet运行然后没有显示任何内容,底部显示“applet未初始化”。我正在使用blueJ。我为这篇文章的篇幅道歉,我无法弄清楚如何用更短的代码做出同样的错误。

我有一个包含多个类的JApplet程序。 RegPanel,WorkshopPanel,ConferenceGUI,ConferenceHandler和ConferenceClient。基本上RegPanel和WorkShop面板被添加到ConferenceGUI,它还创建并添加了几个小面板。 ConferenceClient类仅用于初始化类以运行applet。 ConferenceHandler用于处理JButtons,JTextArea,JCheckBox等的动作事件......通常这整个程序工作正常。除非我为JCheckBox添加一个监听器,否则它将停止工作。问题区域在ConferenceGUI类中,它被星星评论以清楚是什么导致了问题。

我已经被困在这个错误上大约一天了,我感到沮丧的压力很大。所以,为了达到目的,这是完整的代码: (请,我不需要代码的任何其他部分的提示,我只需要帮助解决该错误)。您可能希望跳过代码并只读取错误所在的ConferenceGUI类。如果你也可以向我解释为什么这不起作用,对我来说会非常有帮助。提前谢谢!

RegPanel类:

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class RegPanel extends JPanel
{
    protected JTextField regNameTextBox;

    protected JCheckBox keynoteCheckBox;

    protected final String[] REGTYPES = {"Please select a   type","Business","Student","Complimentary"};
    protected JPanel registrationPanel, keynotePanel;
    protected final double BUSINESSFEE = 895,STUDENTFEE = 495,COMPLIMENTARYFEE = 0;
    protected JComboBox regTypeComboBox;
    public RegPanel()
    {
        //Set the layout for the RegPanel to be 2 rows and 1 column.
        setLayout(new GridLayout(2, 1));

        //initiate the registration panel and add a border
        registrationPanel = new JPanel();
        registrationPanel.setLayout(new FlowLayout());
        registrationPanel.setBorder(BorderFactory.createTitledBorder("Registrant's Name & Type"));


        //initiate the comboBox and add the registration types
        regTypeComboBox = new JComboBox(REGTYPES);

        //Initiate the textfield with a size of 20
        regNameTextBox = new JTextField(20);

        //Add the registration name textbox and type combobox to the registration panel
        registrationPanel.add(regNameTextBox);
        registrationPanel.add(regTypeComboBox);

        //initiate the second panel for the checkbox
        keynotePanel = new JPanel();
        keynotePanel.setLayout(new FlowLayout());

        //initiate the checkbox and add it to the keynote panel
        JCheckBox keynoteCheckBox = new JCheckBox("Dinner and Keynote Speach");
        keynotePanel.add(keynoteCheckBox);

        //Add the two panels to the main panel
        add(registrationPanel);
        add(keynotePanel);

    }    
    public double getRegistrationCost()
    {
        double regFee = 0;
        String comboBoxAnswer = (String)regTypeComboBox.getSelectedItem();

        switch (comboBoxAnswer)
            {
                case "Business": regFee = BUSINESSFEE;
                   break;
                case "Student": regFee = STUDENTFEE;
                    break;

            }
        return regFee;    
    }        
    public double getKeynoteCost()
    {
        double keynoteCost = 0;
        if(keynoteCheckBox.isSelected())
        {
            keynoteCost = 30;
        }    
        return keynoteCost;
    }
    public String getRegType()
    {
        String regType = (String)regTypeComboBox.getSelectedItem();
        return regType;
    }    
}

WorkshopPanel类:

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class WorkshopPanel extends JPanel
{
    protected final double ITFEE = 295, DREAMFEE = 295, JAVAFEE = 395, ETHICSFEE = 395;
    protected final String[] WORKSHOPS = {"IT Trends in Manitoba","Creating a Dream Career","Advanced Java Programming","Ethics: The Challenge Continues"};
    protected JList workshopList;
    public WorkshopPanel()
    {
        setLayout(new FlowLayout());

        workshopList = new JList(WORKSHOPS);
        workshopList.setSelectionMode(2);


        BorderFactory.createTitledBorder("Workshops");

        add(workshopList);
    }    

    public double getWorkshopCost()
    {
        Object[] workshops = workshopList.getSelectedValues();
        double cost = 0;
        String workshopString;
        for (int i = 0; i < workshops.length; i++)
        {
          workshopString = (String)workshops[i];
          switch(workshopString)
          {
              case "IT Trends in Manitoba":
                cost += ITFEE;
                break;
              case "Creating a Dream Career":  
                cost += DREAMFEE;
                break;
              case "Advanced Java Programming":
                cost += JAVAFEE;
                break;
              case "Ethics: The Challenge Continues":
                cost += ETHICSFEE;
                break;

        }    
    }    
        return cost;

    }
    public Object[] getWorkshopList()
    {
        Object[] workshopListArray = workshopList.getSelectedValues();
        return workshopListArray;
    }    
}    

ConferenceGUI类(这包含错误):

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ConferenceGUI extends JPanel
{
   protected JPanel titlePanel, buttonPanel;
   protected RegPanel regPanel;
   protected WorkshopPanel workshopPanel;
   protected JLabel titleLabel;
   protected JButton calculateButton, clearButton;
   protected JTextArea resultArea;
   protected JScrollPane textScroll;


   public ConferenceGUI()
   {



       setLayout(new BorderLayout());

       titlePanel = new JPanel();

       titleLabel = new JLabel("Select Registration Options",JLabel.CENTER);

       Font titleFont = new Font("SansSerif", Font.BOLD, 18);

       titleLabel.setFont(titleFont);

       titlePanel.add(titleLabel);

       add(titlePanel, BorderLayout.NORTH);

       regPanel = new RegPanel();
       add(regPanel, BorderLayout.WEST);


       workshopPanel = new WorkshopPanel();
       add(workshopPanel, BorderLayout.EAST);

       buildButtonPanel();
       add(buttonPanel, BorderLayout.SOUTH);


       ConferenceHandler handler = new ConferenceHandler(this);

       regPanel.regTypeComboBox.addItemListener(handler);
       regPanel.regNameTextBox.addFocusListener(handler);
       //****************************************************************
       //The line below is what causes the error. Without it the code
       //Works, with it it doesn't and i get the aforementioned error.  


       //regPanel.keynoteCheckBox.addItemListener(handler);




   }    
   private void buildButtonPanel()
   {
       buttonPanel = new JPanel();
       buttonPanel.setLayout(new FlowLayout());

       calculateButton = new JButton("Calculate Charges");
       buttonPanel.add(calculateButton);

       clearButton = new JButton("Clear");
       buttonPanel.add(clearButton);


       resultArea = new JTextArea(5,30);
       textScroll = new JScrollPane(resultArea);

       buttonPanel.add(textScroll);

       ConferenceHandler handler = new ConferenceHandler(this);
       calculateButton.addActionListener(handler);
       clearButton.addActionListener(handler);

   }    
}

ConferenceHandler类(这个类未完成,直到我把这个错误弄清楚了):

import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class ConferenceHandler implements ActionListener, ItemListener, FocusListener
{
    protected ConferenceGUI gui;
    public ConferenceHandler(ConferenceGUI gui)
    {
        this.gui = gui;
    }    
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == gui.calculateButton)
        {
           String regType = gui.regPanel.getRegType();
           Object[] workshopList = gui.workshopPanel.getWorkshopList();

           String workshopString;




           if (regType == "Please select a type")
           {
               JOptionPane.showMessageDialog(null,"Please select a registration type","Type Error",JOptionPane.ERROR_MESSAGE );
           }    
           else
           {
               if(gui.regPanel.keynoteCheckBox.isSelected())

               {  
                   gui.resultArea.append("Keynote address will be attended/n");
               } 
               else
               {
                   gui.resultArea.append("Keynot address will not be attended/n");
               }    

           }
        }
        if (e.getSource() == gui.clearButton)
        {
            gui.resultArea.append("CLEAR");
        }    
    }
    private double getTotalCharges()
    {
        double charges = 0;



        return charges;
    }    
    public void itemStateChanged(ItemEvent e)
    {
    }
    public void focusLost(FocusEvent e)
    {
    }
    public void focusGained(FocusEvent e)
    {
    }    

}

ConferenceClient类:

import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ConferenceClient extends JApplet
{
  private final int WINDOW_HEIGHT = 700, WINDOW_WIDTH = 250; 
  private ConferenceGUI gui;

  private Container c;

  public ConferenceClient()
  {
      gui = new ConferenceGUI();

      c = getContentPane();

      c.setLayout(new BorderLayout());

      c.add(gui, BorderLayout.CENTER);

      setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
  }    
}

1 个答案:

答案 0 :(得分:1)

您正在隐藏keynoteCheckBox变量。首先在RegPanel中创建一个实例字段,但在构造函数中,您重新声明它...

public class RegPanel extends JPanel {

    protected JCheckBox keynoteCheckBox;
    //...

    public RegPanel() {
        //...
        //initiate the checkbox and add it to the keynote panel
        JCheckBox keynoteCheckBox = new JCheckBox("Dinner and Keynote Speach");
        keynotePanel.add(keynoteCheckBox);

这会将实例字段保留为null,这将导致NullPointerException

此外,regType == "Please select a type"不是如何在Java中比较String,您想要使用更像"Please select a type".equals(regType)

的内容