java - 使用 Action 监听器实现颜色变化

标签 java swing colors awt actionlistener

我有一个作业,要求球根据用户单击的按钮在屏幕上移动,并通过单击另一个按钮让球在红色和绿色之间交替。一切正常,但颜色发生了变化。我有一个监听器和类对按钮单击使用react,但我似乎没有得到任何改变。有没有更好/更简单的方法来实现这一点?

预先感谢您的帮助!

我有代码:

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();
    }
}

按钮移动监听器类代码:

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

class Lab2MoveBallListener extends Lab2Button implements ActionListener { 

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

好的,更改此代码:

public void colorChange(){
            this.repaint();
        }

对此,编译时出现错误:错误:找不到符号 if (g.getColor() = Color.RED){

public void colorChange(){

            if (g.getColor() = Color.RED){
                g.setColor(Color.GREEN);
            }
            else{
                g.setColor(Color.RED);
            }

            this.repaint();
        }

最佳答案

看看如何使用 JColorChooser 。它可以根据需要设置球的颜色。请参阅How to Use Color Choosers

在这里,您对颜色进行了硬编码,因此无法修改球的颜色。使用类成员 Color 变量并从 getColor 分配它.

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

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

关于java - 使用 Action 监听器实现颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16311708/

相关文章:

c++ - 用于选择颜色的轨迹栏。使用C++、opencv

java - Tomcat context.xml 的 Netbeans 问题

java - 使用 GridLayout 垂直对齐 jpanels 的组件

c# - .NET 随机 RGB 颜色

java - Swing - 避免不同行之间的行填充

java - 如何将数据库中选定的行放入 jTable 中?

python - 使用 Imagemagick 查找相似颜色的区域

java - 尝试在 Android 上读取 SMS/MMS 并获取 java.lang.NullPointerException

java - unicode 字符串的 gson 序列化不起作用

java - '+i'在Java中是什么意思?