-2

好的,我收到此错误 awt eventqueue 0 nullpointerexception 错误。当我尝试删除 JPanel 时。

令我困惑的是,当我点击价格按钮时,它会删除 JPanel 的时间。它工作得很好,但是当我点击时间按钮时,它不会删除价格面板,而是我得到一个 awt eventqueue 错误。下面的第一个代码显示了我的主类、时间类和价格类。抱歉,我重新发布了我想发布我的代码。下面是代码示例

   import javax.swing.JOptionPane;
   import javax.swing.border.TitledBorder;
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

   public class events extends JFrame {
   // variables for JPanel


  private JButton timeButton;
  private JButton priceButton;

       setLayout(new BorderLayout()); 

  buttonPanel = new JPanel();
  buttonPanel.setBackground(Color.lightGray);
  buttonPanel.setBorder( new TitledBorder("Main Menu"));


         timeButton = new JButton("Time"); 
  buttonPanel.add(timeButton);

  priceButton = new JButton("Price");
  buttonPanel.add(priceButton);




           buttontime clickTime = new buttontime(); // event created when time button is clicked
  timeButton.addActionListener(clickTime);


         //ActionListener created for price
   buttonprice ClickPrice = new buttonprice(); // event created when price button is clicked
   priceButton.addActionListener(ClickPrice);


         public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox

public void actionPerformed(ActionEvent clickTime) {
    Price priceObject = new Price();
    priceObject.getPricepanel();
    remove(priceObject.getPricepanel());
    priceObject.getPricepanel().revalidate();

    add(timeObject.getTimePanel(), BorderLayout.EAST);
    timeObject.getTimePanel().revalidate();

   }
}

      //This one gives me 0 errors.
           public class buttonprice implements ActionListener { //creating actionlistener for clicking on      timebutton to bring up a combobox

   public void actionPerformed(ActionEvent ClickPrice) {
    Price priceObject = new Price();
    priceObject.SelectPrice();
    remove(timeObject.getTimePanel());
    timeObject.getTimePanel().revalidate();

    add(priceObject.getPricepanel(), BorderLayout.EAST);
    priceObject.getPricepanel().revalidate();

} }

         TIME CLASS

             class Time
 {

   private JComboBox    timeAirportbox;//comboboxes declared

     private String[] airport = {"","East Midlands", "Birmingham", "Manchester", "Heathrow"};//array of    airports declared
    private String[] destination = {"","New York", "Dahab", "Rome", "Sydney", "Tokyo"};//array of destinations declared
 private JPanel timePanel;
 private JLabel airportLabel;  
  private JLabel destinationLabel;
 public void SelectTime() {   

 //combobox objects created
      timePanel = new JPanel();
  timePanel.setBackground(Color.GRAY);

      timePanel.setBorder( new TitledBorder("Time"));
  timeAirportbox = new JComboBox(airport);//array is inserted into the JComboBox
  timePanel.add(timeAirportbox);
  timeAirportbox.setVisible(true);

}
   public JPanel getTimePanel() {
    return timePanel;
    }

     public JComboBox getAirportBox() {
    return timeAirportbox;      
    }


    }

            PRICE CLASS


                class Price {

     private JPanel pricePanel;
      private JLabel tester;

    public void SelectPrice() {
    pricePanel = new JPanel();
  pricePanel.setBackground(Color.YELLOW);
  pricePanel.setPreferredSize(new Dimension(400, 400));
  tester = new JLabel("HIFHI");
  pricePanel.add(tester);

  }

   public JPanel getPricepanel() {
    return pricePanel;
    }
    }
4

1 回答 1

1

你有无穷无尽的问题和问题清单。

两个直接是...

在你你Price的课上,你从不初始化pricePanel....

class Price {

    private JPanel pricePanel;
    private JLabel tester;

    public void SelectPrice() {
        pricePanel = new JPanel();
        pricePanel.setBackground(Color.YELLOW);

        tester = new JLabel("HIFHI");
        pricePanel.add(tester);

    }

    public JPanel getPricepanel() {
        return pricePanel;
    }
}

这就是导致您的NullPointerException.

您将面临的第二个问题是您创建了 的本地副本PriceObject,但是有些期望能够删除先前实例化对象添加的面板

public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox

    public void actionPerformed(ActionEvent clickTime) {
        Price priceObject = new Price();
        priceObject.getPricepanel();
//----> Remove a local reference of price panel <---- //       
        remove(priceObject.getPricepanel());
        priceObject.getPricepanel().revalidate();

        add(timeObject.getTimePanel(), BorderLayout.EAST);
        timeObject.getTimePanel().revalidate();

    }
}

//This one gives me 0 errors.
public class buttonprice implements ActionListener { //creating actionlistener for clicking on      timebutton to bring up a combobox

    public void actionPerformed(ActionEvent ClickPrice) {
        Price priceObject = new Price();
        priceObject.SelectPrice();
        remove(timeObject.getTimePanel());
        timeObject.getTimePanel().revalidate();

//----> Adding a local reference of price panel <---- //       
        add(priceObject.getPricepanel(), BorderLayout.EAST);
        priceObject.getPricepanel().revalidate();

    }
}
于 2013-03-21T00:23:16.323 回答