我创建了一个覆盖其paintComponent的标签(用弧线以不同的格式绘制它),并将它们定位在一个面板上。一切都很好,但是位于相同位置的标签(由.setlocation提供)正在制造问题。
假设我在同一个位置有3个形状不同的标签。A-B-C首先创建A,然后创建B,最后创建c。当我单击A时,我的函数绘制A并显示"youve‘on click A“
但当我点击B或C时,它就像我点击了A一样。
(我在滚动窗格中堆叠的面板中添加标签)
下面是我制作标签的代码
for(int i=0;i<rowcount;i++){
arcstart = Rawazimuth(azimuth[i]);
//custom JLabel to paint it like an arc
MyLabel FLabel = new MyLabel(100, 50, (int)arcstart,cellid[i],lon[i],lat[i]);
FLabel.setOpaque(true);
Dimension LabelSize = new Dimension( 100,50);
FLabel.setSize(LabelSize);
//locate the JLabel to labels location. it might be same or not
FLabel.setLocation(lon[i], lat[i]);
MyPanel.add(FLabel);
}这是我的自定义jlabel类
public MyLabel(int W, int H, int start,int outcellid,int lonn, int latt) {
addMouseListener(this);
arcsizeW = W;
arcsizeH = H;
arcstart = start;
cellid = outcellid;
clicked = false;
lon = lonn;
lat = latt;
}
@Override
public void paintComponent(Graphics g){
// true if button is clicked, so paint acordingly
if(clicked ==true){
g.setColor(dummyg.getColor());
}else{
g.setColor(Color.blue);
}
// draw the arc
g.fillArc(0, 0,arcsizeW , arcsizeH, arcstart, 60);
}
//if cell is clicked, change its color to red and print its cellid
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(cellid);
dummyg = this.getGraphics();
dummyg.setColor(Color.red);
this.paintComponent(dummyg);
clicked = true;
}// other listener stuff}那么我该如何防止这种情况呢?我想我可以使用jlayerpane,但我至少需要4层(不知道是否可以)。
发布于 2012-10-20 17:58:02
我已经解决了我的问题。
我发现的真正问题是,Java用它们的x,y位置以及宽度和高度来解释每个组件。因此,即使您的标签不重叠(图形),也可以将它们解释为相同的组件。
为了解决我的问题,我找到了鼠标点击点和组件中心之间的角度,然后计算最近的组件并到达它。
层等在这种情况下不起作用,唯一的另一种方法可能是覆盖实际形状的包含方法。
发布于 2012-10-11 17:00:37
确保您调用的是super.paintComponent。这对于确保组件的绘制更正很重要。
鼠标事件就像下雨一样。它们将点击第一个注册为接收鼠标事件的组件并停止(就像雨打雨伞一样)。
如果您有重叠的组件,则鼠标事件将转到注册为处理它们的第一个组件(在本例中为A)。
这里有一个简单的例子来说明我的意思。


当你将鼠标悬停在每个圆圈上时,它会高亮显示,尝试将鼠标悬停在中间…
public class OverlappingMouseTest {
public static void main(String[] args) {
new OverlappingMouseTest();
}
public OverlappingMouseTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BackgroundPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class BackgroundPane extends JLayeredPane {
private CirclePane redPane;
private CirclePane greenPane;
private CirclePane bluePane;
public BackgroundPane() {
redPane = new CirclePane(Color.RED);
greenPane = new CirclePane(Color.GREEN);
bluePane = new CirclePane(Color.BLUE);
add(redPane);
add(greenPane);
add(bluePane);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void doLayout() {
Dimension size = new Dimension(100, 100);
int width = getWidth() - 1;
int height = getHeight() - 1;
Point p = new Point();
p.x = (width / 2) - 50;
p.y = (height / 2) - 75;
redPane.setBounds(new Rectangle(p, size));
p.x = (width / 2) - 75;
p.y = (height / 2) - 25;
bluePane.setBounds(new Rectangle(p, size));
p.x = (width / 2) - 25;
p.y = (height / 2) - 25;
greenPane.setBounds(new Rectangle(p, size));
}
}
protected class CirclePane extends JPanel {
private boolean mouseHover = false;
public CirclePane(Color background) {
setOpaque(false);
setBackground(background);
addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent me) {
mouseHover = false;
repaint();
}
@Override
public void mouseEntered(MouseEvent me) {
mouseHover = true;
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
Graphics2D g2d = (Graphics2D) grphcs.create();
if (!mouseHover) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
}
g2d.setColor(getBackground());
g2d.fill(new Ellipse2D.Float(0, 0, getWidth() - 1, getHeight() - 1));
g2d.dispose();
}
}
}发布于 2012-10-11 16:57:21
一切正常,但是位于相同位置(由.setlocation提供)的JLabel正在制造问题。
假设我在同一个位置有3个不同形状的Jlabels。A-B-C首先创建A,然后创建B,最后创建c。
有几种树方式可供使用
https://stackoverflow.com/questions/12835633
复制相似问题