Home >Java >javaTutorial >How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?
In a dynamically generated data input form managed by Swing's GroupLayout, how can one limit the maximum width of labels to a specific fraction of the parent frame's width, while adapting to resizing events?
To effectively limit label width using GroupLayout:
import javax.swing.GroupLayout; import javax.swing.JLabel; import javax.swing.JPanel; public class LabelWidthLimitPanel extends JPanel { private JLabel label1 = new JLabel("Primary:"); private JTextField field1 = new JTextField(16); private JLabel label2 = new JLabel("Secondary:"); private JTextField field2 = new JTextField(16); private JLabel label3 = new JLabel("Tertiary:"); private JTextField field3 = new JTextField(16); public LabelWidthLimitPanel() { GroupLayout layout = new GroupLayout(this); this.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(label1) .addComponent(label2) .addComponent(label3)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(field1) .addComponent(field2) .addComponent(field3)) ); layout.setVerticalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(label1) .addComponent(field1)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(label2) .addComponent(field2)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(label3) .addComponent(field3)) ); } }
In this example:
The above is the detailed content of How can I limit the maximum width of labels in a Swing GroupLayout-managed form to a fraction of the parent frame's width while handling resizing?. For more information, please follow other related articles on the PHP Chinese website!