0

我正在做一个家庭作业,其中一个球使用左、右、上和下按钮在屏幕上移动。此外,使用绿色和红色按钮更改球的颜色。出于某种原因,我的听众给了我错误“Btns.RightListener 无法解析为一种类型”。我不确定为什么。

任何帮助,将不胜感激。

这是我目前所拥有的......

主类:

import javax.swing.*;

@SuppressWarnings("serial")
public class Lab2Main extends JFrame {


        Lab2Main()
    {
        setTitle("Lab 2 - Move The Ball");
        Lab2 p = new Lab2();
        add(p);
    }

    public static void main (String[] args) { 

        Lab2 frame = new Lab2();
        frame.setTitle("Lab 2 - Move The Ball");
        frame.setSize(450, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }   

}

类持有听众:

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

@SuppressWarnings("serial")
public class Lab2 extends JFrame { 

    Btns canvas = new Btns();
    JPanel panel = new JPanel();

    JButton jbtL = new JButton("Left");
    JButton jbtR = new JButton("Right");
    JButton jbtU = new JButton("Up");
    JButton jbtD = new JButton("Down");
    JButton jbtRd = new JButton("Red");
    JButton jbtG = new JButton("Green");

    Lab2()
        {
        setLayout(new BorderLayout());

        panel.add(jbtR);
        panel.add(jbtL);
        panel.add(jbtU);
        panel.add(jbtD);
        panel.add(jbtRd);
        panel.add(jbtG);

        this.add(canvas, BorderLayout.CENTER);
        this.add(panel, BorderLayout.SOUTH);

        jbtL.addActionListener(new Btns.LeftListener(canvas));
        jbtR.addActionListener(new Btns.RightListener(canvas));
        jbtU.addActionListener(new Btns.UpListener(canvas));
        jbtD.addActionListener(new Btns.DownListener(canvas));
        jbtRd.addActionListener(new Btns.RdColorChangeListener(canvas));
        jbtG.addActionListener(new Btns.GColorChangeListener(canvas));
        }
}   

拿着按钮的班级:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class  Btns extends JPanel
{
    int radius = 5;
    int x = -1;
    int y = -1;

        protected void paintComponent(Graphics g) {
            if (x < 0 || y < 0)
            {
                x = getWidth() / 2 -radius;
                y = getHeight() / 2 -radius;
            }
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillOval(5,10,25,25);
    } 
    public void moveLeft()
    { 

             x -= 5; 
             this.repaint(); 
        } 

        public void moveRight()
        { 

            x += 5; 
            this.repaint(); 
        } 
        public void moveUp()
        { 

            y -= 5; 
            this.repaint(); 
        } 

        public void moveDown()
        { 
            y += 5; 
            this.repaint(); 
        } 


        public class LeftListener implements ActionListener
        { 
            private Btns canvas; 

            LeftListener(Btns canvas) 
            { 
              this.canvas = canvas; 
            } 

             public void actionPerformed(ActionEvent e)
            { 
             canvas.moveLeft(); 
            } 


        public class RightListener implements ActionListener
        { 
            private Btns canvas; 

            RightListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

            public void actionPerformed(ActionEvent e)
            { 
              canvas.moveRight(); 
            } 
        } 


         class UpListener implements ActionListener
         { 
            private Btns canvas; 

            UpListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

            public void actionPerformed(ActionEvent e)
            { 
                canvas.moveUp(); 
            } 
        } 



        class DownListener implements ActionListener
        { 
            private Btns canvas; 

            DownListener(Btns canvas) 
            { 
                this.canvas = canvas; 
            } 

            public void actionPerformed(ActionEvent e)
            { 
             canvas.moveDown(); 
            } 
        }

        class RdColorChangeListener implements ActionListener 
            { 
            private Btns canvas; 

            RdColorChangeListener(Btns canvas) { 
                this.canvas = canvas; 
            } 
            public void actionPerformed(ActionEvent e){ 
                canvas.setColor(Color.RED); 
                repaint();
            } 

        class GColorChangeListener implements ActionListener 
            { 
            private Btns canvas; 

            GColorChangeListener(Btns canvas) { 
                this.canvas = canvas; 
            } 
            public void actionPerformed(ActionEvent e){ 
                canvas.setColor(Color.GREEN); 
                repaint();
            } 
       }
    }
  }


        public void setColor(Color red) {
        // TODO Auto-generated method stub

        }
}
4

2 回答 2

2

需要将类Btns.RightListener(实际上是 的所有内部类Btns)声明为static类,以便像您一样创建新实例。只需将static修饰符添加到每个类声明即可。否则,您将需要执行以下操作:

jbtL.addActionListener(canvas.new Btns.LeftListener(canvas));

哦,正如@antlersoft 指出的那样,您需要更正类嵌套。

于 2013-02-05T21:28:07.653 回答
2

您在 Btns.LeftListener 上缺少一个结束 },因此 Eclipse 认为 RightListener 位于 LeftListener 内部。

于 2013-02-05T21:28:19.120 回答