这是我去年与BlueJ一起开展的项目之一,但遇到了问题并且没有完成它。现在我又回来了,真的需要帮助。
这是我的GUI类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author TANVEER AHMED
*/
public class HouseAdmin
{
private JFrame frame = new JFrame();
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("Menu");
private JMenuItem listAllAppliances;
private JMenuItem turnOnSingleAppliance;
private JMenuItem switchOnAllLights;
private BorderEventListener listener;//this class the inner class to handle the events
private JTextArea textArea;
private String printHouse;
private House myHome;
/**
* Constructor for objects of class HouseAdmin
*/
public HouseAdmin()
{
menuBar = new JMenuBar();
listAllAppliances = new JMenuItem("List All appliances...");
turnOnSingleAppliance = new JMenuItem("Turn on a single appliance...");
switchOnAllLights = new JMenuItem("Switch On All Lights...");
fileMenu.add(listAllAppliances);
fileMenu.add(turnOnSingleAppliance);
fileMenu.add(switchOnAllLights);
menuBar.add(fileMenu);
textArea = new JTextArea(30,70);
frame.add(textArea, BorderLayout.CENTER);
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
frame.pack();
listener = new BorderEventListener();
listAllAppliances.addActionListener(listener);
turnOnSingleAppliance.addActionListener(listener);
switchOnAllLights.addActionListener(listener);
setupHouse();
}
private void setupHouse(){
//Sets up a house
House myHome = new House("Hatfield");
Light bulb1 = new Light(1,"Bed Light",50,"H");
Light bulb2 = new Light(2,"Kitchen Light",5,"L");
Light bulb3 = new Light(3,"Garden Light",30,"E");
Light bulb4 = new Light(4,"Bathroom Light",50,"H");
Light bulb5 = new Light(5,"Shower Light",5,"L");
Light bulb6 = new Light(6,"Solar Light",30,"E");
Light bulb7 = new Light(7,"Passage Light",5,"L");
Light bulb8 = new Light(8,"Storage Light",50,"H");
Light bulb9 = new Light(9,"Garage Light",30,"H");
Light bulb10 = new Light(10,"Living room Light",30,"E");
myHome.addAppliance(bulb1);
myHome.addAppliance(bulb2);
myHome.addAppliance(bulb3);
myHome.addAppliance(bulb4);
myHome.addAppliance(bulb5);
myHome.addAppliance(bulb6);
myHome.addAppliance(bulb7);
myHome.addAppliance(bulb8);
myHome.addAppliance(bulb9);
myHome.addAppliance(bulb10);
printHouse = myHome.toString();
//textArea.setText(myHome.toString());
}
class BorderEventListener implements ActionListener{
public void actionPerformed (ActionEvent e){
if(e.getSource() == listAllAppliances){
textArea.setText(printHouse);
}
if (e.getSource () == turnOnSingleAppliance){
String code = JOptionPane.showInputDialog("Please Enter Application Code");
int code2 = Integer.parseInt(code);
myHome.turnOnASingleAppliance(code2);
textArea.append("Appliance " + code2 + " Has been turned on!");
textArea.setText(printHouse);
}
if (e.getSource () == switchOnAllLights){
myHome.turnAllLightsOn();
textArea.setText(printHouse);
textArea.append("All lights have been turned on!");
}
}
}
}
你可以看到我称之为" turnOnSingleAppliance"在105号线上。 现在崩溃并将终端窗口抛向我。
这是该方法的代码:
import java.util.*;
/**
*
* @author TANVEER AHMED
*/
public class House {
private String address;
private int powerConsumption;
private ArrayList <ElectricalAppliance> HouseAppliances;
public House(String address) {
// initialise instance variables
this.address = address;
HouseAppliances = new ArrayList <ElectricalAppliance>();
}
public ElectricalAppliance turnOnASingleAppliance(int code){
for (int i = 0; i < HouseAppliances.size(); i++){
ElectricalAppliance app = HouseAppliances.get(i);
if (app.getCode() == code ){
app.lightSwitchOn();
}
}
return null;
}
}
也许你们需要看到更多来源?如果是这样我可以上传整个bluej项目。
我的直觉告诉我循环是错误的。你们觉得怎么样?
答案 0 :(得分:1)
更改此行
House myHome = new House("Hatfield");
到此
myHome = new House("Hatfield");
您没有在第一个实例中定义字段,因此您的错误可能在事件侦听器内的此行。
myHome.turnOnASingleAppliance(code2);
你所做的是一个简单的错误,被称为变量阴影&#34;,这意味着你要使用与字段相同的名称来定义局部变量。