Android's Linearlayout For Swing
Is there a LayoutManager for Swing that acts as LinearLayout in Android? I like an idea of components weights very much.
Solution 1:
You can use the FlowLayout, GridLayout or BorderLayout. In my experience in building GUI in java, I mostly use combinations of BorderLayouts (most of the time) and GridLayouts.
If you want it to look like
the code is:
publicvoidinitComponents() {
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);
}
Solution 2:
If you need individual weights use GridBagLayout. Or you can try BoxLayout.
Solution 3:
You can use SwanLayout https://github.com/idayrus/swan-layout
This library bring LinearLayout and FrameLayout implementation to Java Swing
Example in kotlin:
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)
Post a Comment for "Android's Linearlayout For Swing"