所以我有一个小程序,它将显示 2 个 JFrame,一个带有绘制形状的脸部,另一个带有一些编辑选项。每当我运行程序时,它都会显示两个框架,但由于某种原因,它会显示面部框架上工具栏框架中的按钮。它们不是活动按钮,基本上只是它们的图像。这是一个截图:http: //i1318.photobucket.com/albums/t659/brianbolnick1/scrnshot_zps524c99ee.png)
我试图让我的主要内容尽可能简单:
//draw face panel
Face face = new Face();
JFrame frame = new JFrame();
frame.add(face);
frame.setSize(600,400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//draw toolbar panel
JFrame frame1 = new FaceClass ();
frame1.setTitle("Toolbar");
frame1.setSize(200,150);
frame1.setLocation(200,100);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.setVisible(true);
从我所见,我认为没有任何问题。任何人都可以看到任何可能有问题的地方吗?或者问题可能出在其他地方(“Face”类的构造函数或paintComponent)?请帮忙!我一直在研究这个问题,但似乎无法弄清楚......如果您需要查看更多代码,请询问,我不想在墙上贴上代码。
这是面部类的大部分内容:
public Face () {
//register mouse click activity
addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed (MouseEvent event) {
selectShapeUnder(event.getX(), event.getY());
repaint();
}
});
}
protected void paintComponent (Graphics g) {
Graphics2D graphics = (Graphics2D)g;
graphics.setColor((selected == face) ? Color.CYAN : Color.GREEN);
graphics.fill(face);
graphics.setColor((selected == mouth) ? Color.YELLOW : Color.RED);
graphics.fill(mouth);
graphics.setColor((selected == eyeLeft || selected == eyeRight) ? Color.RED
: Color.WHITE);
graphics.fill(eyeLeft);
graphics.fill(eyeRight);
graphics.setColor(Color.BLACK);
graphics.fill(pupilLeft);
graphics.fill(pupilRight);
g.drawLine(220, 185, 270, 185);
g.drawLine(220, 185, 260, 130);
repaint();
}//end pC
public void selectShapeUnder (int x, int y) {
Shape oldSelected = selected;
if (eyeLeft.contains(x, y)){
selected = eyeLeft;
}//end if
else if (eyeRight.contains(x, y)){
selected = eyeRight;
}//end else if
else if (mouth.contains(x, y)){
selected = mouth;
}//end else if
else if (face.contains(x, y)) {
selected = face;
}//end else if
else
selected = null;
if (selected != oldSelected)
repaint();
}//end selectShapeUnder