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

合理编码器

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

我的主要方法包含类:

公共类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;

    }
}
}

动作侦听器类

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然后,递减lives依次将其显示在框架中。事实并非如此。

петер.петров

您的问题是,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();
        }

    }


    // ===== //

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未定义的变量:变量不会在另一个页面中传递

从另一个变量调用类中的变量

调用另一个文件中的类和函数以编辑该类中的变量

一个类的实例可以更改另一个类的实例中的变量吗?

如何从另一个类中的函数更改类变量

更改一个对象中的变量是更新同一类的另一个对象的相同变量

从另一个类(在另一个模块中)调用自变量

如果从python中的另一个函数调用,则if语句的值不会在循环中更改

在Python中从一个类到另一个类访问变量

如何从另一个类更新一个类中的变量

在一个类中设置变量并将值返回给另一个类

如何获得在另一个类的一个类中声明的变量?

C#与另一个类中的一个类的变量进行交互

从Java中的另一个类访问变量

如何使用另一个类中的变量

在另一个类中调用实例变量

快速访问另一个类中的变量

更新另一个类中的变量

从Java中的另一个类访问变量

从python中的另一个类调用变量

REACTJS:在另一个类中更改状态

Oracle触发器不会在另一个表中插入值

重命名多个文件,而不会在php中覆盖另一个文件

在一个函数中定义的全局变量不会在另一个函数中保留定义

如何在一个类中的另一个函数中更新变量?

从另一个类(B类)更改类(A类)中的文本

在一个类中使用一个函数来更改另一个类中的变量的值C ++

在另一个类中更改Mainactivity变量的值

如何在Angular的另一个类中更改变量的值