當前位置:編程學習大全網 - 源碼下載 - 如何在java gui中用jpanel實現組件的絕對定位

如何在java gui中用jpanel實現組件的絕對定位

相對定位是使用布局管理器GridBagLayout()?實現GridBagLayout?布局是否基於GridBagConstraints()?要設置的GridBagConstraints主要有八個重要參數需要掌握。

格裏德克斯,格裏迪?——?設置組件的位置,

Gridx設置為GridBagConstraints。相對意味著該組件位於先前添加的組件的右側。

Gridy設置為GridBagConstraints。相對意味著該組件低於先前添加的組件。

建議定義Gridx和Gridy的位置,以便將來維護。Gridx = 0和gridy = 0放在0行0列。

grid width grid height?——?用於設置構件所占的單位長度和高度。默認值都是1。

您可以使用GridBagConstraints。REMAINDER常量,這意味著該組件是該行或該列中的最後壹個組件,它將占用所有剩余空間。

weightx,有分量?——?用於設置窗口放大時各個組件的放大比例。

當數字較大時,意味著組件可以獲得更多的空間,默認值為0。

主播?——?當組件空間大於組件本身時,組件放在哪裏。

?有中心(默認)、北方、東北、東方、東南、西方和西北的選擇。

insets?——?設置組件之間的間距。

?它有四個參數,分別是上、左、下、右,默認值為(0,0,0,0)。

ipadx,ipady?——?設置組件間距,默認值為0。

GridBagLayout中的所有設置都必須通過GridBagConstraints,所以當我們設置GridBagConstraints的所有參數時,

之後,必須為GridBagLayout創建壹個GridBagConstraints對象以供使用。

構造者:

創建壹個新的GridBagLayout管理器。

GridBagConstraints()創建壹個新的GridBagConstraints對象。

GridBagConstraints(int?gridx,int?格裏迪,

int?gridwidth,int?網格高度,

雙倍?weightx,double?重量級的,

int?主播,int?填充,?Insets?插圖,

int?ipadx,int?Ipady)創建壹個新的GridBagConstraints對象並指定其參數值。

以下是我的日常工作:

實現

進口?javax . swing . *;

進口?Java . util . *;

進口?Java . awt . *;

公共?班級?舉例?延伸?JFrame{

私人?作廢?makeComponent(GridBagLayout?gbLaout,GridBagConstraints?約束,組件?組件)

{

gbl out . set constraints(component,約束);

添加(組件);

}

公共?靜電?作廢?main(字符串?args[])?{

舉例?exp=new?示例();

GridBagLayout?gridbag?=?新的?GridBagLayout();

GridBagConstraints?c?=?新的?GridBagConstraints();

exp . set layout(grid bag);

JButton?JB 1 =新?JButton(" JButton 1 ");

c . gridx = 0;

c . gridy = 0;

c . grid width = 1;

c . grid height = 1;

c . weightx = 1;

c . weighty = 0;?

c.anchor=GridBagConstraints。居中;

c.fill=GridBagConstraints。水平;

c.insets=new?Insets(0,0,0,0);

c . ipadx = 0;

c . ipady = 0;

exp.makeComponent(gridbag,c,JB 1);

JButton?jb2 =新?JButton(" JButton 2 ");

c . gridx = 1;

exp.makeComponent(gridbag,c,jb2);

JButton?jb3 =新?JButton(" JButton 2 ");

c . gridx = 2;

exp.makeComponent(gridbag,c,jb3);

JButton?jb4 =新?JButton(" JButton 2 ");

c . gridx = 3;

exp.makeComponent(gridbag,c,jb4);

JButton?jb5 =新?JButton(" JButton 5 ");

c . gridx = 0;

c . gridy = 1;

c . grid height = 1;

c . grid width = 4;

exp.makeComponent(gridbag,c,jb5);

JButton?jb6 =新?JButton(" JButton 6 ");

c . gridx = 0;

c . gridy = 2;

c . grid width = 3;

exp.makeComponent(gridbag,c,jb6);

JButton?jb7 =新?JButton(" JButton 7 ");

c . gridx = 3;

c . gridy = 2;

c . grid width = 1;

exp.makeComponent(gridbag,c,jb7);

JButton?jb8 =新?JButton(" JButton 8 ");

c . gridx = 0;

c . gridy = 3;

c . grid width = 1;

c . grid height = 2;

c.fill=GridBagConstraints。兩者都有;

exp.makeComponent(gridbag,c,jb8);

JButton?jb9 =新?JButton(" JButton 9 ");

c . gridx = 1;

c . gridy = 3;

c . grid width = 3;

c . grid height = 1;

c.fill=GridBagConstraints。水平;

exp.makeComponent(gridbag,c,jb9);

JButton?JB 10 =新?JButton(" JButton 10 ");

c . gridx = 1;

c . gridy = 4;

c . grid width = 3;

c . grid height = 1;

c.fill=GridBagConstraints。水平;

exp.makeComponent(gridbag,c,JB 10);

exp . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);

exp.setSize(500,500);

exp . set visible(true);

}

}

  • 上一篇:國際美元寶app源代碼
  • 下一篇:源代碼818源代碼
  • copyright 2024編程學習大全網