Re: [問題] 請問如何不讓panel上的物件跑到下層
※ 引述《yuntechvb (雲寶寶)》之銘言:
: ※ 引述《conanist (QQ)》之銘言:
: : PO 你的原始碼這樣大家比較好解決
: jPanel1 = new JPanel(){
: public void paint(Graphics g){
: super.paintComponent(g);
: Graphics2D g2d = (Graphics2D) g;
: g2d.scale(0.5, 0.5);
: g2d.drawImage(temp.getImage(), 0, 0, null);
: }};
實際上的問題並不是其他的元件(label, button)跑到 panel 下面,而是被你
不合格的地圖 panel 給犧牲了。你實做的地圖 panel 不是一個正確的 container,
如果你想把此 panel 當作 container 使用,你就要讓他負起身為 container 的
責任。
原本 JPanel 的 paint method 實做是合格的 container 應有的行為,而你實做
的 paint method 是調用了 super.paintComponent 之後繪製地圖底圖。
super.paintComponent 只負責 JPanel 本身應有的外觀(不包括 border 與其內的
child components),所以你實做的地圖元件實際上已不是一個 container,因為
他在繪圖時只顧著自己,不顧 child component(只有 heavy-weight child
component 能夠保持外觀,其餘放進地圖 panel 裡的 light-weight component
都沒有機會呈現自己的外觀)。
或許你只是筆誤而已,你原本想要 override 的是 paintComponent method。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.128.39
※ 編輯: sbrhsieh 來自: 218.173.128.39 (10/30 14:45)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):