效果图如下:
代码:
public class JLabelTest extends BaseFrame {
private JLabel label;
public JLabelTest() {
JLabel label = createTestLabel();
mainWin.add(label);
JPanel p = createTestPanel();
mainWin.add(p, BorderLayout.SOUTH);
}
private JLabel createTestLabel() {
label = new JLabel();
label.setPreferredSize(new Dimension(600, 300));
label.setOpaque(true);
label.setBackground(Color.WHITE);
return label;
}
private JPanel createTestPanel() {
JPanel p = new JPanel();
p.setBorder(BorderFactory.createTitledBorder("测试"));
// 文本/图片: 显示、调整间距、助记符、禁用与启用时显示的图标
Box verticalBox = new Box(BoxLayout.Y_AXIS);
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
final JCheckBox showIcon = createShowIconCheckBox();
final JCheckBox showText = createShowTextCheckBox();
final JTextField gapTextField = createIconTextGapTextField();
JLabel gapLabel = new JLabel("iconTextGap:");
gapLabel.setDisplayedMnemonic('G');
gapLabel.setLabelFor(gapTextField);
final JCheckBox disabledIcon = createDisabledIconCheckBox();
p1.add(showIcon);
p1.add(showText);
p1.add(gapLabel);
p1.add(gapTextField);
p1.add(disabledIcon);
verticalBox.add(p1);
// 文本 水平/垂直 位置
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
final JComboBox horizontalTextPosition = createHorizontalTextPositionComboBox();
final JComboBox verticalTextPosition = createVerticalTextPositionComboBox();
p2.add(new JLabel("HorizontalTextPosition"));
p2.add(horizontalTextPosition);
p2.add(new JLabel("VerticalTextPosition"));
p2.add(verticalTextPosition);
verticalBox.add(p2);
// 组件 水平/垂直 对齐