JButton同时带有图标(位于顶部)和文本(位于底部),对齐方式为左对齐。

huangapple go评论76阅读模式
英文:

JButton with both icon (on top) and text (on bottom) aligned to the left

问题

以下是您要的翻译部分:

我想要一个JButton,其中图标(位于顶部)和文本(位于底部)都对齐到左侧。用户可以调整按钮的大小,两者都应始终保持左对齐,就像这样:

(图片)

我尝试了以下代码:

// 文本对齐
comp.setHorizontalAlignment(SwingConstants.LEFT);
// 文本位置
comp.setVerticalTextPosition(SwingConstants.BOTTOM);
// 设置图标对齐?!
comp.setHorizontalTextPosition(SwingConstants.CENTER);

但是我的结果是(图标始终相对于文本居中!):

(图片)

有什么建议吗?

英文:

I would like to have a JButton with both icon (on top) and text (on bottom) aligned to the left. The Button can be resized by the user and both should always remain aligned to the left, like this:

JButton同时带有图标(位于顶部)和文本(位于底部),对齐方式为左对齐。

I have tried this

// Text alignment
comp.setHorizontalAlignment(SwingConstants.LEFT);
// text position
comp.setVerticalTextPosition(SwingConstants.BOTTOM);
//set icon alignment?!
comp.setHorizontalTextPosition(SwingConstants.CENTER);

but this is my result (The icon always remains centred in relation to the text!):

JButton同时带有图标(位于顶部)和文本(位于底部),对齐方式为左对齐。

Any ideas?

答案1

得分: 3

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public final class ButtonTextAlignmentTest {
  private Component makeUI() {
    Icon icon = UIManager.getIcon("FileView.directoryIcon");
    String text = "The text aligned to the left";

    String html = String.format("<html><img src='%s'/><br/>Html: %s", makeIconUrl(icon), text);
    JButton button1 = new JButton(html);

    JLabel iconLabel = new JLabel(null, icon, SwingConstants.LEADING);
    JLabel textLabel = new JLabel("Layout: " + text);
    JButton button2 = new JButton();
    button2.setLayout(new GridLayout(0, 1));
    button2.add(iconLabel);
    button2.add(textLabel);

    JPanel p = new JPanel();
    p.add(button1);
    p.add(button2);
    return p;
  }

  private static String makeIconUrl(Icon icon) { // Create a dummy URL for testing
    try {
      File file = File.createTempFile("dummy", ".png");
      file.deleteOnExit();
      BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = bi.createGraphics();
      icon.paintIcon(null, g2, 0, 0);
      g2.dispose();
      ImageIO.write(bi, "png", file);
      return file.toURI().toURL().toString();
    } catch (IOException ex) {
      return "";
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ButtonTextAlignmentTest().makeUI());
      frame.setSize(320, 240);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
    });
  }
}
英文:
  • You can use the &lt;br&gt; tag to split the JButton text onto multiple lines along with the &lt;html&gt; tags.
  • Another common way is to place multiple JLabels on a JButton by setting the LayoutManager.

JButton同时带有图标(位于顶部)和文本(位于底部),对齐方式为左对齐。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public final class ButtonTextAlignmentTest {
private Component makeUI() {
Icon icon = UIManager.getIcon(&quot;FileView.directoryIcon&quot;);
String text = &quot;The text aligned to the left&quot;;
String html = String.format(&quot;&lt;html&gt;&lt;img src=&#39;%s&#39;/&gt;&lt;br/&gt;Html: %s&quot;, makeIconUrl(icon), text);
JButton button1 = new JButton(html);
JLabel iconLabel = new JLabel(null, icon, SwingConstants.LEADING);
JLabel textLabel = new JLabel(&quot;Layout: &quot; + text);
JButton button2 = new JButton();
button2.setLayout(new GridLayout(0, 1));
button2.add(iconLabel);
button2.add(textLabel);
JPanel p = new JPanel();
p.add(button1);
p.add(button2);
return p;
}
private static String makeIconUrl(Icon icon) { // Create a dummy URL for testing
try {
File file = File.createTempFile(&quot;dummy&quot;, &quot;.png&quot;);
file.deleteOnExit();
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
icon.paintIcon(null, g2, 0, 0);
g2.dispose();
ImageIO.write(bi, &quot;png&quot;, file);
return file.toURI().toURL().toString();
} catch (IOException ex) {
return &quot;&quot;;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -&gt; {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new ButtonTextAlignmentTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}

huangapple
  • 本文由 发表于 2020年4月6日 15:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61055090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定