问题描述
这是一个后续行动,我的$p$pvious题。我已经简化事情,尽我所能,它仍然无法正常工作!虽然好事我得到周围使用的getGraphics()
。
在什么位置出了问题的详细解释大规模AP preciated。我怀疑的是,什么是错的我与这里使用 addMouseListener将()
法的方式。
编辑完全重写了code。仍不能正常工作,虽然。
进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口java.awt.event.MouseMotionListener;进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
公共类RunClass { 静态MainClass1 INST1 =新MainClass1(); 公共静态无效的主要(字符串ARGS []){ JFrame的帧1 =新的JFrame();
frame1.add(INST1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle(NewPaintToolbox);
frame1.setSize(200,200);
frame1.setLocationRelativeTo(NULL);
frame1.setVisible(真);
}
}类MainClass1继承JPanel实现的MouseListener,{的MouseMotionListener INT XVAR = 30;
INT yvar = 30;
//静态PaintClass22 INST1 =新PaintClass22();
@覆盖
公共无效的mouseClicked(的MouseEvent为arg0){
// TODO自动生成方法存根
XVAR = arg0.getX();
yvar = arg0.getY();
重绘(XVAR,yvar,10,10); } @覆盖
公共无效的mouseEntered(的MouseEvent为arg0){
// TODO自动生成方法存根 } @覆盖
公共无效的mouseExited(的MouseEvent为arg0){
// TODO自动生成方法存根 } @覆盖
公共无效鼠标pressed(的MouseEvent为arg0){
// TODO自动生成方法存根 } @覆盖
公共无效的mouseReleased(的MouseEvent E){
// TODO自动生成方法存根 } @覆盖
公共无效的mouseDragged(的MouseEvent为arg0){
// TODO自动生成方法存根
XVAR = arg0.getX();
yvar = arg0.getY();
重绘(XVAR,yvar,10,10); } @覆盖
公共无效的mouseMoved(的MouseEvent为arg0){
// TODO自动生成方法存根 }
@覆盖
公共无效的paintComponent(图形G){ super.paintComponent方法(G);
g.setColor(Color.RED);
g.fillRect(XVAR,yvar,10,10); }}
您必须添加的MouseListener到面板上。这并不在默认情况下发生,因为你可能已经预期; - )
MainClass1(){
addMouseListener将(本);
}
BTW:我们不推荐暴露公共API只是为了内部使用。因此,与其让面板实现的MouseListener(这就加强了公开曝光),让面板创建和使用的MouseListener:
私人的MouseListener MouseListener的;
MainClass1(){
的MouseListener = createMouseListener();
addMouseListener将(MouseListener的);
}保护的MouseListener createMouseListener(){
MouseListener的L =新的MouseListener(){ }
返回升;
}
BTW 2:呼吁面积有限重绘不正是你想要的 - 它的暂时的添加正方形画,每当整柜粉刷他们丢失((?)与的getGraphics效果相同)。根据你真正想要的,
- 在最近点击位置画一个正方形:调用重绘()
- 在不断点击所有地点漆广场:存储在列表中的位置并实现重绘遍历该列表。在这里,您可以致电与参数重绘,但何必呢?
This is a follow-up to my previous question. I've simplified things as much as I could, and it still doesn't work! Although the good thing I got around using getGraphics()
.
A detailed explanation on what goes wrong here is massively appreciated. My suspicion is that something's wrong with the the way I used addMouseListener()
method here.
EDIT completely rewrote the code. Still does not work properly though.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RunClass{
static MainClass1 inst1 = new MainClass1();
public static void main(String args[]){
JFrame frame1 = new JFrame();
frame1.add(inst1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("NewPaintToolbox");
frame1.setSize(200, 200);
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
}
}
class MainClass1 extends JPanel implements MouseListener, MouseMotionListener{
int xvar=30;
int yvar=30;
//static PaintClass22 inst1 = new PaintClass22();
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
xvar = arg0.getX();
yvar = arg0.getY();
repaint(xvar,yvar,10,10);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
xvar = arg0.getX();
yvar = arg0.getY();
repaint(xvar,yvar,10,10);
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(xvar, yvar, 10, 10);
}
}
You must add the mouseListener to the panel. That doesn't happen by default as you might have expected ;-)
MainClass1() {
addMouseListener(this);
}
BTW: it's not recommended to expose public api that's only meant to be used internally. So instead of letting the panel implement MouseListener (which enforces the public exposure), let the panel create and use a MouseListener:
private MouseListener mouseListener;
MainClass1() {
mouseListener = createMouseListener();
addMouseListener(mouseListener);
}
protected MouseListener createMouseListener() {
MouseListener l = new MouseListener() {
}
return l;
}
BTW 2: calling the repaint on the limited area isn't exactly what you want (?) - it temporarily adds the squares to the painting, they are lost whenever the whole panel is repainted (same effect as with getGraphics). Depending on what you really want,
- paint a single square at the most recently clicked position: call repaint()
- paint squares at all locations ever clicked: store the locations in a list and implement repaint to loop over that list. Here you may call the repaint with parameters, but why bother?
这篇关于使用addMouseListener将()和的paintComponent()进行的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!