setIconTextGap 用于具有居中对齐的标签

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

setIconTextGap for labels with centered alignment

问题

我需要将图标添加到表头,表头是一个JLabel。

当文本对齐在左侧时,它正常工作。

然而,当文本居中时,调用setIconTextGap()似乎不起作用。这会导致图标也放置在标签的中心:

[![enter image description here][1]][1]

这在一般情况下不起作用吗,还是我漏掉了什么?

我正在使用Java 6进行工作(无法更改这个事实)。

我已经创建了下面的SSCCE:

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;

public class Main {

    // ... (上面的代码未变)

    public static class IconLabel extends JLabel implements TableCellRenderer {

        // ... (上面的代码未变)

        @Override
        public void setBounds(int x, int y, int width, int height) {
            super.setBounds(x, y, width, height);
            int textWidth = getFontMetrics(getFont()).stringWidth(getText());
            Insets insets = getInsets();
            int iconTextGap = width - textWidth - icon.getIconWidth() - insets.left - insets.right - 3;
            setIconTextGap(iconTextGap);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setHorizontalTextPosition(alignment);
            return this;
        }
    }

    public static class TableArrowIcon implements Icon {

        // ... (上面的代码未变)

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(Color.darkGray);

            int controlSize = 20;
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            direction.drawArrow((Graphics2D) g, controlSize, x, y);

        }

        @Override
        public int getIconWidth() {
            return 14;
        }

        @Override
        public int getIconHeight() {
            return 14;
        }
    }
}

提前感谢任何指导。

英文:

I need to add an icon to a table header which is a JLabel.

When the text is aligned on the left hand side, it's working fine.

However, when the text is centered, the call to setIconTextGap() is somehow not considered. This has the effect, that the icon is also placed in the center of the label:

[![enter image description here][1]][1]

Is this not working in general, or did I miss something?

I'm working with Java 6 (cannot change that fact).

I have created a SSCCE below:

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
public class Main {
public static void main(String[] args)
{
IconLabel label1 = new IconLabel("test text left", new TableArrowIcon(TableArrowIcon.TableArrowDirection.NORTH), SwingConstants.LEFT);
IconLabel label2 = new IconLabel("test text center", new TableArrowIcon(TableArrowIcon.TableArrowDirection.NORTH), SwingConstants.CENTER);
Object[][] data = {
{"Speed reading", 20, true },
{"Joe", "Brown", "Pool", 10, false }
};
String[] columnNames = { "test", "test" };
JPanel panel = new JPanel(new BorderLayout());
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
label2.setHorizontalTextPosition(JLabel.CENTER);
TableColumn column1 = new TableColumn();
TableColumn column2 = new TableColumn();
column1.setHeaderRenderer(label1);
column2.setHeaderRenderer(label2);
table.addColumn(column1);
table.addColumn(column2);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(1);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static class IconLabel extends JLabel implements TableCellRenderer {
private final Icon icon;
private final int alignment;
public IconLabel(String text, Icon icon, int alignment) {
super(text);
this.icon = icon;
setIcon(icon);
setPreferredSize(new Dimension(50, 20));
this.alignment = alignment;
}
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
int textWidth = getFontMetrics(getFont()).stringWidth(getText());
Insets insets = getInsets();
int iconTextGap = width - textWidth - icon.getIconWidth() - insets.left - insets.right -     3;
setIconTextGap(iconTextGap);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean     isSelected, boolean hasFocus, int row, int column) {
setHorizontalTextPosition(alignment);
return this;
}
}
public static class TableArrowIcon implements Icon {
public enum TableArrowDirection {
NORTH() {
@Override
public void drawArrow(Graphics2D g, int controlSize, int x, int y) {
g.drawLine(x + 3, y + (controlSize - 10), x + 7, y + (controlSize - 14));
g.drawLine(x + 7, y + (controlSize - 14), x + (controlSize - 9), y + (controlSize     - 10));
}
};
public abstract void drawArrow(Graphics2D g, int controlSize, int x, int y);
}
private final TableArrowDirection direction;
public TableArrowIcon(TableArrowDirection direction) {
this.direction = direction;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.darkGray);
int controlSize = 20;
((Graphics2D) g).setRenderingHint( RenderingHints.KEY_ANTIALIASING,     RenderingHints.VALUE_ANTIALIAS_ON );
direction.drawArrow((Graphics2D) g, controlSize, x, y);
}
@Override
public int getIconWidth() {
return 14;
}
@Override
public int getIconHeight() {
return 14;
}
}
}

Thanks in advance for any pointers.
[1]: https://i.stack.imgur.com/Kmazc.png

答案1

得分: 1

  1. 使用JPanel作为渲染器,并向面板添加两个标签。一个用于文本,另一个用于图标。
  2. 您可以为面板使用BorderLayout布局。
  3. 将文本标签添加到CENTER,将图标添加到LINE_END。
  4. 然后,您需要为渲染器添加一个参数,以指示文本是应左对齐还是居中。这应在构造函数中设置。
英文:
  1. Use a JPanel as the render and add two labels to the panel. One for the text and the other for the Icon.
  2. You can use a BorderLayout for the panel.
  3. Add the text label to the CENTER and the Icon to the LINE_END.
  4. You will then need a parameter for the renderer to indicate if the text should be left justified or centered. This should be set in the constructor.

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

发表评论

匿名网友

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

确定