java布局管理器的应用总结,GridBagLayout布局管理器的应用

GridBagLayout布局管理器比较复杂,参数也比较多,参数的名字是

newGridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,

fill,insert,ipadx,ipady);每个参数均是按这样的顺序排列,关于参数的详细介绍以后再写,下面的例子用布局管理器实现了基本的功能

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.Toolkit;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextArea;

@SuppressWarnings("serial")

public class Teste3 extends JFrame{

private static JTextArea jTextArea;

private JButton jButton1;

private JButton jButton2;

private JButton jButton3;

public Teste3(){

GridBagLayout gridbag = new GridBagLayout();

this.setLayout(gridbag);

gridbag.setConstraints(getJtextArea(), new GridBagConstraints(0,0,3,2,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(100,100,30,100),0,0));

gridbag.setConstraints(getJButton1(), new GridBagConstraints(0,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,200,100,100),0,0));

gridbag.setConstraints(getJButton2(), new GridBagConstraints(1,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,100,100,100),0,0));

gridbag.setConstraints(getJButton3(), new GridBagConstraints(2,2,1,1,1.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL,new Insets(30,100,100,200),0,0));

this.add(getJtextArea());

this.add(getJButton1());

this.add(getJButton2());

this.add(getJButton3());

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

int screenWidth = (int) screenSize.getWidth(); //获得屏幕的宽

int screenHight = (int) screenSize.getHeight();//获得屏幕的高

this.setSize(screenWidth, screenHight);

this.setVisible(true);

}

private JTextArea getJtextArea(){

if(jTextArea == null){

jTextArea = new JTextArea();

}

return jTextArea;

}

private JButton getJButton1(){

if(jButton1 == null){

jButton1 = new JButton("jButton1");

}

return jButton1;

}

private JButton getJButton2(){

if(jButton2 == null){

jButton2 = new JButton("jButton2");

}

return jButton2;

}

private JButton getJButton3(){

if(jButton3 == null){

jButton3 = new JButton("jButton3");

}

return jButton3;

}

public static void main(String args[]){

new Teste3();

}

}

上面的代码运行之后,窗口变化时页面大小也会跟着变化,jTextArea窗口没有加入滚动条,加入这两行代码后:

JScrollPane scroll = new JScrollPane(getJTextArea());

this.add(scroll);

jTextArea将变得不可见,正在调试中,也欢迎高手指教。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值