匿名内部类 - 非法开始表达

时间:2016-02-17 10:06:05

标签: java user-interface jframe anonymous-inner-class java-threads

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

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)

以下是代码:

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);
   }
 }  
}

2 个答案:

答案 0 :(得分:1)

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

          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方法实际上应该在匿名内部类中,但事实并非如此。

      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();
      }
   }); 

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

答案 1 :(得分:1)

准确地看这些方面:

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();
    }
}

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

当然,这不是你的意思。

当然,您希望在此actionPerformer中实施ActionListener,并且这样做:

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();
      }

});