2
我有一個簡單的問題,我想,你應該知道如何解決.. 我想要做到這一點: 我有一個JFrame。 然後我創建一個JPanel並將其佈局設置爲GridLayout(5,5) Finnaly我將此JPanel添加到JFrame的容器中。JFrame容器中JPanel的寬度和高度爲0,爲什麼?
當我試圖獲得我的面板的寬度它不應該給我0對嗎?我做到這一點:System.out.println(mypanel.getSize()。getWidth());它說這是零:c 爲什麼? 我想知道的大小,所以我可以每5分和知道多少畫爲每個標籤,這將是標籤的格.. 非常感謝提前...
public janela(){
window = new JFrame("teste");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
contentor = window.getContentPane();
interior = new JPanel();
interior.setLayout(new GridLayout(5,5));
contentor.add(interior);
System.out.println(interior.getSize().getWidth()); //it says 0 ?!?! why??
}
非常感謝提前球員!!!!
感謝您的快速回復!但看起來我嘗試在我的標籤之前添加到面板,然後嘗試獲得尺寸..但仍然是零.. 看我的代碼在這裏隊友:http://pastebin.com/FEtFnK9r 非常感謝;) – TiagoM
doLayout僅在窗口可見時調用。除非我錯過了它,否則確保在檢查大小之前調用setVisible(true)。 – MeBigFatGuy
固定感謝隊友; D – TiagoM