java gui中怎么用jpanel实现组件的绝对定位

不是绝对,是相对

相对定位是利用布局管理器GridBagLayout() 来实现的GridBagLayout 布局是根据GridBagConstraints() 来设定的GridBagConstraints主要有8个重要参数需要掌握非别是

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);

    }

}

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-04-07
javagui中jpanel实现组建的绝对定位,使用的是规定组件的x、y坐标来确定,如下:
setBounds
public void setBounds(int x,
int y,
int width,
int height)移动组件并调整其大小。由 x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。

参数:
x - 组件的新 x 坐标
y - 组件的新 y 坐标
width - 组件的新 width
height - 组件的新 height

所有Swing组件都可以用本回答被网友采纳
第2个回答  2011-08-21
setBounds
public void setBounds(int x,
int y,
int width,
int height)移动组件并调整其大小。由 x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。

参数:
x - 组件的新 x 坐标
y - 组件的新 y 坐标
width - 组件的新 width
height - 组件的新 height

所有Swing组件都可以用追问

这个在JPanel中有用吗?

追答

绝大部分带J的swing组件都有用,
这个方法是Component的,只要直接或间接继承Component
(除了几个没继承放在swing包里面的:如JDialog继承的是Dialog)都能用

本回答被网友采纳
第3个回答  2011-08-28
jpanle里可以直接设定坐标
相似回答