2012-01-31 51 views
0

我想感謝Andrew Thompson幫助我在代碼中實現這一目標。 如何訪問每個單獨按鈕的actionPerformed偵聽器?如何訪問每個單獨的ActionPerformed for按鈕?

代碼應該根據您按下的按鈕來移動屏幕上的「球」。

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

public class Lab2a extends JFrame { 

Lab2a(){ 
    setTitle("Lab 1b - Application #2"); 
    Lab2Panel p = new Lab2Panel(); 
    add(p); 
} 

public static void main(String[] args){ 

Lab2 frame = new Lab2(); 
frame.setTitle("Lab2 Application # 1"); 
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"); 

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

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

leftButton.addActionListener(new Lab2MoveBallListener(canvas)); 
rightButton.addActionListener(new Lab2MoveBallListener(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.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(); 
    } 


} 

class Lab2MoveBallListener implements ActionListener{ 
    private Lab2Button canvas; 

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

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

} 

回答

2

由於您正在使用1個具有2個按鈕的動作監聽器類,因此您必須知道哪個按鈕被按下。你可以嘗試這樣的事情在actionPerformed方法:

public void actionPerformed(ActionEvent e) { 
    if(e.getActionCommand().matches("left")) 
     System.out.print("left button pressed"); 
    else if(e.getActionCommand().matches("right")) 
     System.out.print("right button pressed"); 
} 

另一種方法是做到這一點是這樣做是爲了創建一個匿名類:

buttonLeft.addActionListener(new ActionListener(){ 
     public void actionPerformed(){ 
     //left button code 
     } 
    }); 

    buttonRight.addActionListener(new ActionListener(){ 
    public void actionPerformed(){ 
     //right button code 
    } 
    }); 
+0

這工作,你甚至可以用'如果(e.getActionCommand()。equals(「left」))'如果使用匹配不舒服。 – 2012-01-31 05:56:18

1

在的ActionListener的actionPerformed(...)方法,你可以得到的VIA的動作事件的getActionCommand()方法被按下按鈕的文字。

只是測試它看到的結果:

public void actionPerformed(ActionEvent e){ 
    String actionCommand = e.getActionCommand(); 

    System.out.println("actionCommand is: " + actionCommand); 
} 

現在你可以使用這裏面的信息,這種方法的做多隻寫標準,但我會讓你找出其餘。