Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用操作侦听器实现颜色更改_Java_Swing_Colors_Awt_Actionlistener - Fatal编程技术网

Java 使用操作侦听器实现颜色更改

Java 使用操作侦听器实现颜色更改,java,swing,colors,awt,actionlistener,Java,Swing,Colors,Awt,Actionlistener,我有一个任务,要求一个球在屏幕上移动,根据用户点击的按钮,球在红色和绿色之间交替,点击另一个按钮。除了颜色变化外,一切都正常。我有一个听者和一个班级对按钮的点击做出反应,但我似乎没有得到任何改变。有没有更好/更简单的方法来实现这一点 提前感谢您的帮助 我有: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Lab2b extends JFrame { Lab2b(){ set

我有一个任务,要求一个球在屏幕上移动,根据用户点击的按钮,球在红色和绿色之间交替,点击另一个按钮。除了颜色变化外,一切都正常。我有一个听者和一个班级对按钮的点击做出反应,但我似乎没有得到任何改变。有没有更好/更简单的方法来实现这一点

提前感谢您的帮助

我有:

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

public class Lab2b extends JFrame {

Lab2b(){
    setTitle("Lab 2");
    Lab2Panel p = new Lab2Panel();
    add(p);
}

public static void main(String[] args){

    Lab2b frame = new Lab2b();
    frame.setTitle("Lab 2 - Ball Mover ");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);
    }

}

class Lab2Panel extends JPanel{
Lab2Button canvas = new Lab2Button();
JPanel panel = new JPanel();

Lab2Panel () {

    setLayout(new BorderLayout());

    JButton leftButton = new JButton("left");
    JButton rightButton = new JButton("right");
    JButton upButton = new JButton("up");
    JButton downButton = new JButton("down");
    JButton colorButton = new JButton("Change Color");

    panel.add(leftButton);
    panel.add(rightButton);
    panel.add(upButton);
    panel.add(downButton);
    panel.add(colorButton);

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

    leftButton.addActionListener(new LeftListener(canvas));
    rightButton.addActionListener(new RightListener(canvas));
    upButton.addActionListener(new UpListener(canvas));
    downButton.addActionListener(new DownListener(canvas));
    colorButton.addActionListener(new ColorChangeListener(canvas));
}


}

class Lab2Button 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.drawOval(x,y, 2 * radius, 2 * radius);


}

        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 void colorChange(){
            this.repaint();
        }



}

class LeftListener implements ActionListener{
    private Lab2Button canvas;

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

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

class RightListener implements ActionListener{
    private Lab2Button canvas;

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

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


class UpListener implements ActionListener{
    private Lab2Button canvas;

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

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



class DownListener implements ActionListener{
    private Lab2Button canvas;

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

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

class ColorChangeListener implements ActionListener {
    private Lab2Button canvas;

    ColorChangeListener(Lab2Button canvas) {
        this.canvas = canvas;
    }
    public void actionPerformed(ActionEvent e){
        canvas.colorChange();
    }
}
好的,更改了此代码:

public void colorChange(){
            this.repaint();
        }
对于此错误,它在编译时出错:错误:找不到符号 if(g.getColor()=Color.RED){

请查看使用。它可以根据需要设置球的颜色。请参阅

在这里,您硬编码了颜色,因此无法修改球的颜色。请使用类成员
color
变量并从中分配它

旁白:请记住在调用
drawOval
之前设置颜色:

g.setColor(ballColor);
g.drawOval(x, y, 2 * radius, 2 * radius);
请查看使用。它可以根据需要设置球的颜色。请参阅

在这里,您硬编码了颜色,因此无法修改球的颜色。请使用类成员
color
变量并从中分配它

旁白:请记住在调用
drawOval
之前设置颜色:

g.setColor(ballColor);
g.drawOval(x, y, 2 * radius, 2 * radius);

调用方法
colorChange()时
你从来不说要改变颜色,你只需要重新绘制屏幕。你需要在某个地方改变颜色。要做到这一点,我需要在颜色按钮的ActionPerformed方法中使用一个颜色变量和if语句。if将具有布尔值,如果为真,将颜色变量设置为该颜色,否则将其设置为其他颜色。现在,在
paintComponent()
中使用
g.setColor(Color.RED);
而不是
g.setColor(colorVariable);
。希望这有助于解决您的问题。

当您调用方法
colorChange()
你从来不说要改变颜色,你只需要重新绘制屏幕。你需要在某个地方改变颜色。要做到这一点,我需要在颜色按钮的ActionPerformed方法中使用一个颜色变量和if语句。if将具有布尔值,如果为真,将颜色变量设置为该颜色,否则将其设置为其他颜色。现在,在
paintComponent()
中使用
g.setColor(Color.RED);
而不是
g.setColor(colorVariable);
。希望这能帮助您解决问题

g.setColor(ballColor);
g.drawOval(x, y, 2 * radius, 2 * radius);