简体   繁体   中英

<br>? \n? a line break in java

I have three JLabels and three 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. How can I do a simple line break?

如果这是一个Swing应用程序,您应该使用布局管理器将字段放在容器中。

Line break won't help with placing Swing objects; you need to place a layout on a center 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. GridLayout(6,1) may do it.

You can use layout managers like GridLayout or 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.

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 :

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:

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. But there is no such thing as an intended line break in a 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.

Layout managers you might want to get to know are:

  • BorderLayout - very good if you want resizeable content
  • GridLayout - simple equals width and height grid
  • null - allows you to use setBounds on each component to get absolute positions

There are more, but these three should allow you to layout 95% of your panels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM