我编写了一个 GUI 来显示加拿大的省和地区。一切似乎都运行良好,但每次编译程序时,我都会遇到NullPointerException
. 提前感谢任何有帮助的人。
代码:
import java.io.*;
import java.awt.*; // the "basic" awt functionality
import javax.swing.*; // java's awesome swing functionality
import java.awt.event.*; // necessary for event listeners
public class ProvincesofCanada extends JApplet implements ActionListener{
//components
private JPanel panel;
private JTextField provinceCapital;
private JTextField provincePopulation;
private JTextField provinceConfederation;
private JComboBox provinceNameList;
private JLabel title;
private JLabel motto;
private JLabel provinceMotto;
private JLabel provinceName;
private ImageIcon provinceFlag;
private ImageIcon provinceMap;
private JLabel flagLabel;
private JLabel mapLabel;
private JLabel spacer;
//constants for window size
//private final int WINDOW_WIDTH = 257;
//private final int WINDOW_HEIGHT = 535;
public ProvincesofCanada(){
//Issue the title of the program
//setTitle("Provinces and Territories of Canada");
//Specify what happens when the close button is pressed
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Build the panel that contains the other components
buildPanel();
//Add the panel to the content pane
add(panel);
//Set the size and display the window
//setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//setVisible(true);
}
public void buildPanel(){
//Choose a Province prompt
JLabel chooseAProvince = new JLabel("Choose a Province");
String[] provinces = {"","Alberta","British Columbia","Manitoba",
"New Brunswick", "Newfoundland and Labrador","Northwest Territories",
"Nova Scotia","Nunavut","Ontario","Prince Edward Island","Quebec","Saskatchewan","Yukon Territory"};
provinceNameList = new JComboBox(provinces);
//ComboBox Listener
provinceNameList.addActionListener(this);
//Province Name
provinceName = new JLabel();
provinceName.setText("<html>Welcome to Canada<br>Bienvenue au Canada</html>");
//Province Motto
provinceMotto = new JLabel("");
Label lb1 = new Label();
// create a flag image icon
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/canadaflag.png");
flagLabel = new JLabel();
flagLabel.setIcon(new ImageIcon(provinceFlag));
// create a map image icon
provinceMap = new ImageIcon("images/canadamap.png");
mapLabel = new JLabel();
mapLabel.setIcon(provinceMap);
//Province Data
JLabel provinceCapitalLabel = new JLabel("Capital: ");
JLabel provincePopulationLabel = new JLabel("Population: ");
JLabel provinceConfederationLabel = new JLabel("Confederation: ");
// Create text fields
provinceCapital = new JTextField(10);
provinceCapital.setText("Ottawa");
provinceCapital.setEditable(false);
provincePopulation = new JTextField(10);
provincePopulation.setText("33,476,688");
provincePopulation.setEditable(false);
provinceConfederation = new JTextField(6);
provinceConfederation.setText("1867");
provinceConfederation.setEditable(false);
//Create new line
JLabel newLine = new JLabel(" ");
//create new instance of JPanel called panel
panel = new JPanel();
//add components to panel
panel.add(chooseAProvince);
panel.add(provinceNameList);
panel.add(provinceName);
panel.add(provinceMotto);
panel.add(flagLabel);
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(mapLabel);
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(provinceCapitalLabel);
panel.add(provinceCapital);
panel.add(spacer = new JLabel(" "),"span, grow");
panel.add(provincePopulationLabel);
panel.add(provincePopulation);
panel.add(provinceConfederationLabel);
panel.add(provinceConfederation);
}
public void actionPerformed(ActionEvent e){
String provinceChoice = String.valueOf(provinceNameList.getSelectedItem());
if (provinceChoice == "Alberta"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Fortis et liber\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/abflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/abmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Edmonton");
provincePopulation.setText("3,645,257");
provinceConfederation.setText("1905");
} else if (provinceChoice == "British Columbia"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Splendor sine occasu\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/bcflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/bcmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Victoria");
provincePopulation.setText("4,400,057");
provinceConfederation.setText("1871");
} else if (provinceChoice == "Manitoba"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Gloriosus ey Liber\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/mbflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/mbmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Winnipeg");
provincePopulation.setText("1,208,268");
provinceConfederation.setText("1870");
} else if (provinceChoice == "New Brunswick"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Spem Reduxit\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/nbflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/nbmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Fredericton");
provincePopulation.setText("751,171");
provinceConfederation.setText("1867");
} else if (provinceChoice == "Newfoundland and Labrador"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Quaerite Prime Regnum Dei\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/nlflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/nlmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("St. John's");
provincePopulation.setText("514,536");
provinceConfederation.setText("1949");
} else if (provinceChoice == "Northwest Territories"){
provinceName.setText(provinceChoice);
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/ntflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/ntmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Yellowknife");
provincePopulation.setText("41,462");
provinceConfederation.setText("1870");
} else if (provinceChoice == "Nova Scotia"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Munit Haec ey Altera Vincit\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/nsflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/nsmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Halifax");
provincePopulation.setText("921,727");
provinceConfederation.setText("1867");
} else if (provinceChoice == "Nunavut"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Nunavut Sanginivut\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/nuflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/numap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Iqaluit");
provincePopulation.setText("31,906");
provinceConfederation.setText("1999");
} else if (provinceChoice == "Ontario"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Ut Incepit Fidelis Sic Permanet\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/onflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/onmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Toronto");
provincePopulation.setText("12,851,821");
provinceConfederation.setText("1867");
} else if (provinceChoice == "Prince Edward Island"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Parva Sub Ingenti\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/peflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/pemap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Charlottetown");
provincePopulation.setText("140,204");
provinceConfederation.setText("1873");
} else if (provinceChoice == "Quebec"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Je me souviens\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/qbflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/qbmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Quebec City");
provincePopulation.setText("7,903,001");
provinceConfederation.setText("1867");
} else if (provinceChoice == "Saskatchewan"){
provinceName.setText("<html><center>" + provinceChoice + "<br>\"Multibus E Gentibus Vires\"</center></html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/skflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/skmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Regina");
provincePopulation.setText("1,033,381");
provinceConfederation.setText("1905");
} else if (provinceChoice == "Yukon Territory"){
provinceName.setText(provinceChoice);
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/ytflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/ytmap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Whitehorse");
provincePopulation.setText("33,897");
provinceConfederation.setText("1898");
} else{
provinceName.setText("<html>Welcome to Canada<br>/Bienvenue au Canada</html>");
provinceMotto.setText("");
java.net.URL provinceFlag = ProvincesofCanada.class.getResource("images/canadaflag.png");
flagLabel.setIcon(new ImageIcon(provinceFlag));
java.net.URL provinceMap = ProvincesofCanada.class.getResource("images/canadamap.png");
mapLabel.setIcon(new ImageIcon(provinceMap));
provinceCapital.setText("Ottawa");
provincePopulation.setText("33,476,688");
provinceConfederation.setText("1867");
}
}
// //main method
// public static void main(String[] args){
// ProvincesofCanada poc = new ProvincesofCanada();
// }
}
HTML 代码:
<html>
<head>
<title>Provinces and Territories of Canada</title>
</head>
<body></br></br>
<center><applet code ="ProvincesofCanada.class" width="257" height="535"></center>
</body>
</html>