簡體   English   中英

如何使用 GridBagLayout 刪除 JFrame 中的多余空格?

[英]How to remove extra spaces in JFrame using GridBagLayout?

當我在我的框架中插入任何按鈕或任何組件並將組件gridxgridy為值0 ,它不會將該組件放在框架的開頭,而是將該組件放在中心的某個位置。

如何刪除空格並將我的組件放在框架的最開始?

以下是代碼:

         JFrame frame = new JFrame("Grigbag layout");

         JPanel pane = new JPanel(new GridBagLayout());

         GridBagConstraints c = new GridBagConstraints();

         JButton button = new JButton("First button");

         c.gridx = 0;
         c.gridy = 0;
         pane.add(button, c);

在此處輸入圖片說明

我建議您使用 Flow Layout 並添加此代碼

frame.pack();

正如上面安德魯湯普森所提到的

更多信息: https : //docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

使用frame.pack()方法並刪除frame.setSize(); 方法。

frame 使用默認布局。 用類似的東西設置框架的布局

frame.setLayout(new FlowLayout(FlowLayout.LEFT));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM