1

私はJavaを学んでいます.MouseListenerの問題を解決しているときに、クラス宣言でエラーが発生しました.私が知っているすべてのことを試してみてください. 私によると、すべてのコーディングを正しく行ったので、IDE にもコードを貼り付けましたが、同じエラーが発生しました。

ありがとう

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

       public class Sub extends JFrame      
    {
private JPanel mousepanel;
private JLabel statusbar;

public Sub()
{
    super("Mouse Events");
    // we didnt have a FlowLayout as we had in previous programs

    mousepanel = new JPanel();
    mousepanel.setBackground(Color.WHITE);
    add(mousepanel, BorderLayout.CENTER); //BorderLayout used instead of FlowLayout and it will place it in the center of the window.

    statusbar = new JLabel("Default");
    add(statusbar, BorderLayout.SOUTH); // same as above

    thehandler handler = new thehandler();
    mousepanel.addMouseListener(handler);
    mousepanel.addMouseMotionListener(handler);

            private class thehandler implements MouseListener, MouseMotionListener
    {
        public void mouseClicked(MouseEvent event)
        {
            statusbar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
        }
        public void mousePressed(MouseEvent event)
        {
            statusbar.setText("You press down the mouse.");
        }
        public void mouseReleased(MouseEvent event)
        {
            statusbar.setText("You released the mouse.");
        }
        public void mouseEntered(MouseEvent event)
        {
            statusbar.setText("You enetered the mouse panel area.");
            mousepanel.setBackground(Color.PINK);
        }
        public void mouseExited(MouseEvent event)
        {
            statusbar.setText("The mouse has left the window.");
            mousepanel.setBackground(Color.WHITE);
        }

        //these aremouse motion events

        public void mouseDragged(MouseEvent event)
        {
            statusbar.setText("Your are dragging the mouse.");
        }
        public void mouseMoved(MouseEvent event)
        {
            statusbar.setText("You moded the mouse.");
        }
    }
}
  }
4

3 に答える 3

1

クラス定義をコンストラクターthehandlerのスコープ外に移動する必要があります。Sub

補足:クラス名は大文字で始まります。

于 2012-12-12T21:16:11.290 に答える
0

中かっこを確認し、IDEを使用すると、エラーをより簡単に修正できます。

于 2012-12-18T17:50:46.083 に答える
0

次のように、クラス定義をコンストラクタの外に移動します。

public Sub() {
    // ... rest of code
}

// ... rest of code

private class thehandler implements MouseListener, MouseMotionListener {
    // ... rest of code
}
于 2012-12-12T21:18:04.787 に答える