我的代码出了什么问题?名为'leftPanel'的面板在我运行程序时没有显示,而'flightPanel
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TravelAgentSystem {
public static void main(String[] args){
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setTitle("Main Menu");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel flightPanel = new JPanel();
flightPanel.setLayout(new GridLayout(2,2));
JButton timeb, priceb, hotelb, exitb;
timeb = new JButton("Time");
priceb = new JButton ("Price");
hotelb = new JButton ("Hotels Menu");
exitb = new JButton ("Exit Program");
class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton)event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Exit Program")) {System.exit(0);}
}
}
buttonHandler handler = new buttonHandler();
exitb.addActionListener(handler);
flightPanel.add(timeb);
flightPanel.add(priceb);
flightPanel.add(hotelb);
flightPanel.add(exitb);
mainFrame.add(flightPanel, BorderLayout.CENTER);
JPanel travelPanel = new JPanel();
travelPanel.setLayout(new GridLayout(2,2));
travelPanel.setVisible(true);
JPanel timePanel = new JPanel();
timePanel = new JPanel();
timePanel.setLayout(new GridLayout(2,1));
timePanel.setVisible(true);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(2,1));
leftPanel.setVisible(true);
JLabel Lfrom, Lto, LDeparture;
Lfrom = new JLabel("From");
Lto = new JLabel("To");
LDeparture = new JLabel("Departure Date (DD/MM/YY)");
String[] fromOptions = {"East Midlands","Birmingham","Heathrow","Manchester"};
String[] toOptions = {"New York", "Dahab", "Rome", "Sydney", "Tokyo"};
JComboBox fromDest = new JComboBox(fromOptions);
JComboBox toDest = new JComboBox(toOptions);
JPanel datePanel = new JPanel();
datePanel.setLayout(new FlowLayout()); // So i can fix size of dateField
JTextField dateField = new JTextField();
dateField.setPreferredSize(new Dimension(100,20));
datePanel.add(dateField);
travelPanel.add(Lfrom);
travelPanel.add(fromDest);
travelPanel.add(Lto);
travelPanel.add(toDest);
timePanel.add(LDeparture);
timePanel.add(datePanel);
leftPanel.add(travelPanel);
leftPanel.add(timePanel);
mainFrame.add(leftPanel, BorderLayout.EAST);
}
}
答案 0 :(得分:2)
最后致电mainFrame.setVisible(true);
。即,
mainFrame.add(leftPanel, BorderLayout.EAST);
mainFrame.setVisible(true);
这是我得到的: