关于java:匿名内部类 – 非法启动表达式

anonymous inner class - illegal start of expression

我正在编写一个进度条的代码,需要声明一个匿名的内部类,但是当我这样做时,我会得到这些:

Lab2Part2.java:25:错误:非法启动表达式
public void actionPerformed(ActionEvent e)
^
Lab2Part2.java:25:错误:非法启动表达式
public void actionPerformed(ActionEvent e)
^
Lab2Part2.java:25:错误:';' 预期
public void actionPerformed(ActionEvent e)
^
Lab2Part2.java:25:错误:';' 预期
public void actionPerformed(ActionEvent e)

这是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

    public class Lab2Part2 extends JFrame implements ActionListener
    {  

   private JFrame frame;
   private JButton button;
   private JPanel panel;

   public Lab2Part2()
   {
      setLayout(new GridLayout());
      frame = new JFrame();
      button = new JButton("Let's start this show");
      panel = new JPanel();
      panel.add(button);

      InnerProgress prog1 = new InnerProgress("Progress 1:");
      InnerProgress prog2 = new InnerProgress("Progress 2:");

      button.addActionListener(new ActionListener());

      //this is where it throws errors
      public void actionPerformed(ActionEvent e)
      {  
      Object source = e.getSource();
      if(source == button)
      {
      Thread th1 = new Thread(prog1);
      Thread th2 = new Thread(prog2);
      th1.start();
      th2.start();
      }
   }

  add(panel);
  add(prog1);
  add(prog2);  



  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  pack();
   }

   public static void main(String [] args)
   {
   new Lab2Part2();
   }  

   class InnerProgress extends JPanel implements Runnable
   {
   private String progress;
   private JProgressBar bar;

   public InnerProgress(String _progress)
   {
      progress = _progress;
      add(bar);
   }  

   public void run()
   {  
      System.out.println("We are running:" + progress);
   }
 }  
}


准确地看这些方面:

1
2
3
4
5
6
7
8
9
10
11
12
button.addActionListener(new ActionListener());

//this is where it throws errors
public void actionPerformed(ActionEvent e) {  
    Object source = e.getSource();
    if (source == button) {
        Thread th1 = new Thread(prog1);
        Thread th2 = new Thread(prog2);
        th1.start();
        th2.start();
    }
}

你在这里做的是禁止接口的实例化,然后在另一种方法的主体中创建方法,这也是禁止的。

当然,这不是你想要做的。

当然,你想在这个ActionListener中实现actionPerformer,这样就完成了:

1
2
3
4
5
6
7
8
9
10
11
12
button.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {  
      Object source = e.getSource();
      if(source == button) {
          Thread th1 = new Thread(prog1);
          Thread th2 = new Thread(prog2);
          th1.start();
          th2.start();
      }

});


你的代码混淆了,最后是方法内部的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
          button.addActionListener(new ActionListener());

          //this is where it throws errors
          public void actionPerformed(ActionEvent e)
          {  
          Object source = e.getSource();
          if(source == button)
          {
          Thread th1 = new Thread(prog1);
          Thread th2 = new Thread(prog2);
          th1.start();
          th2.start();
          }
       }

整个actionPerformed方法实际上应该是一个匿名的内部类,但事实并非如此。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      button.addActionListener(new ActionListener(){

      //this is where it throws errors
      public void actionPerformed(ActionEvent e)
      {  
      Object source = e.getSource();
      if(source == button)
      {
      Thread th1 = new Thread(prog1);
      Thread th2 = new Thread(prog2);
      th1.start();
      th2.start();
      }
   });

将您的方法保留在匿名实现中。