
December 9th, 2007, 10:53 PM
|
|
Registered User
|
|
Join Date: Dec 2007
Posts: 1
Time spent in forums: 10 m 54 sec
Reputation Power: 0
|
|
|
Help with JButton
I'm creating an application that manages the flights for an airline. I'm having trouble getting the exit button to work. I have not gotten to the point of programming the others yet as I will be doing that as I add those features. Any help would be appreciated.
Thanks
Code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class FlightManager implements ActionListener{
// JDBC driver name and database url
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/flightmanager";
private JTextField fn = new JTextField(15);
private JTextField oc = new JTextField(15);
private JTextField dc = new JTextField(15);
private JTextField dt = new JTextField(15);
private JLabel fnl = new JLabel("Flight Number: ");
private JLabel ocl = new JLabel("City of Origin: ");
private JLabel dcl = new JLabel("Destination City: ");
private JLabel dtl = new JLabel("Departure Time: ");
private int flightNum;
private String originCity;
private String destinationCity;
private String departure;
private JButton addButton;
private JButton removeButton;
private JButton searchButton;
private JButton exitButton;
public static void main(String[] args) throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(DATABASE_URL, "root", "lilly");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
FlightManager fm = new FlightManager();
Container pc = new Container();
pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS));
JPanel blank = new JPanel();
pc.add(blank);
pc.add(fm.getFlightInputPanel());
pc.add(fm.getOriginPanel());
pc.add(fm.getDestinationPanel());
pc.add(fm.getDepartureTimePanel());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(pc, "First");
f.add(fm.getButtonPanel(), "Last"); // adds button panel to JFrame
f.setSize(500, 200);
f.setLocation(200,200);
f.setVisible(true);
}
private JPanel getFlightInputPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel fnp = new JPanel();
fnp.add(fnl);
fnp.add(fn);
return fnp;
}
private JPanel getOriginPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel ocp = new JPanel();
ocp.add(ocl);
ocp.add(oc);
return ocp;
}
private JPanel getDestinationPanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel dcp = new JPanel();
dcp.add(dcl);
dcp.add(dc);
return dcp;
}
private JPanel getDepartureTimePanel(){
FlowLayout flightInputLayout = new FlowLayout(FlowLayout.CENTER);
JPanel dtp = new JPanel();
dtp.add(dtl);
dtp.add(dt);
return dtp;
}
private JPanel getButtonPanel(){
FlowLayout buttonLayout = new FlowLayout(FlowLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton = new JButton("Add Flight");
addButton.addActionListener(this);
removeButton = new JButton("Delete Flight");
removeButton.addActionListener(this);
searchButton = new JButton("Search For Flight");
searchButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(searchButton);
buttonPanel.add(exitButton);
return buttonPanel;
}
// processes actions for when buttons are pushed
public void actionPerformed(ActionEvent e) {
// action to take if add ball button is pushed
if(e.getSource() == addButton)
flightNum = Integer.parseInt(fn.getText());
originCity = oc.getText();
destinationCity = dc.getText();
departure = dt.getText();
// clears out current contents of JTextFields
fn = null;
oc = null;
dc = null;
dt = null;
// action to take if delete ball button is pushed
if(e.getSource() == searchButton)
if(e.getSource() == removeButton)
// action to take if exit button is pushed
if(e.getSource() == exitButton)
System.exit(0);
}
}
|