[英]<br>? \n? a line break in java
I have three JLabels and three JTextAreas. 我有三个JLabel和三个JTextAreas。 I have them in borderlayout, center, but I want each of them in a different line, that's not happening and the top ten search results in Google for line break java don't solve the problem.
我将它们放在borderlayout,center中,但是我希望它们中的每一个都在不同的行中,这种情况并没有发生,并且谷歌中用于换行符java的十大搜索结果都无法解决问题。 How can I do a simple line break?
我怎么能做一个简单的换行?
如果这是一个Swing应用程序,您应该使用布局管理器将字段放在容器中。
Line break won't help with placing Swing objects; 换行符不会有助于放置Swing对象; you need to place a layout on a center JPanel.
你需要在中心JPanel上放置一个布局。 That is, the center of your border layout should be a single Swing object, a JPanel, and you should set that to a style which allows you to stack each widget.
也就是说,边框布局的中心应该是单个Swing对象,JPanel,您应该将其设置为允许您堆叠每个窗口小部件的样式。 GridLayout(6,1) may do it.
GridLayout(6,1)可能会这样做。
You can use layout managers like GridLayout or GridBagLayout . 您可以使用GridLayout或GridBagLayout等布局管理器。 Even though the latter one is only recommended for code generated by GUI generators I prefer it because it gives me the most flexibility.
即使后者仅推荐用于GUI生成器生成的代码,我更喜欢它,因为它给了我最大的灵活性。
JPanel panel = new JPanel();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
layout.add(label1, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
layout.add(area1, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
layout.add(label2, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
layout.add(area2, new GridBagConstraints(1, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
layout.add(label3, new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
layout.add(area3, new GridBagConstraints(1, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(area1);
panel.add(area2);
panel.add(area3);
Of course this looks butt-ugly but should get you started. 当然这看起来很丑,但应该让你开始。
You can also abuse a BorderLayout : 你也可以滥用BorderLayout :
JPanel panel = new JPanel(new BorderLayout());
JPanel topRow = new JPanel();
panel.add(topRow, BorderLayout.PAGE_START);
topRow.add(label1);
topRow.add(area1);
JPanel middleRowBox = new JPanel(new BorderLayout());
panel.add(middleRowBox, BorderLayout.CENTER);
JPanel middleRow = new JPanel();
middleRowBox.add(middleRow, BorderLayout.PAGE_START);
middleRow.add(label2);
middleRow.add(area2);
JPanel bottomRowBox = new JPanel();
middleRowBox.add(bottomRowBox, BorderLayout.CENTER);
JPanel bottomRow = new JPanel();
bottomRowBox.add(bottomRow, BorderLayout.PAGE_START);
bottomRow.add(label3);
bottomRow.add(area3);
bottomRowBix.add(new JPanel(), BorderLayout.CENTER);
Try using a GridLayout for starters: 尝试使用GridLayout作为初学者:
panel.setLayout(new GridLayout(0,2));
// the order of added components is important
panel.add(labelA);
panel.add(textAreaA);
panel.add(labelB);
panel.add(textAreaB);
...
Doesn't look too pretty but it gets you started. 看起来不太漂亮,但它让你开始。
If you don't set a LayoutManager to a new panel, it will use a FlowLayout which behaves somewhat like HTML layout. 如果没有将LayoutManager设置为新面板,它将使用FlowLayout,其行为有点像HTML布局。 But there is no such thing as an intended line break in a FlowLayout.
但是在FlowLayout中没有预期的换行符。 It will just put component after component until it reaches the end of the available space and then start a new row.
它只会将组件放在组件之后,直到它到达可用空间的末尾,然后开始一个新行。
If you want control over your layouts - don't use FlowLayout. 如果要控制布局 - 请勿使用FlowLayout。
Layout managers you might want to get to know are: 您可能想要了解的布局管理器是:
There are more, but these three should allow you to layout 95% of your panels. 还有更多,但这三个应该允许您布局95%的面板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.