gpt4 book ai didi

java - MouseListener mouseClicked() 缺少第一个事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:20 25 4
gpt4 key购买 nike

public  class TesterApplication {
static JPanel CenterPanel;
public static void main(String[] args){
/* get image MapImg */
JFrame frame=new JFrame();
CenterPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g){
g.drawImage(MapImg, 0, 0, null);
}
};
CenterPanel.addMouseListener(new LineBuildListener(new TesterApplication()));
frame.getContentPane().add(BorderLayout.CENTER, CenterPanel);
frame.setSize(x, y);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}

现在是内部类

class LineBuildListener implements MouseListener {
TesterApplication TA;
int xFirstClick;
int yFirstClick;
int ClickCounter=0;
int xClick;
int yClick;
LineBuildListener(TesterApplication TA){
this.TA=TA;
}

@Override
public void mouseClicked(MouseEvent e) {
xFirstClick=xClick;
yFirstClick=yClick;
xClick=e.getX();
yClick=e.getY();
TA.CenterPanel.getGraphics().fillOval(xClick, yClick, 10, 10);
if(ClickCounter!=0){
SecondClick();
ClickCounter++;
}else{
ClickCounter++;
}
System.out.println(ClickCounter);
}

public void SecondClick(){
TA.CenterPanel.getGraphics().drawLine(xClick, yClick, xFirstClick,yFirstClick);
}
}

与此同时,我第一次点击时,我的 GUI 闪烁,点击计数器打印我已经点击了一次,但我没有得到我的第一个圆圈。如果我继续点击一切正常,它会打印下一个圆圈,增加计数器并在它们之间画线,所以我不明白为什么第一个圆圈丢失了

最佳答案

看看这个:

 xFirstClick=xClick;
yFirstClick=yClick;
xClick=e.getX();
yClick=e.getY();

xClick和yClick第一次没有初始化

关于java - MouseListener mouseClicked() 缺少第一个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com