昨天看了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分别为相对、绝对、没有(这个是猜的)位置,分别对应前面的数字。