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

时间:2014-01-03 23:38:30

标签: java variables

这显然是一个非常简单的问题。但是,我无法解决它。我正在制作一个简单的代码游戏,其中中间有五个按钮,对应于数字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。其中,转向,应出现在框架中。它没有。

1 个答案:

答案 0 :(得分:2)

您的问题是,当您启动应用时,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();
        }

    }


    // ===== //