我就是想让窗体里面的哦控件,随着窗体的大小变化而变化。我试了很多布局管理器。都不行,用弹簧布局写代码又太麻烦了。请高手指点一下。谢谢。
解决方案 »
- 【HtmlParser】提取网页的meta信息
- 【急】关于键盘事件的问题
- 子类覆盖父类方法问题
- 难题!!有关java类应用...
- <html:base server="www.sohu.com" target="_blank"/>的问题
- 菜鸟问个有关InstallAnywhere的问题,。。。
- 初学套接字编程一问,在线等
- 请教高手:Object类的clone( )方法的有关问题
- basic error
- java 显示FTP目录下所有文件,FTPFile[] files = ftpClient.listFiles(); 总是返回null
- 如何安装JBuilder2008
- 怎么把图片从eclipse里存储到数据库里去
1.GridLayout布局
比较重要的属性有
GridData.FILL 通常与GridData类的对象属性horizontalAlignment和verticalAlignment配合使用,充满对象属性指定的空间。
GridData. FILL_HORIZONTAL 水平充满,组件充满网格水平方向的空间。
GridData. FILL_VERTICAL 垂直充满,组件充满网格垂直方向的空间。
GridData. FILL_BOTH 双向充满,组件充满水平和垂直方向的空间。配合以下两种属性
grabExcessHorizontalSpace 抢占额外的水平空间。
grabExcessVerticalSpace 抢占额外的垂直空间。既可以达到楼主要的效果了,
具体实例代码如下:Text t1=new Text(shell,SWT.BORDER);
t1.setText("文本框 1");
gridData = new GridData();
gridData.verticalSpan = 2; //跨两行
gridData.horizontalSpan=2; //跨两列
gridData.verticalAlignment = GridData.FILL; //垂直方向充满
gridData.grabExcessVerticalSpace = true; //抢占垂直方向额外空间
gridData.horizontalAlignment = GridData.FILL;//水平方向充满
gridData.grabExcessHorizontalSpace = true;//抢占水平方向额外空间
t1.setLayoutData(gridData); //gridData用于文本框t1==============================================================================
2.FormLayout布局
比较重要的类:FormData和FormAttachmentAttachment 的含义是附着、粘贴。FormAttachment 类就是用来指定组件在父容器中的
粘贴位置。FormAttachment 计算组件粘贴位置和组件大小的方法是依据下面的表达式:
y = ax + b
表达式中y 是纵坐标,从上往下是正方向;x 是横坐标,从左至右是正方向;a 是斜率
(a=m/n,n≠0),b 是偏移量,沿x、y轴正方向的偏移量为正,反之为负。具体实例:FormAttachment formAttachment=new FormAttachment(b1); //指定B1为参照物
FormData formData2=new FormData(50,30); //创建FormData对象,宽度50,高度30
formData2.left=formAttachment; //b2的左边紧贴与b1的右边
b2.setLayoutData(formData2); //设置b2的布局数据为formData2Button b3=new Button(shell,SWT.PUSH);
b3.setText("B3");
FormData formData3=new FormData();//创建布局数据对象formData3
formData3.top=new FormAttachment(b2,10,SWT.BOTTOM);//b2的底边与b3的顶部距离为10
formData3.left=new FormAttachment(b2,0,SWT.LEFT);//b2的左边与b3左边位移为0,
//即左边对齐
formData3.right=new FormAttachment(b2,0,SWT.RIGHT); //b2的右边与b3右边对齐
b3.setLayoutData(formData3); //设置b3的布局数据为formData3更详细的例子可以参照《Java程序设计之swt教程》,我的资源空间里有
panel.setLayout(new BoxLayout(panel,BoxLayout.X../Y..));
package com.syj;import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* @author 孙钰佳
* @main [email protected]
* @date 2008-3-21 下午11:17:11
*/
final public class GUI extends JFrame { private JTextArea textArea; public GUI() {
super();
this.setTitle("http://blog.csdn.net/sunyujia/");
setBounds(20, 20, 800, 600);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
init();
setVisible(true);
} public void init() {
final JPanel upPanel = new JPanel();
upPanel.setPreferredSize(new Dimension(0, 33));
upPanel.setLayout(null);
getContentPane().add(upPanel, BorderLayout.NORTH); Font font = new Font("宋体", Font.PLAIN, 12);
final JButton button = new JButton();
button.setBounds(5, 5, 70, 26);
button.setFocusPainted(false);
button.setText("test");
button.setFont(font);
upPanel.add(button); final JPanel mainJPlanel = new JPanel();
mainJPlanel.setLayout(new BorderLayout());
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 13));
mainJPlanel.add(textArea);
getContentPane().add(mainJPlanel, BorderLayout.CENTER);
JScrollPane textAreaJScrollPane = new JScrollPane();
textAreaJScrollPane.setViewportView(textArea);
mainJPlanel.add(textAreaJScrollPane);
final JPanel bottomJPlanel = new JPanel();
bottomJPlanel.setPreferredSize(new Dimension(0, 20));
bottomJPlanel.setLayout(new GridLayout());
getContentPane().add(bottomJPlanel, BorderLayout.SOUTH);
} public static void main(String args[]) {
try {
new GUI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
混合使用。
这就要求各个控件和主窗体都要通信
重截paint方法计算吧
这样要开销很大的,一般不会这么做,有这们的需要吗
import java.awt.Container;
import java.awt.GridLayout;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;public class Main
{
public static void main(String[] args)
{
JFrame myFrame = new JFrame("test");
Container contentPane = myFrame.getContentPane();
contentPane.setLayout(new GridLayout(2,1));
JTable table = new JTable(3, 2);
table.setRowHeight(30);
table.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JButton button = new JButton("test");
contentPane.add(table);
contentPane.add(button);
myFrame.setSize(400, 300);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}主要是用了 GridLayout 的布局管理器