-1

我有下图。它由一个 JPanel 组成,其中一个标签用于图片,另一个用于“@usuario1”文本,另一个用于“contenido”,另一个用于“#demo”。图片下方是构造它的代码。

源代码

SettingsManagerTab.panelForTweet1 = new JPanel();
SettingsManagerTab.panelForTweet1.setOpaque(true);
SettingsManagerTab.panelForTweet1.setLayout(new net.miginfocom.swing.MigLayout());
this.add(SettingsManagerTab.panelForTweet1);

SettingsManagerTab.labelForTweetPhoto1 = new JLabel(new ImageIcon(
        SettingsManagerTab.class.getResource("/res/User1.png")));
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetPhoto1);

SettingsManagerTab.labelForTweetUsername1 = new JLabel("@usuario1");
final Font contentFont = new Font(SettingsManagerTab.labelForTweetUsername1.getFont()
        .getName(), Font.BOLD, SettingsManagerTab.labelForTweetUsername1.getFont()
        .getSize());
final Font userFont = new Font(contentFont.getName(), Font.BOLD,
        (int) (contentFont.getSize() * 0.7));
SettingsManagerTab.labelForTweetUsername1.setFont(userFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetUsername1);

SettingsManagerTab.labelForTweetContent1 = new JLabel("contenido");
SettingsManagerTab.labelForTweetContent1.setFont(contentFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetContent1);

SettingsManagerTab.labelForTweetHashtag1 = new JLabel("#demo");
SettingsManagerTab.labelForTweetHashtag1.setFont(contentFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetHashtag1);

但我想要实现的是:

目标

对我来说这看起来微不足道。只需用图片制作标签,然后添加带有wrap选项的“@ usuario1 ”,以便在下一行的开头添加下一个组件(“contenido”标签),并且由于图片标签占据了所有JPanel 的高度,提到的下一行将在“@usuario1”标签下方开始。然而,似乎发生的是下一行从图片标签下方开始,但由于标签占据了 JPanel 的所有高度,因此没有显示,所以我得到的结果比开始时更差。

我的目标可能吗?如果是这样,怎么办?

4

2 回答 2

3

浏览MiGLayout链接中的PDF,或者您可以尝试 GridBagLayout。如果您想使用 GridBagLayout,此链接可能会有所帮助。使用 GridBagLayout 您可以根据需要设置组件。

参考下面的例子:

    panel.add(comp1);
    panel.add(comp2, "span 2 2"); // The component will span 2x2 cells.
    panel.add(comp3, "wrap"); // Wrap to next row
    panel.add(comp4);
    panel.add(comp5, "wrap"); // Note that it "jumps over" the occupied cells.
    panel.add(comp6);
    panel.add(comp7);


开/关:

在此处输入图像描述

于 2013-03-10T11:19:14.050 回答
0

哈哈!由于您的指示 sachin 设法做到了,但使用了 GridBagLayout。

SettingsManagerTab.panelForTweet1.setLayout(new GridBagLayout());
this.add(SettingsManagerTab.panelForTweet1);

SettingsManagerTab.labelForTweetPhoto1 = new JLabel(new ImageIcon(
        SettingsManagerTab.class.getResource("/res/User1.png")));

final GridBagConstraints a = new GridBagConstraints(), b = new GridBagConstraints(), c = new GridBagConstraints(), d = new GridBagConstraints();
a.gridheight = GridBagConstraints.REMAINDER;
a.gridx = 0;
a.weightx = 0.1;
a.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetPhoto1, a);

SettingsManagerTab.labelForTweetUsername1 = new JLabel("@usuario1");
final Font contentFont = new Font(SettingsManagerTab.labelForTweetUsername1.getFont()
        .getName(), Font.BOLD, SettingsManagerTab.labelForTweetUsername1.getFont()
        .getSize());
final Font userFont = new Font(contentFont.getName(), Font.BOLD,
        (int) (contentFont.getSize() * 0.7));
SettingsManagerTab.labelForTweetUsername1.setFont(userFont);
b.weightx = 1;
b.gridwidth = 2;
b.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetUsername1, b);

SettingsManagerTab.labelForTweetContent1 = new JLabel("contenido");
SettingsManagerTab.labelForTweetContent1.setFont(contentFont);
c.gridheight = GridBagConstraints.REMAINDER;
c.weightx = 1;
c.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetContent1, c);

SettingsManagerTab.labelForTweetHashtag1 = new JLabel("#demo");
SettingsManagerTab.labelForTweetHashtag1.setFont(contentFont);
d.weightx = 30;
d.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetHashtag1, d);
于 2013-03-10T14:46:48.810 回答