2013-05-01 66 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);。希望這有助於解決您的問題。