Action Listener检测单个事件的多个事件
我设计了一个包含一些按钮的面板。按钮附带一个ActionListener。当我点击那个按钮时,这个ActionListener检测到这个单击的4个事件。而它应该只检测一个。有人知道究竟是什么原因吗?Action Listener检测单个事件的多个事件
public class Buttons extends JPanel
{
private JButton undo=new JButton("Undo");
private JButton replay=new JButton("Replay");
public void paint(Graphics g)
{
super.paint(g);
super.setSize(new Dimension(560,30));
super.add(replay);
super.add(undo);
undo.setBorder(new LineBorder(Color.WHITE,3));
replay.setBorder(new LineBorder(Color.WHITE,3));
undo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Controler.pieces.undo();
Controler.reDraw();
}
});
replay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Dastiii");
}
});
}
}
而这些事件正在这里使用
public void undo()
{
System.out.print(Controler.allMoves.size());
if(Controler.allMoves.size()<=1)
{
init_board();
return;
}
Piece temp[][]=Controler.allMoves.get(Controler.allMoves.size()-2);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
board[i][j].set_name(temp[i][j].get_name());
board[i][j].set_oneWay(temp[i][j].get_oneWay());
}
}
Controler.allMoves.remove(Controler.allMoves.size()-2);
}
您注册您的paint
方法中ActionListener
小号!
让我们即使不用担心的事实,这是联合国推荐的覆盖paint
不要改变或修改组件或任何它的国家在任何paint
方法中的子组件,这些将被调用多次在执行你的应用程序期间。例如,它的不寻常的只是当是可见的主窗口被称为2-4次paint
方法...
public void paint(Graphics g)
{
super.paint(g);
/** All this should be done within the constructor
// If you are using a layout manager, this is pointless, if your not
// then that's another problem
super.setSize(new Dimension(560,30));
super.add(replay);
super.add(undo);
undo.setBorder(new LineBorder(Color.WHITE,3));
replay.setBorder(new LineBorder(Color.WHITE,3));
undo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Controler.pieces.undo();
Controler.reDraw();
}
});
replay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Dastiii");
}
});
**/
}
在看看:
有关如何与画什么更多的细节是在Swing
圣洁的废话我是怎么看的...... – csmckelvey
@MadProgrammer哦......我明白了..我知道了.. !! :) –
@csmckelvey我看起来就对它传递了一下......认为我的潜意识试图保护我...... – MadProgrammer
将您的代码添加到问题中。 –
也许它正在检测像'buttonDown'' buttonPressed''buttonUp'和其他一个完整的点击。 – csmckelvey
@csmckelvey我添加了actionlistener的按钮,** buttonDown **或其他一些链接到我认为的鼠标侦听器。我对吗? –