Java swing-对点击执行操作 [英] Java swing- perform an action on click

查看:327
本文介绍了Java swing-对点击执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经把自己写成了一个角落。我正在尝试使用java swing做这个效果。

I think I've coded myself into a bit of a corner here. I am trying to do something to this effect using java swing.

单击下一步按钮,从文件中加载一个新行(通过行索引号),然后如果文件中的行的日期尚未到达,则灰显下一个按钮。我的问题是,当我有以下代码时:

On click of the button next, load a new line from a file (via line index number), then if the date from the line in the file has not yet arrived, grey out the next button. My issue is that when I have the following code:

    Scanner input = new Scanner(System.in);
    System.out.println("Enter week number");
    int j = input.nextInt();
    String[] strArray = new String[4];        
    xmlLoader(j, strArray);

    JButton nextButton = new JButton("Next");
    nextButton.setBounds(750, 250, 80, 30);
    nextButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae){
           j++;
           doNext(j, nextButton);
       } 
    });

我无法通过j,因为它不是最终的,我不能改变按钮上的任何内容它是最后的,helpppp!

I cannot pass the j because it's not final, and I can't change anything on the button if it's final, helpppp!

特定错误:从内部类中访问局部变量j;需要被宣布为最终

Specific error: local variable j is accessed from within inner class; needs to be declared final

推荐答案

您可以将 j 定义为字段在外层。

You may define j as a field in outer class.

class Sample{
   private int j;

   void method() {
     ...
     nextButton.setBounds(750, 250, 80, 30);
     nextButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae){
           j++;
           doNext(j, nextButton);
       }
      });
    }
}

这篇关于Java swing-对点击执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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