我有以下图片。它包含一个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);
但我想要达到的目的是:
对我来说这看起来微不足道。只需使用图片使 growy 标签,然后使用 wrap 选项添加'@ usuario1',以便在开头添加下一个组件('contenido'标签)在下一行中,由于图片标签占据了JPanel的所有高度,所提到的下一行将从'@ usuario1'标签下方开始。然而,似乎发生的事情是下一行开始于图片标签的正下方,但由于标签占据了JPanel的所有高度,因此未显示,因此我得到的结果比开头更差。
我的目标可能吗?如果是这样,怎么样?
答案 0 :(得分:3)
浏览link处于MiGLayout的PDF文件,或者您可以尝试使用GridBagLayout。如果你想使用GridBagLayout this link可能会有所帮助。使用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);
O / P:
答案 1 :(得分: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);