我的新游戏JButton不工作? [英] My New Game JButton is not working?

查看:102
本文介绍了我的新游戏JButton不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在完成一项98%完成的作业。我已经制作了gui程序,其中jtextfield设置为0,但是当你玩游戏时它会改变。

Hi I am currently working on an assignment which I have 98% finished. I have made gui program where it has a jtextfield set to 0 but as you play the game it changes.

现在我创建了一个名为NEW GAME的按钮和放弃。单击时,我的退出按钮工作正常。但我的新游戏按钮是我的问题。

Now I have created a j button called "NEW GAME" and "QUIT". My Quit button works fine when clicked. but my new game button is my problem.

我想要它,这样当我点击按钮时,它将得分设为0

I want it so that when I click the button it sets the score to 0

 public ShinyButtonsGUIProgramToShare(String tit, int x, int y) {
        //The button GUI
        ShinyButtonsGUIToShare sbg = new ShinyButtonsGUIToShare("NYI", 552, 552, new ShinyButtons());
        sbg.setLocation(10, 10);
        getContentPane().add(sbg);
        //The score text and text box
        JLabel jlb = new JLabel("Score: ");
        jlb.setLocation(12, (y - 75));
        jlb.setSize(45, 40);
        getContentPane().add(jlb);

        JTextField jtf = new JTextField("0");
        jtf.setLocation(60, (y - 70));
        jtf.setSize(150, 30);
        jtf.setHorizontalAlignment(JTextField.RIGHT);
        getContentPane().add(jtf);


        JButton NewGame, Quit;
        NewGame = new JButton("New Game");
        Quit = new JButton("Quit");

        Quit.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) { 
            System.exit(0); 
          }});

        NewGame.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) { 
          JTextField jtf.setText("0");
          }});


        NewGame.setLocation((x - 220), (y - 70));
        NewGame.setSize(100, 30);
        getContentPane().add(NewGame);

        Quit.setLocation((x - 110), (y - 70));
        Quit.setSize(100, 30);
        getContentPane().add(Quit);
        setDefaultCloseOperation(EXIT_ON_CLOSE); // allow window to close 
        setSize(x, y);
        setLayout(null);
        setResizable(false);
    }

    public static void main(String[] args) {
        ShinyButtonsGUIProgramToShare sbgp = new ShinyButtonsGUIProgramToShare("Shiny   Buttons", 578, 634);
        sbgp.setVisible(true);
    }
}


推荐答案

你有一个上下文问题......

You have a context issue...

jtf 的实例无法在<上下文中引用code> NewGame 's ActionListener

The instance of jtf can not be referenced from within the context of the NewGame's ActionListener

你需要赚 jtf 和实例变量,因此可以从 ShinyButtonsGUIProgramToShare 对象的实例中的任何位置访问它。

You need to make jtf and instance variable so it can be accessed from anywhere within an instance of ShinyButtonsGUIProgramToShare object.

查看了解班级成员 Java编程语言的代码约定

这篇关于我的新游戏JButton不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆