简体   繁体   English

切换JPanel-单击按钮时不切换

[英]Switching JPanels - Doesn't switch when clicking button

I've made a program and i'm having difficulty with switching JPanels on the click of a button. 我已经编写了一个程序,单击按钮就很难切换JPanels。

There are four buttons> "Time" "Hotels Menu" "Price" and "Exit" 有四个按钮:“时间”,“酒店菜单”,“价格”和“退出”

Exit was simple to do. 退出很简单。

The rest need to change JPanels to another Panel. 其余的需要将JPanels更改为另一个面板。 I've got the Price to work and the Time. 我有工作的价钱和时间。 However the Time stopped working and I can't get the Hotels menu to change. 但是,时间停止工作了,我无法更改“酒店”菜单。

    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){
        final Flights flightObject = new Flights();

        // Creates the main JFrame where all the panels will be situated.

        final 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);

        // This is for the options Menu.

        final 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");


        flightPanel.add(timeb);
        flightPanel.add(priceb);
        flightPanel.add(hotelb);
        flightPanel.add(exitb);


        mainFrame.add(flightPanel, BorderLayout.EAST);

        // There will be 2 panels on the left hand side for options travelPanel
        // and timePanel. These will be in another panel named leftPanel.

        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"};

        final JComboBox fromDest = new JComboBox(fromOptions);
        final JComboBox toDest = new JComboBox(toOptions);

        // This is for the date textbox.

        JPanel datePanel = new JPanel();
        datePanel.setLayout(new FlowLayout());   
        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.CENTER);
        mainFrame.pack();
        mainFrame.setVisible(true);

        // This creates the panel where the Time duration of the flight is output.

        final JPanel showTimePanel = new JPanel();
        showTimePanel.setLayout(new FlowLayout());
        showTimePanel.setVisible(false);


        mainFrame.add(showTimePanel, BorderLayout.EAST);

        final JPanel showPricePanel = new JPanel();
        showPricePanel.setLayout(new FlowLayout());
        showPricePanel.setVisible(false);

        mainFrame.add(showPricePanel, BorderLayout.EAST);

        // Creates the Panel for the Hotels Menu.

        final JPanel hotelPanel = new JPanel();
        hotelPanel.setVisible(false);
        hotelPanel.setLayout(new GridLayout(3,3));
        JButton hView,hAdd,hSort,hSave,hRetrieve,hExit;
        hView = new JButton("View");
        hAdd = new JButton ("Add");
        hSort = new JButton("Sort");
        hSave = new JButton ("Save");
        hRetrieve = new JButton ("Retrieve");
        hExit = new JButton ("Exit");

        hotelPanel.add(hView);
        hotelPanel.add(hAdd);
        hotelPanel.add(hSort);
        hotelPanel.add(hSave);
        hotelPanel.add(hRetrieve);
        hotelPanel.add(hExit);

        // buttonHandler class does all the eventhandling for the buttons.
        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);}

                else if (buttonText.equals("Hotels Menu")){
                    flightPanel.setVisible(false);
                    hotelPanel.setVisible(true);
                }
                else if (buttonText.equals("Time")){
                    int fromDestination = fromDest.getSelectedIndex();
                    int toDestination = toDest.getSelectedIndex();
                    flightObject.setTime(fromDestination, toDestination);
                    String timeTaken = flightObject.getTime();
                    JLabel timeLabel = new JLabel("The Duration of the Flight is: " + timeTaken);

                    showTimePanel.add(timeLabel);
                    flightPanel.setVisible(false);
                    showTimePanel.setVisible(true);

                }
                else if(buttonText.equals("Price")){
                    int fromDestination = fromDest.getSelectedIndex();
                    int toDestination = toDest.getSelectedIndex();
                    flightObject.setPrice(fromDestination, toDestination);
                    int Price = flightObject.getPrice();
                    JLabel priceLabel = new JLabel ("The Price of the Flight is: " + Price + " GBP");

                    showPricePanel.add(priceLabel);
                    flightPanel.setVisible(false);
                    showPricePanel.setVisible(true);
                }
            }

        }



        buttonHandler handler = new buttonHandler();

        exitb.addActionListener(handler);
        timeb.addActionListener(handler);
        hotelb.addActionListener(handler);
        priceb.addActionListener(handler);

    }

}

当您将另一个组件添加到同一位置时, BorderLayout将替换组件。

mainFrame.add(showTimePanel, BorderLayout.EAST);

mainFrame.add(showPricePanel, BorderLayout.EAST);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM