有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java匿名内部类非法启动表达式

我正在为进度条编写代码,需要声明一个匿名的内部类,但当我这样做时,我得到了以下信息:

第二部分。java:25:错误:表达式的开头非法 已执行的公共无效行动(行动事件e) ^ 第二部分。java:25:错误:表达式的开头非法 已执行的公共无效行动(行动事件e) ^ 第二部分。java:25:错误:';'预期 已执行的公共无效行动(行动事件e) ^ 第二部分。java:25:错误:';'预期 已执行的公共无效行动(行动事件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) 个答案

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

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

    将方法保留在匿名实现中

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

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

    当然,那不是你想做的

    当然,您希望在这个ActionListener中实现actionPerformer,这就是这样做的:

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