这是一个非常基本的问题,但我认为我整天都没有看到代码。
在GUIMainEvent类中,每次都对来自其他类的对象的所有引用都进行了硬编码,即每次从客户屏幕对单选按钮的引用都是gui.customerScreen.rdbtnOneWay。
我在GUIMainEvent中创建了变量,因此它看起来更好,更易于阅读和修改,但现在我已经
了Exception in thread "main" java.lang.NullPointerException
at projFlight.GUIMainEvent.<init>(GUIMainEvent.java:36)
at projFlight.GUIMain.<init>(GUIMain.java:25)
at projFlight.GUIMain.main(GUIMain.java:35)
这是指我试图创建的第一个变量,但我知道每个变量都会发生。
我可以在哪里声明这些变量以避免这种情况?我认为gui.frame存在于我分配值的地方。谢谢你的期待。
package projFlight;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.BorderLayout;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
public class GUIMain {
GUIMainEvent event = new GUIMainEvent(this);
JFrame frame;
GUILoginScreen login = new GUILoginScreen();
GUICustomerScreen customerScreen = new GUICustomerScreen();
/**
* Launch the application.
*/
public static void main(String[] args) {
GUIMain window = new GUIMain();
window.frame.setVisible(true);
}
/**
* Create the application.
*/
public GUIMain() {
setLookAndFeel();
initialize();
controller.start();
}
// The thread controlling changes of panels in the main window.
private Thread controller = new Thread() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.getContentPane().add(login);
addLogo(login);
frame.revalidate();
}
});
}
};
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.BLUE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
frame.setBounds(100, 100, 406, 473);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
login.btnOk.addActionListener(event);
login.btnCancel.addActionListener(event);
customerScreen.rdbtnOneWay.addActionListener(event);
customerScreen.rdbtnReturnsecondLeg.addActionListener(event);
customerScreen.cboDeptLeg1.addActionListener(event);
customerScreen.cboDestLeg1.addActionListener(event);
customerScreen.cboSeatType.addActionListener(event);
customerScreen.cboDeptLeg2.addActionListener(event);
customerScreen.cboDestLeg2.addActionListener(event);
customerScreen.chkbxInsurance.addActionListener(event);
customerScreen.btnBookFlights.addActionListener(event);
customerScreen.btnClear.addActionListener(event);
customerScreen.btnLogout.addActionListener(event);
}
public void addLogo(JPanel panel) {
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(new File("C:\\Users\\Phil\\workspace\\projFlight\\Pictures\\WolfLogo.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
picLabel.setBounds(0, 0, 170, 128);
panel.add(picLabel);
}
// method to set the look and feel of the GUI
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
}
package projFlight;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.JPasswordField;
import javax.swing.JButton;
public class GUILoginScreen extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField tboUsername;
JPasswordField passwordField;
JButton btnOk;
JButton btnCancel;
/**
* Create the panel.
*/
public GUILoginScreen() {
setBackground(Color.DARK_GRAY);
setLayout(null);
setLookAndFeel();
JLabel lblUsername = new JLabel("Username");
lblUsername.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
lblUsername.setBounds(77, 170, 109, 35);
add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
lblPassword.setBounds(77, 235, 109, 35);
add(lblPassword);
tboUsername = new JTextField();
tboUsername.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
tboUsername.setBounds(77, 203, 241, 31);
add(tboUsername);
tboUsername.setColumns(10);
passwordField = new JPasswordField();
passwordField.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
passwordField.setBounds(77, 268, 241, 31);
add(passwordField);
btnOk = new JButton("OK");
btnOk.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
btnOk.setBounds(77, 349, 109, 35);
add(btnOk);
btnCancel = new JButton("Cancel");
btnCancel.setFont(new Font("Segoe UI Black", Font.ITALIC, 18));
btnCancel.setBounds(209, 349, 109, 35);
add(btnCancel);
}
// method to set the look and feel of the GUI
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
}
/**
*
*/
package projFlight;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import java.awt.event.ActionEvent;
/**
* @author Phil
*
*/
public class GUIMainEvent implements ActionListener {
ArrayList<String> airportList = new ArrayList<String>();
GUIMain gui;
GUIMainEvent(GUIMain in) {
gui = in;
airportList.add("Dublin");
airportList.add("Sligo");
airportList.add("Luton");
}
JFrame mainGUI = gui.frame;
GUICustomerScreen customerScreenGUI = gui.customerScreen;
GUILoginScreen loginGUI = gui.login;
JButton btnLoginOk = gui.login.btnOk;
JButton btnLoginCancel = gui.login.btnCancel;
JRadioButton rdbtnCustomerOneWay = gui.customerScreen.rdbtnOneWay;
JRadioButton rdbtnCustomerReturn = gui.customerScreen.rdbtnReturnsecondLeg;
JComboBox cboCustomerDeptLeg1 = gui.customerScreen.cboDeptLeg1;
JComboBox cboCustomerDestLeg1 = gui.customerScreen.cboDestLeg1;
JComboBox cboCustomerDeptLeg2 = gui.customerScreen.cboDeptLeg2;
JComboBox cboCustomerDestLeg2 = gui.customerScreen.cboDestLeg2;
JLabel lblCustomerDeptLeg2 = gui.customerScreen.lblDeptLeg2;
JLabel lblCustomerDestLeg2 = gui.customerScreen.lblDestLeg2;
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object source = event.getSource();
if (source == btnLoginOk) {
mainGUI.getContentPane().remove(loginGUI);
mainGUI.repaint();
mainGUI.getContentPane().add(customerScreenGUI);
gui.addLogo(customerScreenGUI);
customerScreenGUI.setVisible(true);
mainGUI.repaint();
mainGUI.revalidate();
} else if (source == btnLoginCancel) {
System.exit(0);
} else if (source == rdbtnCustomerOneWay) {
if (rdbtnCustomerOneWay.isSelected()) {
disableLeg2();
}
} else if (source == rdbtnCustomerReturn) {
if (rdbtnCustomerReturn.isSelected()) {
enableLeg2();
}
} else if (source == cboCustomerDeptLeg1) {
updateDestLeg1();
} else if (source == cboCustomerDestLeg1) {
if (!rdbtnCustomerOneWay.isSelected()) {
updateDeptLeg2();
}
} else if (source == cboCustomerDeptLeg2) {
if (!rdbtnCustomerOneWay.isSelected()) {
updateDestLeg2();
}
} else if (source == gui.customerScreen.btnBookFlights) {
bookFlights();
} else if (source == gui.customerScreen.btnClear) {
clearAll();
} else if (source == gui.customerScreen.btnLogout) {
logout();
}
}
private void logout() {
// TODO Auto-generated method stub
}
private void clearAll() {
cboCustomerDeptLeg1.setSelectedIndex(0);
cboCustomerDestLeg1.removeAllItems();
cboCustomerDeptLeg2.removeAllItems();
cboCustomerDestLeg2.removeAllItems();
}
private void bookFlights() {
// TODO Auto-generated method stub
}
private void updateDestLeg2() {
JComboBox from = cboCustomerDeptLeg2;
JComboBox to = cboCustomerDestLeg2;
if (from.getSelectedItem() != null) {
to.removeAllItems();
for (int i = 0; i < cboCustomerDeptLeg1.getItemCount(); i++) {
if (cboCustomerDeptLeg1.getItemAt(i).toString().equals(gui.customerScreen.defaultItem)) {
continue;
} else if (!(from.getSelectedItem().toString().equals(cboCustomerDeptLeg1.getItemAt(i).toString()))) {
to.addItem(cboCustomerDeptLeg1.getItemAt(i));
}
}
}
}
private void updateDeptLeg2() {
JComboBox from = cboCustomerDestLeg1;
JComboBox to = cboCustomerDeptLeg2;
if (from.getSelectedItem() != null) {
to.removeAllItems();
to.addItem(from.getSelectedItem());
}
}
private void updateDestLeg1() {
JComboBox from = cboCustomerDeptLeg1;
JComboBox to = cboCustomerDestLeg1;
if (from.getSelectedItem() != null) {
to.removeAllItems();
for (int i = 0; i < from.getItemCount(); i++) {
if (from.getItemAt(i).toString().equals(gui.customerScreen.defaultItem)) {
continue;
} else if (!(from.getSelectedItem().toString().equals(from.getItemAt(i).toString()))) {
to.addItem(from.getItemAt(i));
}
}
}
}
private void enableLeg2() {
updateDeptLeg2();
updateDestLeg2();
lblCustomerDeptLeg2.setEnabled(true);
lblCustomerDestLeg2.setEnabled(true);
cboCustomerDeptLeg2.setEnabled(true);
cboCustomerDestLeg2.setEnabled(true);
}
private void disableLeg2() {
cboCustomerDeptLeg2.removeAllItems();
cboCustomerDestLeg2.removeAllItems();
lblCustomerDeptLeg2.setEnabled(false);
lblCustomerDestLeg2.setEnabled(false);
cboCustomerDeptLeg2.setEnabled(false);
cboCustomerDestLeg2.setEnabled(false);
}
}
package projFlight;
import javax.swing.JPanel;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class GUICustomerScreen extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Create the panel.
*/
String firstName = "Philip";
String lastName = "Woulfe";
JRadioButton rdbtnOneWay;
JRadioButton rdbtnReturnsecondLeg;
ButtonGroup group;
JComboBox<String> cboDeptLeg1;
JComboBox<String> cboDestLeg1;
JComboBox<String> cboSeatType;
JLabel lblDeptLeg2;
JLabel lblDestLeg2;
JComboBox<String> cboDeptLeg2;
JComboBox<String> cboDestLeg2;
JCheckBox chkbxInsurance;
JButton btnBookFlights;
JButton btnClear;
JButton btnLogout;
String defaultItem = "--Select Airport--";
public GUICustomerScreen() {
setLayout(null);
JLabel lblHello = new JLabel("<html>Hello " + firstName + "<br>" + lastName + "</html>");
lblHello.setFont(new Font("Segoe UI Black", Font.ITALIC, 14));
lblHello.setBounds(260, 25, 107, 44);
add(lblHello);
rdbtnOneWay = new JRadioButton("One Way");
rdbtnOneWay.setSelected(true);
rdbtnOneWay.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
rdbtnOneWay.setBounds(88, 113, 109, 23);
add(rdbtnOneWay);
// login.btnOk.addActionListener(event);
rdbtnReturnsecondLeg = new JRadioButton("Return/Second Leg");
rdbtnReturnsecondLeg.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
rdbtnReturnsecondLeg.setBounds(199, 113, 143, 23);
add(rdbtnReturnsecondLeg);
group = new ButtonGroup();
group.add(rdbtnOneWay);
group.add(rdbtnReturnsecondLeg);
JLabel lblDeparture = new JLabel("Departure ");
lblDeparture.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
lblDeparture.setBounds(40, 146, 67, 14);
add(lblDeparture);
JLabel lblArrival = new JLabel("Destination");
lblArrival.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
lblArrival.setBounds(154, 146, 67, 14);
add(lblArrival);
JLabel lblSeatType = new JLabel("Seat Type");
lblSeatType.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
lblSeatType.setBounds(260, 146, 67, 14);
add(lblSeatType);
cboDeptLeg1 = new JComboBox<String>();
cboDeptLeg1.setMaximumRowCount(10);
cboDeptLeg1.setBounds(40, 171, 104, 20);
cboDeptLeg1.removeAllItems();
cboDeptLeg1.addItem(defaultItem);
cboDeptLeg1.addItem("Dublin");
cboDeptLeg1.addItem("Luton");
cboDeptLeg1.addItem("Sligo");
add(cboDeptLeg1);
cboDestLeg1 = new JComboBox<String>();
cboDestLeg1.setBounds(154, 171, 96, 20);
add(cboDestLeg1);
cboSeatType = new JComboBox<String>();
cboSeatType.setBounds(260, 171, 82, 20);
add(cboSeatType);
lblDeptLeg2 = new JLabel("Departure ");
lblDeptLeg2.setEnabled(false);
lblDeptLeg2.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
lblDeptLeg2.setBounds(40, 202, 67, 14);
add(lblDeptLeg2);
lblDestLeg2 = new JLabel("Destination");
lblDestLeg2.setEnabled(false);
lblDestLeg2.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
lblDestLeg2.setBounds(154, 202, 67, 14);
add(lblDestLeg2);
cboDeptLeg2 = new JComboBox<String>();
cboDeptLeg2.setEnabled(false);
cboDeptLeg2.setBounds(40, 227, 104, 20);
add(cboDeptLeg2);
cboDestLeg2 = new JComboBox<String>();
cboDestLeg2.setEnabled(false);
cboDestLeg2.setBounds(154, 227, 96, 20);
add(cboDestLeg2);
chkbxInsurance = new JCheckBox("Would you like to purchase insurance?");
chkbxInsurance.setSelected(true);
chkbxInsurance.setFont(new Font("Segoe UI Black", Font.ITALIC, 11));
chkbxInsurance.setBounds(40, 254, 250, 23);
add(chkbxInsurance);
btnBookFlights = new JButton("Book Flight(s)");
btnBookFlights.setBounds(40, 331, 104, 23);
add(btnBookFlights);
btnClear = new JButton("Clear");
btnClear.setBounds(154, 331, 106, 23);
add(btnClear);
btnLogout = new JButton("Logout");
btnLogout.setBounds(270, 331, 97, 23);
add(btnLogout);
}
}