MouseMotion Listener无法正常工作

时间:2014-09-04 18:20:19

标签: java swing mouselistener mousemotionlistener

我正在编写一段代码来添加和定义一个mousemotion监听器,但编译器会将错误报告给以下代码:“类CustomListener是public,应该在名为CustomListener.java的文件中声明 公共类CustomListener实现MouseMotionListener {        ^ 1错误 “ 我不知道这是什么意思。

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


public class TetrisGUI implements ActionListener{

JButton quit = new JButton();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
JPanel mainArea = new JPanel();

public static void main(String[] args) {

    TetrisGUI gui = new TetrisGUI();
    gui.go();
}

public void go() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    quit = new JButton("QUIT");
    label1 = new JLabel("Level:        1");
    label2 = new JLabel("Lines:        0");
    label3 = new JLabel("Score:        0");

    quit.addActionListener(this);
    mainArea.addMouseMotionListener(new CustomListener(){});

    MyDrawPanel drawPanel = new MyDrawPanel();

    frame.getContentPane().add(quit);
    frame.getContentPane().add(label1);
    frame.getContentPane().add(label2);
    frame.getContentPane().add(label3);
    frame.getContentPane().add(mainArea);
    frame.getContentPane().add(BorderLayout.CENTER,drawPanel);

    label1.setSize(100,200);
    label1.setLocation(310,60);
    label2.setSize(100,200);
    label2.setLocation(310,110);
    label3.setSize(100,200);
    label3.setLocation(310,160);
    mainArea.setSize(250,500);
    mainArea.setLocation(20,50);
    quit.setSize(100,40);
    quit.setLocation(300,310);
    frame.setSize(400,500);
    frame.setVisible(true);
    mainArea.setBackground(new Color(0,0,0,0));
    mainArea.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
    System.exit(0);
}


}

class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
    g.setColor(Color.black);
    g.drawRect(20,50,250,500);//Main Area
    g.drawRect(300,50,100,70);//NextShape

    g.drawRect(145,80,25,25);//Tian Block
    g.drawRect(145,105,25,25);
    g.drawRect(120,80,25,25);
    g.drawRect(120,105,25,25);
    Color deepGreen = new Color(51,138,52);
    g.setColor(deepGreen); 
    g.fillRect(121,81,24,24);//Tian Block
    g.fillRect(121,106,24,24);
    g.fillRect(146,81,24,24);
    g.fillRect(146,106,24,24);

    g.setColor(Color.black);
    g.drawRect(245,525,25,25);//Right bottom
    g.drawRect(220,525,25,25);
    g.drawRect(195,525,25,25);
    g.drawRect(170,525,25,25);
    g.drawRect(245,500,25,25);
    g.drawRect(245,475,25,25);
    g.drawRect(220,500,25,25);
    g.drawRect(195,500,25,25);
    Color deepBlue = new Color(15,66,148);
    g.setColor(deepBlue);
    g.fillRect(246,526,24,24);
    g.fillRect(221,526,24,24);
    g.fillRect(246,501,24,24);
    g.fillRect(246,476,24,24);
    Color brightYellow = new Color(252,238,33);
    g.setColor(brightYellow);
    g.fillRect(196,526,24,24);
    g.fillRect(171,526,24,24);
    g.fillRect(196,501,24,24);
    g.fillRect(221,501,24,24);

    //NextShape Blocks
    g.setColor(Color.black);
    g.drawRect(360,60,25,25);
    g.drawRect(335,85,25,25);
    g.drawRect(360,85,25,25);
    g.drawRect(310,85,25,25);
    Color deepRed = new Color(227,23,36);
    g.setColor(deepRed);
    g.fillRect(361,61,24,24);
    g.fillRect(336,86,24,24);
    g.fillRect(361,86,24,24);
    g.fillRect(311,86,24,24);
}
}

public class CustomListener implements MouseMotionListener {
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){

}
public void mouseExited(MouseEvent me){}
}

1 个答案:

答案 0 :(得分:3)

在java中,每个公共类都必须在一个单独的文件中定义。移动班级' CustomListener'到具有相同名称的其他文件或只删除公共修饰符。