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);最後にお電話ください。すなわち、

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

これが私が得るものです:

ここに画像の説明を入力

于 2013-04-23T16:48:49.543 に答える