나는 그것을 paintComponent
(호가있는 다른 형식으로 그리기)로 덮어 쓰기하여 패널에 배치하여 레이블을 만들었습니다. 모든 것이 정상적으로 작동했지만 같은 위치에있는 라벨 (.setlocation
)이 문제를 일으키고 있습니다.JLabel 겹치기
다른 모양의 동일한 위치에 3 개의 레이블이 있다고 가정 해 보겠습니다. A-B-C A가 먼저 작성된 후 B가 작성되고 마지막으로 c가 작성됩니다. 내가 클릭하면 내 기능은
"youve를이를 클릭"하지만 B 또는 C를 클릭하면 내가 A.
(I가 추가 해요 라벨을 클릭 한 것처럼,이 역할을하는 및 표시 페인트 스크롤 창 적층 패널)에서 다음
내가 만드는 코드는 라벨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 개의 레이어가 필요합니다.
자바 명명 규칙을 배우고 준수하십시오. – kleopatra
은 JPanel에서 어떤 레이아웃을 사용하고 있는지 알려주고, 기본값은 FlowLayout이며 이제는 겹칠 수 있습니다. – Gianmarco
절대 레이아웃을 사용하고 있습니다. – Ozan