Java Swing – perform actions on clicks

I think I've written myself into a corner I'm trying to do this with Java swing

Click the next button to load a new line from the file (through the line index number), and then gray the next button if the date of the line in the file has not yet arrived My problem is 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);
       } 
    });

I can't pass J because it's not final. If it's final, I can't change anything on the button, Helpppp!

Specific error: accessing local variable j from internal class; Final announcement required

Solution

You can define J as a field in an external class

class Sample{
   private int j;

   void method() {
     ...
     nextButton.setBounds(750,30);
     nextButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae){
           j++;
           doNext(j,nextButton);
       }
      });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>