-2

わかりました、このエラー awt eventqueue 0 nullpointerexception エラーが発生しています。JPanel を削除しようとすると。

私を混乱させているのは、価格ボタンをクリックするとJPanelの時間が削除されることです。それは完全に正常に動作しますが、タイムボタンをクリックすると、pricepanel が削除されず、代わりに 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です。

直面する 2 番目の問題は、 のローカル コピーを作成するという事実ですが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 に答える