2013-05-01 45 views
0

我正在为我的课程之一开发一个LAB,并且需要一些帮助。带有内部类的Java GUI动作监听器

我正在建设一个公寓大楼GUI,它将有一个菜单系统和许多不同类别之间的单独功能。由租户,员工和银行组成的综合体。

我目前有整个项目基于控制台的工作,但现在我被分配将其转换为GUI界面。

这是GUI在我的主函数的代码:

ApartmentComplex mavPlace = new ApartmentComplex(); //creates a new apartment complex object 
    mavPlace.aptBank.setBalance(ANNUAL_BUDGET); //sets the apartment bank budget 
    readFile(mavPlace); 

    mavPlace.goThroughAndAssignValues(mavPlace); 
    JFrame frame = new JFrame("My First GUI"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(300,300); 
    JButton button = new JButton("Press"); 
    frame.getContentPane().add(button); // Adds Button to content pane of frame 
    frame.setVisible(true); 

    button.addActionListener(new ActionListener() 
      { 
        public void actionPerformed(ActionEvent e) 
        { 
         //Execute when button is pressed       
         mavPlace.lease(mavPlace); 
        } 
      }); 

随着动作侦听器,当按下按钮时就应该调用一个函数租赁在另一个类矿井。从那里我想它回到控制台输出。 netbeans给我的错误是:局部变量mavPlace从内部类访问;需要被宣布为最终 ....现在我去了一个制定的decleration最后只是为了看看发生了什么,它的工作,但我无法编辑我的复杂细节,所以这是不可能的。

我该怎么办?

谢谢!

+1

'最终ApartmentComplex temp = mavPlace;',并使用'temp'代替动作侦听器中的'mavPlace' – iluxa 2013-05-01 08:58:21

回答

0

如果使用匿名类,你应该设置在类为final类型使用的参数在当前块中或作为成员私有变量。

class MyGUI 
{ 
    ApartmentComplex mavPlace; 
    public MyGUI() 
    { 
    JFrame frame = new JFrame("My First GUI"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(300,300); 
    JButton button = new JButton("Press"); 
    frame.getContentPane().add(button); // Adds Button to content pane of frame 
    frame.setVisible(true); 
    mavPlace = new ApartmentComplex(); //creates a new apartment complex object 
    mavPlace.aptBank.setBalance(ANNUAL_BUDGET); //sets the apartment bank budget 
    readFile(mavPlace); 

    mavPlace.goThroughAndAssignValues(mavPlace); 
    button.addActionListener(new ActionListener() 
      { 
        public void actionPerformed(ActionEvent e) 
        { 
         //Execute when button is pressed       
         mavPlace.lease(mavPlace); 
        } 
      }); 



    } 


} 

我认为你应该重新考虑你的程序结构。 如果你告诉我们完整的目的,你会得到更好的答案。