このエラーがどのように発生するのかわかりません。時間ボタンをクリックすると、時間コンボボックスが表示されるようにしました。私のコードは以下です。ボタンがクリックされたときなど、さまざまな方法を試しました。コンボボックスが挿入されたタイムパネルを挿入します。私のコードが機能しない理由は何ですか?
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events extends JFrame {
// variables for JPanel
private JPanel timePanel;
private JButton timeButton;
private JButton moneyButton;
private JButton hotelButton;
private JButton exitButton;
public events()
{
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
add(buttonPanel,BorderLayout.WEST);
JPanel timePanel = new JPanel();
timePanel.setBackground(Color.BLUE);
timePanel.setPreferredSize(new Dimension(320, 240));
add(timePanel,BorderLayout.EAST);
Time timeObject = new Time();
timeObject.SelectTime();
timePanel.add(timeObject.gettimePanel());
timeObject.gettimePanel().setVisible(false);
timeButton = new JButton("Time");
buttonPanel.add(timeButton);
moneyButton = new JButton("Price");
buttonPanel.add(moneyButton);
hotelButton = new JButton("Hotel");
buttonPanel.add(hotelButton);
exitButton = new JButton("Exit");
buttonPanel.add(exitButton);
buttontime clickTime = new buttontime();
timeButton.addActionListener(clickTime);
}
//classes for Time
public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox
public void actionPerformed(ActionEvent clickTime) {
Time timeObject = new Time();
timeObject.SelectTime();
timePanel.add(timeObject.gettimePanel());
timeObject.gettimePanel().setVisible(true);
}
}
public static void main (String args[]) {
events mainmenu = new events(); //object is created
mainmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainmenu.setSize(600,400);
mainmenu.setVisible(true);
mainmenu.setLayout(new BorderLayout());
mainmenu.setTitle("Travel Agent System");
mainmenu.setBackground(Color.BLUE);
}
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Time
{
private JComboBox timeairportbox;//comboboxes declared
private String[] Airport = {"","East Midlands", "Birmingham", "Manchester", "Heathrow"};//array of airports declared
public void SelectTime() {
//combobox objects created
timeairportbox = new JComboBox(Airport);//array is inserted into the JComboBox
timeairportbox.setVisible(false); // Combobox is placed in the timePanel
}
public JComponent gettimePanel() {
return timeairportbox;
}
}
}