2013-05-01 27 views
0

我有一项任务,要求球根据用户点击的按钮在屏幕上移动,并通过单击另一个按钮让球在红色和绿色之间切换。这一切都有效,只是颜色的变化。我有一个听众和类对按钮点击作出反应,但我似乎没有得到改变。有没有更好/更简单的方法来实现这一目标?使用动作监听器实现颜色更改

在此先感谢您的帮助!

代码,我有:

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

回答

2

看看使用JColorChooser。它可以根据需要设置球的颜色。见How to Use Color Choosers

在这里你已经硬编码的颜色,使其无法修改球的颜色。使用类成员Color变量并从getColor分配它。

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

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

当你打电话给你的方法colorChange()你从来没有说要改变颜色,你只要重新粉刷屏幕。您需要在某处更改颜色。要做到这一点,我会在颜色按钮的ActionPerformed方法中有一个颜色变量和if语句。如果会有布尔值,如果它是真的,则将颜色变量设置为等于该颜色,否则将其设置为等于另一种颜色。现在,您的paintComponent()不是g.setColor(Color.RED);,而是g.setColor(colorVariable);。希望这有助于解决您的问题。