Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                    
                
        

以下是什么问题?我已经完全按照书中所示实现了适配器类,但是我的编译器不接受他的代码部分:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Eevento{
   public static void main(String[] args){
      JFrame frame = new JFrame("Wumbleton");
      Container content = frame.getContentPane();
      JButton button1 = new JButton("Press this");
      content.add(button1, BorderLayout.NORTH);
      button1.addMouseListener(new MouseAdapter{
                                 public void mousePressed(MouseEvent e){
                                    System.out.println("button 1 has been                pressed");
                                 }
                                });

      frame.setVisible(true);
      frame.setSize(500,500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

最佳答案

在适配器的构造方法之后,您会缺少括号:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Eevento {
   public static void main(String[] args){
      JFrame frame = new JFrame("Wumbleton");
      Container content = frame.getContentPane();
      JButton button1 = new JButton("Press this");
      content.add(button1, BorderLayout.NORTH);
      button1.addMouseListener(new MouseAdapter(){ // parentheses added
          public void mousePressed(MouseEvent e){
               System.out.println("button 1 has been pressed");
          }
      });
      frame.setSize(500,500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

关于java - 事件适配器作为内部类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35924870/

10-14 10:20