排列摆动组件的边缘(Line up swing components by edges)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
排列摆动组件的边缘(Line up swing components by edges)

是否可以排列摆动组件? 这些组件位于使用流布局的独立面板中。 这两个面板位于另一个使用网格布局的面板中。

正如你所看到的,有一个微妙的差异,我觉得很烦人。 我知道所有的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垂直和水平组

有关示例,请参阅如何使用GroupLayout (包括对该图像的讨论)。

另请参阅MCVE的此答案 。

AFAIU this could be achieved using GroupLayout. This layout would require 5 horizontal groups and 2 vertical groups.

GroupLayout vertical & horizontal groups

See How to Use GroupLayout for examples (including discussion of that image).

See also this answer for an MCVE.

更多推荐

本文发布于:2023-04-24 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/786f06257b69887132ceaabb07993475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   排列   边缘   Line   components

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!