有没有适用于Swing的LayoutManager,其行为类似于Android中的LinearLayout?我非常喜欢组件权重的想法。
public void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setLayout(new java.awt.GridLayout(0, 1));
jButton1.setText("jButton1");
add(jButton1);
jButton2.setText("jButton2");
add(jButton2);
jButton3.setText("jButton3");
add(jButton3);
jButton4.setText("jButton4");
add(jButton4);
}
val panelVertical = JPanel(LinearLayout(LinearLayout.VERTICAL))
val panelHorizontal = JPanel(LinearLayout(LinearLayout.HORIZONTAL))
val lc = LinearConstraints()
lc.reset()
lc.width = 0
lc.weight = 0.5
lc.margin = 10
lc.marginEnd = 5
panelHorizontal.add(JButton("Horizontal (weight 0.5)"), lc)
lc.reset()
lc.width = 0
lc.weight = 0.5
lc.margin = 10
lc.marginStart = 5
panelHorizontal.add(JButton("Horizontal (weight 0.5)"), lc)
// Add panelHorizontal to panelVertical
lc.reset()
lc.width = LinearConstraints.MATCH_PARENT
lc.height = LinearConstraints.WRAP_CONTENT
panelVertical.add(panelHorizontal, lc)
lc.reset()
lc.margin = 10
lc.marginTop = 0
lc.width = LinearConstraints.WRAP_CONTENT
lc.height = LinearConstraints.WRAP_CONTENT
lc.gravity = LinearConstraints.CENTER
panelVertical.add(JButton("Gravity Center"), lc)
lc.reset()
lc.margin = 10
lc.marginTop = 0
lc.width = LinearConstraints.MATCH_PARENT
lc.height = LinearConstraints.MATCH_PARENT
panelVertical.add(JButton("Fill Remain"), lc)