是否可以排列摆动组件? 这些组件位于使用流布局的独立面板中。 这两个面板位于另一个使用网格布局的面板中。
正如你所看到的,有一个微妙的差异,我觉得很烦人。 我知道所有的jlabel [蓝色/紫色矩形都具有相同的大小,所以我认为这可能是因为'+'和'*',但我不确定,因为第一个的左侧两个箱子没有排队。
面板
JPanel panel2 = new JPanel(new GridLayout(4, 1)); JPanel panel2a = new JPanel(new FlowLayout()); JPanel panel2b = new JPanel(new FlowLayout());前两个矩形(紫色)
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);两个蓝色的长方形
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));+和*
opA = new JLabel("+", JLabel.CENTER); opS = new JLabel("*", JLabel.CENTER);显示矩形的大小相同
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));添加到面板
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);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);最满意答案
AFAIU可以使用GroupLayout来实现。 这种布局需要5个水平组和2个垂直组。
有关示例,请参阅如何使用GroupLayout (包括对该图像的讨论)。
另请参阅MCVE的此答案 。
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.
更多推荐
发布评论