好的,我收到此错误 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;
}
}