プログラムを作成しましたが、ボタンをクリックするだけで JPanels を切り替えることができません。
ボタンは4つ>「時間」「ホテルメニュー」「料金」「退出」
終了は簡単でした。
残りは JPanels を別の Panel に変更する必要があります。私は働く価格と時間を持っています。ただし、時間が機能しなくなり、ホテルメニューを変更できません。
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);
}
}