Java mouseClicked并发症

时间:2014-06-08 13:07:42

标签: java mouselistener

代码有效,我的不好。但我仍然愿意接受如何改进或使代码更优雅的建议。

我创建了这个布局,并希望每当用户点击白色区域时就能画一个圆圈。

Couldn't post an image, so here is the link

白色区域基本上是一个矩形。但是我的代码不起作用,它只是不响应鼠标点击。当我试图查看它是否响应mouseDragged时,它工作得很好,但这不是我需要的。

这是我的代码,一些“测试”被放置为/ comments /但它们都没有按预期工作。 我非常感谢你的帮助。这是我的代码:

import java.awt.*;

import java.awt.Graphics; import javax.swing。*;

public class CitiesMapPanel extends JPanel implements MouseListener {

private JButton cmdAddWay, cmdFindPath, cmdClearMap, cmdClearPath;
private JLabel lblFrom, lblTo;
private JTextField txtFrom, txtTo;

public CitiesMapPanel() {

    cmdAddWay = new JButton("Add Way");
    cmdFindPath = new JButton("Find Path");
    cmdClearMap = new JButton("Clear Map");
    cmdClearPath = new JButton("Clear Path");
    lblFrom = new JLabel("From");
    lblTo = new JLabel("To");
    txtFrom = new JTextField(6);
    txtTo = new JTextField(6);

    this.addMouseListener(this);
    setLayout(new BorderLayout());      
    add(buildGui(), BorderLayout.SOUTH);

}




private JPanel buildGui() {
    JPanel buttonsBar = new JPanel();
    //The "south" of the BorderLayout consist of a (2,4) GridLayout.

    buttonsBar.setLayout(new GridLayout(2,4));
    buttonsBar.add(lblFrom);
    buttonsBar. add(txtFrom);
    buttonsBar.add(lblTo);
    buttonsBar.add(txtTo);
    buttonsBar.add(cmdAddWay);
    buttonsBar.add(cmdFindPath);
    buttonsBar.add(cmdClearMap);
    buttonsBar.add(cmdClearPath);
    return buttonsBar;
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(0, 0, this.getSize().height, this.getSize().width);
}

public static void main(String[] args) {
    JFrame frame = new JFrame("layout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(530, 550);
    CitiesMapPanel gui = new CitiesMapPanel();
    frame.add(gui);
    frame.setVisible(true);
}

/*abstract private class MyMouseListner implements MouseListener{
public void mouseClicked(MouseEvent e){
    int x = e.getX();
    int y = e.getY();
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.fillOval(x,y,15,15);          
    }
}*/

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    Graphics g = getGraphics();
    g.setColor(Color.black);
    g.fillOval(x,y,15,15);   
    System.out.println("test");

}
@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:1)

点击监听器不是问题。你的绘画方法是完全错误的。你不能做getGraphic,在上面画画,并期望呈现结果。在Swing(AWT)中,工作根本不同。您需要创建一个您绘制的屏幕外图像,然后在paintComponent方法中显示在屏幕上,或者您需要跟踪要在数据结构中绘制的对象并在{ {1}}方法。您可以通过调用paintComponent在单击侦听器中触发重绘,以便UI子系统知道需要重新绘制组件的已更改状态。

详细了解Swing painting tutorial中的基本知识。