如何使JLabels从下一行开始 [英] How to make JLabels start on the next line

查看:160
本文介绍了如何使JLabels从下一行开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.add(economy);
pMeasure.add(regularity);
...

当我运行上面的代码时,我得到以下输出:

When I run the code above I get this output:

Economy Regularity

在每个JLabel从新行开始的地方,如何获得此输出?谢谢

How can I get this output, where each JLabel starts on a new line? Thanks

Economy  
Regularity

推荐答案

您将要试用

You'll want to play around with layout managers to control the positioning and sizing of the controls in your JPanel. Layout managers are responsible for placing controls, determining where they go, how big they are, how much space is between them, what happens when you resize the window, etc.

有很多不同的布局管理器,每个管理器允许您以不同的方式布局控件.默认的布局管理器是FlowLayout,如您所见,它只是将组件从左到右彼此相邻放置.那是最简单的.其他一些常见的布局管理器是:

There are oodles of different layout managers each of which allows you to layout controls in different ways. The default layout manager is FlowLayout, which as you've seen simply places components next to each other left to right. That's the simplest. Some other common layout managers are:

  • GridLayout-将组件排列在具有相等大小的行和列的矩形网格中
  • BorderLayout-在中央有一个主要组成部分,在上方,下方,左侧和右侧最多具有四个周围的组件.
  • GridBagLayout-所有内置布局管理器中的Big Bertha,它使用起来最灵活但也最复杂.
  • GridLayout - arranges components in a rectangular grid with equal-size rows and columns
  • BorderLayout - has one main component in the center and up to four surrounding components above, below, to the left, and to the right.
  • GridBagLayout - the Big Bertha of all the built-in layout managers, it is the most flexible but also the most complicated to use.

例如,您可以使用 BoxLayout 布置标签.

You could, for example, use a BoxLayout to layout the labels.

BoxLayout要么将其组件堆叠在一起,要么将它们放置在一行中-您可以选择.您可能会认为它是FlowLayout的一个版本,但是功能更强大.这是一个应用程序的图片,它演示了如何使用BoxLayout来显示组件的居中列:

BoxLayout either stacks its components on top of each other or places them in a row — your choice. You might think of it as a version of FlowLayout, but with greater functionality. Here is a picture of an application that demonstrates using BoxLayout to display a centered column of components:

使用BoxLayout的代码示例为:

JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));
pMeasure.add(economy);
pMeasure.add(regularity);
...

这篇关于如何使JLabels从下一行开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆