2012-01-31 55 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); 
} 

现在你可以使用这里面的信息,这种方法的做多只写标准,但我会让你找出其余。

相关问题