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

本文介绍了如何在Java GUI中利用GridBagLayout布局管理器进行组件的绝对定位。详细讲解了GridBagConstraints的重要参数,如gridx、gridy、gridwidth、gridheight、weightx、weighty等,并提供了一个示例代码,展示了如何创建并设置这些参数以达到预期的布局效果。
摘要由CSDN通过智能技术生成

展开全部

相对定位是利用布局管理器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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值