有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有内部类的swing Java 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);
                    }
            });

使用action listener,当按下按钮时,它应该调用我的另一个类中的lease函数。我希望它从那里返回到控制台输出。 netbeans给我的错误是:从内部类访问局部变量mavPlace;需要宣布为最终决定 .... 现在我去做了一个最终的减刑,只是为了看看发生了什么,它起了作用,但我不能编辑我复杂的细节,所以这是不可能的

我能做什么

谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    如果使用匿名类,则应将类中使用的参数设置为当前块中的最终类型或成员专用变量

    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);
                        }
                });
    
    
    
      }
    
    
    }
    

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