日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

javapanel根据内部组件_java gui中怎么用jpanel实现组件的绝对定位

發布時間:2024/7/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javapanel根据内部组件_java gui中怎么用jpanel实现组件的绝对定位 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

展開全部

相對定位是利用布局管理器GridBagLayout()?來實現的GridBagLayout?布局是根據GridBagConstraints()?來設定的GridBagConstraints主要有8個重要參數需要掌握非別62616964757a686964616fe58685e5aeb931333332613033是

gridx,gridy? ——? 設置組件的位置,

gridx設置為GridBagConstraints.RELATIVE代表此組件位于之前所加入組件的右邊。

gridy設置為GridBagConstraints.RELATIVE代表此組件位于以前所加入組件的下面。

建議定義出gridx,gridy的位置以便以后維護程序。gridx=0,gridy=0時放在0行0列。

gridwidth,gridheight? ——? 用來設置組件所占的單位長度與高度,默認值皆為1。

你可以使用GridBagConstraints.REMAINDER常量,代表此組件為此行或此列的最后一個組件,而且會占據所有剩余的空間。

weightx,weighty? ——? 用來設置窗口變大時,各組件跟著變大的比例。

當數字越大,表示組件能得到更多的空間,默認值皆為0。

anchor? ——? 當組件空間大于組件本身時,要將組件置于何處。

有CENTER(默認值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST選擇。

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

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

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

GridBagLayout里的各種設置都必須通過GridBagConstraints,因此當我們將GridBagConstraints的參數都設置

好了之后,必須new一個GridBagConstraints的對象出來,以便GridBagLayout使用。

構造函數:

GirdBagLayout()建立一個新的GridBagLayout管理器。

GridBagConstraints()建立一個新的GridBagConstraints對象。

GridBagConstraints(int?gridx,int?gridy,

int?gridwidth,int?gridheight,

double?weightx,double?weighty,

int?anchor,int?fill,?Insets?insets,

int?ipadx,int?ipady)建立一個新的GridBagConstraints對象,并指定其參數的值

下面是我的例程:

實現

import?javax.swing.*;

import?java.util.*;

import?java.awt.*;

public?class?Example?extends?JFrame{

private?void?makeComponent(GridBagLayout?gbLaout,GridBagConstraints?constraints,Component?component)

{

gbLaout.setConstraints(component,?constraints);

add(component);

}

public?static?void?main(String?args[])?{

Example?exp=new?Example();

GridBagLayout?gridbag?=?new?GridBagLayout();

GridBagConstraints?c?=?new?GridBagConstraints();

exp.setLayout(gridbag);

JButton?jb1=new?JButton("JButton1");

c.gridx=0;

c.gridy=0;

c.gridwidth=1;

c.gridheight=1;

c.weightx=1;

c.weighty=0;

c.anchor=GridBagConstraints.CENTER;

c.fill=GridBagConstraints.HORIZONTAL;

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

c.ipadx=0;

c.ipady=0;

exp.makeComponent(gridbag,c,jb1);

JButton?jb2=new?JButton("JButton2");

c.gridx=1;

exp.makeComponent(gridbag,c,jb2);

JButton?jb3=new?JButton("JButton2");

c.gridx=2;

exp.makeComponent(gridbag,c,jb3);

JButton?jb4=new?JButton("JButton2");

c.gridx=3;

exp.makeComponent(gridbag,c,jb4);

JButton?jb5=new?JButton("JButton5");

c.gridx=0;

c.gridy=1;

c.gridheight=1;

c.gridwidth=4;

exp.makeComponent(gridbag,c,jb5);

JButton?jb6=new?JButton("JButton6");

c.gridx=0;

c.gridy=2;

c.gridwidth=3;

exp.makeComponent(gridbag,c,jb6);

JButton?jb7=new?JButton("JButton7");

c.gridx=3;

c.gridy=2;

c.gridwidth=1;

exp.makeComponent(gridbag,c,jb7);

JButton?jb8=new?JButton("JButton8");

c.gridx=0;

c.gridy=3;

c.gridwidth=1;

c.gridheight=2;

c.fill=GridBagConstraints.BOTH;

exp.makeComponent(gridbag,c,jb8);

JButton?jb9=new?JButton("JButton9");

c.gridx=1;

c.gridy=3;

c.gridwidth=3;

c.gridheight=1;

c.fill=GridBagConstraints.HORIZONTAL;

exp.makeComponent(gridbag,c,jb9);

JButton?jb10=new?JButton("JButton10");

c.gridx=1;

c.gridy=4;

c.gridwidth=3;

c.gridheight=1;

c.fill=GridBagConstraints.HORIZONTAL;

exp.makeComponent(gridbag,c,jb10);

exp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

exp.setSize(500,500);

exp.setVisible(true);

}

}

總結

以上是生活随笔為你收集整理的javapanel根据内部组件_java gui中怎么用jpanel实现组件的绝对定位的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。