Code:
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class CustomerView
{
public static void addComponentsToPane(Container pane)
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel tCarModel = new JLabel("Car Model");
// inserts (top, left, bottom, right)
c.insets = new Insets(10,40,10,7);
c.gridx = 0;
c.gridy = 0;
pane.add(tCarModel, c);
// temporary string of Audi Models to go into combobox
String[] carModels = {"A2", "A3", "A4"};
JComboBox iCarModel = new JComboBox(carModels);
c.insets = new Insets(10,10,10,60);
c.gridx = 1;
c.gridy = 0;
pane.add(iCarModel, c);
JLabel tFirstDayOfRental = new JLabel("First Day of Rental");
c.insets = new Insets(10,10,10,7);
c.gridx = 2;
c.gridy = 0;
pane.add(tFirstDayOfRental, c);
JFormattedTextField iFirstDayOfRental = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy"));
iFirstDayOfRental.setValue(new Date());
c.insets = new Insets(10,10,10,40);
c.gridx = 3;
c.gridy = 0;
pane.add(iFirstDayOfRental, c);
JLabel tCarType = new JLabel("Car Type");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 1;
pane.add(tCarType, c);
// temporary string of Car Types to go into combobox
String[] carTypes = {"Audi"};
JComboBox iCarType = new JComboBox(carTypes);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 1;
pane.add(iCarType, c);
JLabel tNumberOfDays = new JLabel("Number of Days");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 1;
pane.add(tNumberOfDays, c);
JTextField iNumberOfDays = new JTextField(4);
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 1;
pane.add(iNumberOfDays, c);
JLabel tLicence = new JLabel("Licence");
c.insets = new Insets(20,40,10,7);
c.gridx = 0;
c.gridy = 2;
pane.add(tLicence, c);
String[] licence = {"International", "UK Licence"};
JList iLicence = new JList(licence);
c.insets = new Insets(20,10,10,60);
c.gridx = 1;
c.gridy = 2;
pane.add(iLicence, c);
JLabel tTimeOfBooking = new JLabel("Time of Booking");
c.insets = new Insets(20,10,10,7);
c.gridx = 2;
c.gridy = 2;
pane.add(tTimeOfBooking, c);
JFormattedTextField iTimeOfBooking = new JFormattedTextField(new SimpleDateFormat("dd/MM/yy:HH:mm"));
iTimeOfBooking.setValue(new Date());
c.insets = new Insets(20,10,10,40);
c.gridx = 3;
c.gridy = 2;
pane.add(iTimeOfBooking, c);
JButton iOK = new JButton("OK");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 3;
pane.add(iOK, c);
JButton iCancel = new JButton("Cancel");
c.insets = new Insets(30,10,20,10);
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 3;
pane.add(iCancel, c);
}
private static void createAndShowGUI()
{
// create and set up the window
JFrame tJFrame = new JFrame("Car Rental Information Systems");
tJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set up the content pane
addComponentsToPane(tJFrame.getContentPane());
// display the window
tJFrame.pack();
tJFrame.setVisible(true);
}
public static void main(String[] Args)
{
// schedule a job for the event-dispatching thread
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
I am trying to make the Cancel button close the frame... don't have a clue how to do it though. Any suggestions please?
Bookmarks