关于JAVA AnchorLayout布局不值一提的发现

本文介绍了一个使用AnchorLayout布局的Java Swing应用示例。通过具体代码展示了如何设置组件的位置及大小,探讨了AnchorConstraint属性的使用方法及其对于组件定位的影响。

昨天看了AnchorLayout布局,发现它是一个很不错的布局,但是遇到个问题,看代码先。

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class NewJFrame extends javax.swing.JFrame {
private JPanel jPanel4;
private JPanel jPanel1;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public NewJFrame() {
super();
initGUI();
}

private void initGUI() {
try {
AnchorLayout thisLayout = new AnchorLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, new AnchorConstraint(1, 905, 1001, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jPanel1.setPreferredSize(new java.awt.Dimension(557, 330));
}
pack();
this.setSize(400, 400);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}

}

红色部分数字为编辑区域的千分比,后面的 AnchorConstraint.ANCHOR_REL、AnchorConstraint.ANCHOR_ABS、AnchorConstraint.ANCHOR_NONE分别为相对、绝对、没有(这个是猜的)位置,分别对应前面的数字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值