Java – arrange swing components by edge

Can swing components be arranged?

You can see a subtle difference that I find very annoying I know that all jlabels [Blue / Purple rectangles have the same size, so I think it may be because of "and" * ", but I'm not sure because the left boxes of the first two don't line up

panel

JPanel panel2 = new JPanel(new GridLayout(4,1));
JPanel panel2a = new JPanel(new FlowLayout());
JPanel panel2b = new JPanel(new FlowLayout());

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);

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));

And*

opA = new JLabel("+",JLabel.CENTER);
opS = new JLabel("*",JLabel.CENTER);

The display rectangle is 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));

Add to panel

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);

Solution

Afaiu can be implemented using grouplayout This layout requires five horizontal groups and two vertical groups

For an example, see how to use grouplayout, including a discussion of the image

For mcve, see also this answer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>