java - 在一个类中编辑变量似乎不会在另一个类中更改它

标签 java variables

这显然是一个非常简单的问题。但是,我无法修复它。我正在制作一个简单的代码游戏,其中中间有五个按钮,分别对应数字 1 到 5。目标是回答顶部给出的数字问题。如果你通过了给定的最大数字,你可以猜一个问题多少次,我想改变你在屏幕上的生命数。我有 3 节课。

我的主要方法包含类:

公共(public)类 LevelChanger {

protected static int buttoncount = 0;
protected static int buttonlimit = 20;
protected static int answer = 12;
protected static int level = 1;
protected static int lives = 10;
protected static String Hint = "";
protected static String Question = "default text";

public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CodeGui frame = new CodeGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
        checkbuttonlimitAndAnswer();

}

protected static void checkbuttonlimitAndAnswer() {

    if (buttoncount > buttonlimit) {
        lives--;
        buttoncount = 0;

    }
}
}

Action 监听器类

public class ActionListeners implements ActionListener {


public void actionPerformed(ActionEvent B) {

    CodeGui Gui = new CodeGui();

    if (Gui.num1.getName().equals(((Component) B.getSource()).getName())) {
        LevelChanger.buttoncount++;
    }
    if (Gui.num2.getName().equals(((Component) B.getSource()).getName())) {
        LevelChanger.buttoncount += 2;
    }
    if (Gui.num3.getName().equals(((Component) B.getSource()).getName())) {
        LevelChanger.buttoncount += 3;
    }
    if (Gui.num4.getName().equals(((Component) B.getSource()).getName())) {
        LevelChanger.buttoncount += 4;
    }
    if (Gui.num5.getName().equals(((Component) B.getSource()).getName())) {
        LevelChanger.buttoncount += 5;
        System.out.println(LevelChanger.buttoncount);
    }

}
}

最后是 gui 类:

public class CodeGui extends JFrame {
private static final long serialVersionUID = 1L;

ActionListeners Al = new ActionListeners();


protected JPanel contentPane;

JTextArea questionArea, hintArea;
JLabel livesleft, lblLevel, Maxbutclick;
final JButton num1, num2, num3, num4, num5;

public CodeGui() {

    setTitle("The Code Game");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 602, 411);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLACK);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    livesleft = new JLabel("Lives Left : " + LevelChanger.lives);
    livesleft.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 14));
    livesleft.setForeground(Color.WHITE);
    livesleft.setBounds(10, 347, 126, 14);
    contentPane.add(livesleft);

    lblLevel = new JLabel("level : " +  LevelChanger.level);
    lblLevel.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 14));
    lblLevel.setForeground(Color.WHITE);
    lblLevel.setBounds(468, 347, 108, 14);
    contentPane.add(lblLevel);

    ActionListeners buttonL = new ActionListeners();

    num1 = new JButton("1");
    num1.setBounds(10, 229, 89, 23);
    num1.setName("button1");
    num1.addActionListener(buttonL);
    contentPane.add(num1);

    num2 = new JButton("2");
    num2.setBounds(128, 229, 89, 23);
    num2.setName("button2");
    num2.addActionListener(buttonL);
    contentPane.add(num2);

    num3 = new JButton("3");
    num3.setBounds(248, 229, 89, 23);
    num3.setName("button3");
    num3.addActionListener(buttonL);
    contentPane.add(num3);

    num4 = new JButton("4");
    num4.setBounds(374, 229, 89, 23);
    num4.setName("button4");
    num4.addActionListener(buttonL);
    contentPane.add(num4);

    num5 = new JButton("5");
    num5.setBounds(487, 229, 89, 23);
    num5.setName("button5");
    num5.addActionListener(buttonL);
    contentPane.add(num5);

    questionArea = new JTextArea();
    questionArea.setBackground(Color.BLACK);
    questionArea.setForeground(Color.WHITE);
    questionArea.setLineWrap(true);
    questionArea.setFont(new Font("Myriad Web Pro Condensed", Font.PLAIN,
            14));
    questionArea.setWrapStyleWord(true);
    questionArea.setText( LevelChanger.Question);
    questionArea.setRows(10);
    questionArea.setColumns(5);
    questionArea.setBounds(68, 21, 461, 159);
    contentPane.add(questionArea);

    hintArea = new JTextArea();
    hintArea.setForeground(Color.WHITE);
    hintArea.setBackground(Color.BLACK);
    hintArea.setText("Hint : " + LevelChanger.Hint);
    hintArea.setBounds(95, 278, 397, 58);
    contentPane.add(hintArea);

    Maxbutclick = new JLabel("Max Button count for level : "+  LevelChanger.buttonlimit);
    Maxbutclick.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 11));
    Maxbutclick.setBackground(Color.BLACK);
    Maxbutclick.setForeground(Color.WHITE);
    Maxbutclick.setBounds(321, 263, 255, 14);
    contentPane.add(Maxbutclick);

}

}

我正在尝试检查buttoncount是否超过buttonlimit。然后,减少生命。反过来,它应该出现在框架中。但事实并非如此。

最佳答案

您的问题是,当您启动应用程序时,checkbuttonlimitAndAnswer 仅被调用一次。所以你调用它,检查失败(即为假),然后你不再调用它。每次增加 buttoncount 时都应该调用它。

这是您的代码的固定版本。
将其与您的版本进行比较,看看我更改了什么。

    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ActionListeners implements ActionListener {

        public void actionPerformed(ActionEvent B) {

            CodeGui Gui = new CodeGui();

            if (Gui.num1.getName().equals(((Component) B.getSource()).getName())) {
                // LevelChanger.buttoncount++;
                LevelChanger.incButtonCount(1);
            }
            if (Gui.num2.getName().equals(((Component) B.getSource()).getName())) {
                // LevelChanger.buttoncount += 2;
                LevelChanger.incButtonCount(2);
            }
            if (Gui.num3.getName().equals(((Component) B.getSource()).getName())) {
                // LevelChanger.buttoncount += 3;
                LevelChanger.incButtonCount(3);
            }
            if (Gui.num4.getName().equals(((Component) B.getSource()).getName())) {
                // LevelChanger.buttoncount += 4;
                LevelChanger.incButtonCount(4);
            }
            if (Gui.num5.getName().equals(((Component) B.getSource()).getName())) {
                // LevelChanger.buttoncount += 5;
                LevelChanger.incButtonCount(5);
                // System.out.println(LevelChanger.buttoncount);
            }

        }
    }


    // ===== //


    import java.awt.Color;
    import java.awt.Font;

    import javax.swing.*;
    import javax.swing.border.EmptyBorder;

    public class CodeGui extends JFrame {
        private static final long serialVersionUID = 1L;

        ActionListeners Al = new ActionListeners();

        protected JPanel contentPane;

        JTextArea questionArea, hintArea;
        JLabel livesleft, lblLevel, Maxbutclick;
        final JButton num1, num2, num3, num4, num5;

        public CodeGui() {

            setTitle("The Code Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 602, 411);
            setResizable(false);
            contentPane = new JPanel();
            contentPane.setBackground(Color.BLACK);
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            livesleft = new JLabel("Lives Left : " + LevelChanger.lives);
            livesleft.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 14));
            livesleft.setForeground(Color.WHITE);
            livesleft.setBounds(10, 347, 126, 14);
            contentPane.add(livesleft);

            lblLevel = new JLabel("level : " + LevelChanger.level);
            lblLevel.setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 14));
            lblLevel.setForeground(Color.WHITE);
            lblLevel.setBounds(468, 347, 108, 14);
            contentPane.add(lblLevel);

            ActionListeners buttonL = new ActionListeners();

            num1 = new JButton("1");
            num1.setBounds(10, 229, 89, 23);
            num1.setName("button1");
            num1.addActionListener(buttonL);
            contentPane.add(num1);

            num2 = new JButton("2");
            num2.setBounds(128, 229, 89, 23);
            num2.setName("button2");
            num2.addActionListener(buttonL);
            contentPane.add(num2);

            num3 = new JButton("3");
            num3.setBounds(248, 229, 89, 23);
            num3.setName("button3");
            num3.addActionListener(buttonL);
            contentPane.add(num3);

            num4 = new JButton("4");
            num4.setBounds(374, 229, 89, 23);
            num4.setName("button4");
            num4.addActionListener(buttonL);
            contentPane.add(num4);

            num5 = new JButton("5");
            num5.setBounds(487, 229, 89, 23);
            num5.setName("button5");
            num5.addActionListener(buttonL);
            contentPane.add(num5);

            questionArea = new JTextArea();
            questionArea.setBackground(Color.BLACK);
            questionArea.setForeground(Color.WHITE);
            questionArea.setLineWrap(true);
            questionArea.setFont(new Font("Myriad Web Pro Condensed", Font.PLAIN,
                    14));
            questionArea.setWrapStyleWord(true);
            questionArea.setText(LevelChanger.Question);
            questionArea.setRows(10);
            questionArea.setColumns(5);
            questionArea.setBounds(68, 21, 461, 159);
            contentPane.add(questionArea);

            hintArea = new JTextArea();
            hintArea.setForeground(Color.WHITE);
            hintArea.setBackground(Color.BLACK);
            hintArea.setText("Hint : " + LevelChanger.Hint);
            hintArea.setBounds(95, 278, 397, 58);
            contentPane.add(hintArea);

            Maxbutclick = new JLabel("Max Button count for level : "
                    + LevelChanger.buttonlimit);
            Maxbutclick
                    .setFont(new Font("Stencil Std", Font.BOLD | Font.ITALIC, 11));
            Maxbutclick.setBackground(Color.BLACK);
            Maxbutclick.setForeground(Color.WHITE);
            Maxbutclick.setBounds(321, 263, 255, 14);
            contentPane.add(Maxbutclick);

        }

        public void update() {
            livesleft.setText("Lives Left : " + LevelChanger.lives);
            livesleft.repaint();
        }

    }


    // ===== //


    import java.awt.EventQueue;

    public class LevelChanger {

        private static int buttoncount = 0;
        private static CodeGui frame = null;

        protected static int buttonlimit = 20;
        protected static int answer = 12;
        protected static int level = 1;
        protected static int lives = 10;
        protected static String Hint = "";
        protected static String Question = "default text";

        public static void main(String[] args) {

            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        CodeGui frame = new CodeGui();
                        LevelChanger.frame = frame;
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });
            checkbuttonlimitAndAnswer();

        }

        protected static void checkbuttonlimitAndAnswer() {
            System.out.println("1) buttoncount = " + buttoncount + " // lives = " + lives  + " // buttonlimit = " + buttonlimit);

            if (buttoncount > buttonlimit) {
                lives--;
                buttoncount = 0;
            }

            System.out.println("2) buttoncount = " + buttoncount + " // lives = " + lives  + " // buttonlimit = " + buttonlimit);

            if (frame != null) {
                frame.update();
            }

        }

        protected static void incButtonCount(int val) {
            buttoncount += val;
            checkbuttonlimitAndAnswer();
        }

    }


    // ===== //

关于java - 在一个类中编辑变量似乎不会在另一个类中更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20914728/

相关文章:

java - HashMap 实现,允许覆盖 hash 和 equals 方法

使用 UI 编辑 MS Office 或 Open Office 文件的 Java API 或库

java - Android - 使用方法设置字符串变量

javascript - 将变量传递给 JavaScript 函数

php - 为什么我不能索引 mysql 返回的数组?

java - 迭代 HashMap 时引用局部变量 -> 从 lambda 表达式引用的局部变量必须是最终的或有效的最终错误

java - 如何在网格中导航寻找最接近的位置?

java - 如何在特定位置创建断点,而断点后没有任何代码

python - 在另一个函数中引用变量?

bash - 为什么这种花括号和双引号的组合在 bash 中不起作用?