我有一个很好的问题,因为我有3类:使图形按钮的JPanel
- 窗框 - 它的一类扩展JFrame的,它会显示内部面板之一;
- WindowPanel - 它是一个扩展JPanel的类。我在那里布置BoxLayout。我想在WindowFrame中的其他面板之间切换;
- GButton - 它的一类像正常的按钮,但我对BG的质感,我想简单的由add(把它们添加到WindowPanel):
public class GButton extends JComponent implements MouseMotionListener, MouseListener{
int x, y , w, h; //the coordinates of button
Graphics2D g2d;
public GButton(int x, int y){
this.x = x; this.y = y;
this.w = 200; this.h = 100;
... //add listeners, loading some information etc
}
@Override
public void paintComponent(Graphics g){
g2d = (Graphics2D) g;
...//loading and draw on g2d
g2d.drawString("asidoaisjdiajsoidj",x,y);
}
//code with using events of mouse and so on
}
当我添加按钮,按钮的纹理是在y下面50px!下一个按钮增加了与y的这种差别。为什么?我知道这是因为我在帮助MouseMove事件中检查它。这不是一个大小不一的图像或类似的东西。为什么它不起作用,并且在其他地方绘制东西?更简单的方法来获得相同的效果?请回答:)
花一些时间来阅读你不必'公众诠释了getX()'和'的getY()'你有办法吗?如果是这样,你可能会在不知不觉中重写JComponent的方法并导致灾难性的结果。 –