Reputation: 5081
Is it possible to line up swing components? The components are in separate panels which both use flow layout. These two panels are in another panel which is using a grid layout.
As you can see there is a subtle difference and I find it annoying. I know that all of the jlabels [the rectangles in blue/purple all have the same size, so i think it might be because of the '+' and '*', but I'm not sure because the left sides of the first two boxes aren't lined up.
the panels
JPanel panel2 = new JPanel(new GridLayout(4, 1));
JPanel panel2a = new JPanel(new FlowLayout());
JPanel panel2b = new JPanel(new FlowLayout());
the first two rectangles (purple)
add1 = new JLabel("", JLabel.CENTER);
add1.setTransferHandler(new TransferHandler("text"));
add1.setBorder(b2);
add2 = new JLabel("", JLabel.CENTER);
add2.setTransferHandler(new TransferHandler("text"));
add2.setBorder(b2);
the two blue rectangles
textFieldA = new JTextField();
textFieldA.setHorizontalAlignment(JTextField.CENTER);
textFieldA.setEditable(false);
textFieldA.setBorder(new LineBorder(Color.blue));
textFieldM = new JTextField();
textFieldM.setHorizontalAlignment(JTextField.CENTER);
textFieldM.setEditable(false);
textFieldM.setBorder(new LineBorder(Color.blue));
the + and *
opA = new JLabel("+", JLabel.CENTER);
opS = new JLabel("*", JLabel.CENTER);
Showing that the rectangles are the same size
Dimension d = card1.getPreferredSize();
int width = d.width + 100;
int height = d.height + 50;
add1.setPreferredSize(new Dimension(width, height));
add2.setPreferredSize(new Dimension(width, height));
mult1.setPreferredSize(new Dimension(width, height));
mult2.setPreferredSize(new Dimension(width, height));
textFieldA.setPreferredSize(new Dimension(width, height));
textFieldM.setPreferredSize(new Dimension(width, height));
Adding to the panels
panel2a.add(add1);
panel2a.add(opA);
panel2a.add(add2);
panel2a.add(enterA);
panel2a.add(textFieldA);
panel2c.add(mult1);
panel2c.add(opM);
panel2c.add(mult2);
panel2c.add(enterM);
panel2c.add(textFieldM);
panel2.add(panel2a);
panel2.add(panel2c);
Upvotes: 3
Views: 447
Reputation: 168825
AFAIU this could be achieved using GroupLayout
. This layout would require 5 horizontal groups and 2 vertical groups.
See How to Use GroupLayout for examples (including discussion of that image).
See also this answer for an MCVE.
Upvotes: 6
Reputation: 347204
Cross container layout management isn't really possible (or at least I've never seen a layout manager that does it).
You can, however, think carefully about your layout requirements and produce some interesting effects using compound components and layout managers...
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2, 0));
frame.add(new CalculatePane("+"));
frame.add(new CalculatePane("x"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CalculatePane extends JPanel {
public CalculatePane(String operator) {
setLayout(new GridBagLayout());
Font font = UIManager.getFont("TextField.font").deriveFont(Font.BOLD, 24);
JTextField field1 = new JTextField(2);
JTextField field2 = new JTextField(2);
JTextField field3 = new JTextField(2);
field1.setFont(font);
field2.setFont(font);
field3.setFont(font);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.fill = GridBagConstraints.BOTH;
add(field1, gbc);
gbc.gridx++;
add(new JLabel(operator), gbc);
gbc.gridx++;
add(field2, gbc);
gbc.gridx++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JButton("="), gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx++;
add(field3, gbc);
}
}
}
Upvotes: 6