gpt4 book ai didi

java - JLabel重叠

转载 作者:行者123 更新时间:2023-11-29 10:17:58 24 4
gpt4 key购买 nike

我已经创建了一个标签,覆盖了它的 paintComponent(用不同的弧形格式绘制它)并将它们放在面板上。一切正常,但位于同一位置的标签(通过 .setlocation)产生了问题。

假设我在同一位置有 3 个不同形状的标签。 A-B-C首先创建 A,然后创建 B,最后创建 c。当我点击 A 时,我的函数绘制 A 并显示“你点击了 A”

但是当我点击 B 或 C 时,它就像我点击了 A 一样。

(我在滚动 Pane 中堆叠的面板中添加标签)

这是我制作标签的代码

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 层(不知道是否可以)。

最佳答案

确保您正在调用 super.paintComponent。这对于确保组件进行绘画校正很重要。

鼠标事件如雨。它们将击中第一个注册接收鼠标事件的组件并停止(就像雨打雨伞一样)。

如果您有重叠的组件,在它们重叠的地方,鼠标事件会转到注册处理它们的第一个组件(在本例中为 A)。

这里有一个简单的例子来说明我的意思......

enter image description here enter image description here

当你将鼠标悬停在每个圆圈上时,它会突出显示,尝试将鼠标悬停在中间......

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();
}
}
}

关于java - JLabel重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12835633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com