[英]Java. Swing. Absolute position
我需要在窗口中設置組件的位置。 我需要在另一個組件附近的GlassPane
上繪制組件,該組件被單擊。 我傳遞了組件,它將click事件引發給某個管理器,在那里我想得到坐標在哪里畫。
public void mouseClicked(MouseEvent e) {
ApplicationManager.getInstance().drawOnGlassPane((Component e.getSource());
}
public void drawOnGlassPane(final Component caller) {
mainFrame = (JFrame) SwingUtilities.getWindowAncestor(caller);
JPanel glassPane = (JPanel) mainFrame.getGlassPane();
glassPane.setVisible(true);
Point where = caller.getLocationOnScreen();
JButton btn = new JButton("on glass pane");
btn.setBounds((int) where.getX(), (int) (where.getY() + caller.getHeight()), 50, 20);
glassPane.add(btn);
}
}
新組件出現在錯誤的位置。 我怎樣才能設置正確的位置?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.