怎么修?变量与从内部类中访问,需要是最终的或有效的最终

时间:2018-06-11 19:28:38

标签: java compiler-errors

我正在使用Java开发Anti-AFK脚本并遇到此错误。

  

“变量与从内部类中访问,需要是最终的或有效的最终”

我已经搜索过,但仍然不知道如何修复。谢谢您的帮助。这是脚本。

还有一件事,这是我在这个网站上发表的第一篇文章,所以如果我错过任何内容,我会道歉。另外,如果你们真的想帮助我,因为我很愚蠢,我怎么能让关键听众课工作呢?它有自己的错误"MKeyListener is never used"感谢你的一切!

package com.plugin.TTT;

import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Main {


    Random random = new Random();

    public static void main(String[] args) throws InterruptedException {

        String Switch = "Off";
        boolean test = true;
        boolean On = false;
        JFrame afk = new JFrame("Anti AFK");
        afk.setLayout(new BoxLayout(afk.getContentPane(), BoxLayout.Y_AXIS));
        JLabel text = new JLabel("Anti AFK Program");

        JToggleButton toggleButton = new JToggleButton("On");
        ItemListener itemListener = new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                int state = itemEvent.getStateChange();
                if (state == ItemEvent.SELECTED) {
                    Switch = "On";
                    toggleButton.setText("Off");
                    afk.getContentPane().setBackground(Color.GREEN);
                } else {
                    Switch = "Off";
                    afk.getContentPane().setBackground(Color.RED);
                    toggleButton.setText("On");
                }
            }
        };
        toggleButton.addItemListener(itemListener);



        text.setAlignmentX(Component.CENTER_ALIGNMENT);
        text.setAlignmentY(Component.CENTER_ALIGNMENT);
        toggleButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        toggleButton.setAlignmentY(Component.CENTER_ALIGNMENT);

        afk.getContentPane().setBackground(Color.RED);
        afk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        afk.setSize(500, 500);
        afk.add(text);
        afk.add(toggleButton);
        afk.setVisible(true);




        while (Switch.equals("On")){
        Random diceRoller = new Random();

            int RN = diceRoller.nextInt(4) + 1;
            Thread.sleep(1000);

            System.out.println(RN);


            if (RN == 1) {
                try {

                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.VK_ESCAPE);
                    robot.keyPress(KeyEvent.VK_ESCAPE);
                    robot.keyPress(KeyEvent.VK_W);


                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
            if (RN == 2) {
                    try {

                        Robot robot = new Robot();
                        robot.keyPress(KeyEvent.VK_ESCAPE);
                        robot.keyPress(KeyEvent.VK_ESCAPE);
                        robot.keyPress(KeyEvent.VK_A);

                    } catch (AWTException e) {
                        e.printStackTrace();
                    }
                }
             if (RN == 3) {
                        try {

                            Robot robot = new Robot();
                            robot.keyPress(KeyEvent.VK_ESCAPE);
                            robot.keyPress(KeyEvent.VK_ESCAPE);
                            robot.keyPress(KeyEvent.VK_S);

                        } catch (AWTException e) {
                            e.printStackTrace();
                        }
                    }
             if (RN == 4) {
                            try {

                                Robot robot = new Robot();
                                robot.keyPress(KeyEvent.VK_ESCAPE);
                                robot.keyPress(KeyEvent.VK_ESCAPE);
                                robot.keyPress(KeyEvent.VK_D);

                            } catch (AWTException e) {
                                e.printStackTrace();
                            }
                        }

}






    }

class MyKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
System.exit(1);
System.out.println("YOU PRESSED");

}}}}

2 个答案:

答案 0 :(得分:1)

您可以通过将Switch变量移到课程的最高级别来使其变为全局变量,这样您就可以解决问题。

在方法中定义并由匿名内部类访问的变量必须是final。发生这种情况是因为在匿名类中您没有父作用域,并且您无法知道它是否更改了该变量。通过最终确定,您可以保证更改仅在内部类的范围内完成。通过将其设为全局,您可以将其显示为一般。

答案 1 :(得分:0)

我猜编译器抱怨JFrame afk

您在内部类中使用它,但它不是最终变量。

最简单的解决方案是将它作为内部类的构造函数变量提供并在那里使用它。你会以这种方式摆脱错误。