0

我的代码有什么问题?当我运行程序时,名为“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);


    }
}
4

1 回答 1

2

最后打电话mainFrame.setVisible(true);。IE,

mainFrame.add(leftPanel, BorderLayout.EAST);
    mainFrame.setVisible(true);

这是我得到的:

在此处输入图像描述

于 2013-04-23T16:48:49.543 回答