2015-12-15 184 views
1

這裏是我的類奇怪的JPanel畫

public class Grid extends JPanel{ 
    public Grid(Dimension dim){ 
     this.dim = dim; 
     step = 0.02; 
     this.setSize(dim); 
     this.setLocation(0, 0); 
    } 

    @Override 
    public void paint(Graphics g){ 
     for(int i=-1; i<this.getHeight(); i+=this.getHeight()*step){ 
      g.drawLine(0, i, this.getWidth(), i); 
     } 
     for(int i=-1; i<this.getWidth(); i+=this.getWidth()*step){ 
      g.drawLine(i, 0, i, this.getHeight()); 
     } 
    } 
} 

(類變量的我跳過了定義)

我在另一個JPanel中添加這個類像這樣

Grid grid = new Grid(dim); 
parentPanel.add(grid); 

默認情況下,網格被隱藏(grid.hide();),所以我的jFrame看起來像這樣: enter image description here

其中「藍色」區域是父面板,「橙色」區域是帶按鈕的JPanel。問題是,當我按下網格按鈕,我得到以下結果:enter image description here

其中網格線是好的,但我也得到這個橙色框,我不知道爲什麼有。

任何想法?

+0

什麼是這些按鍵在頂部內以下行?你確定你沒有在面板上添加一個按鈕嗎?我認爲你發佈的代碼太少,無法提供幫助。 – mxb

+0

他們是我的程序的隨機按鈕,是的我沒有添加任何按鈕的面板(忘了提及,我可以添加其他JPanels,以及可以在面板上移動鼠標,如果我將其中的一個移動到面板上奇怪的橙色框,它會消失,因爲它正在被擦除) –

+0

也許它需要在'public void paint(Graphics g)'''中調用'super.paint()'? – mxb

回答

0

我不得不添加我的構造

this.setOpaque(false);