gpt4 book ai didi

java - 如何修改 javax.swing.TreeCellRenderer 以便描边单元格中的文本

转载 作者:行者123 更新时间:2023-12-02 11:21:21 25 4
gpt4 key购买 nike

有人知道如何修改 javax.swing.TreeCellRenderer 才能描边单元格中的文本吗?

最佳答案

如果您想在某些列中描边文本,您应该使用渲染器。如果所有单元格都需要这种字体,只需修改表格的字体即可。以下是两种变体的示例:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.font.TextAttribute;
import java.util.Collections;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("unchecked")
public class TableRendererTest {

public static void main(String[] args) {
JFrame frm = new JFrame("Renderer test");
DefaultTableModel model = new DefaultTableModel(new String[] {"First", "Second", "Third" }, 3);
model.setValueAt("Test String", 0, 0);
model.setValueAt("Corner String", 2, 0);
model.setValueAt("Last cell", 2, 2);

// table with strike-out renderer (first column is stroked out)
JTable tbl = new JTable(model);
tbl.getColumnModel().getColumn(0).setCellRenderer(new StrikeOutRenderer());
frm.add(new JScrollPane(tbl), BorderLayout.NORTH);

// table with strike-out font (all cells are stroked out)
JTable another = new JTable(model);
another.setFont(
another.getFont().deriveFont(Collections.singletonMap(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON)));
frm.add(new JScrollPane(another), BorderLayout.SOUTH);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}

private static class StrikeOutRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component res = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
res.setFont(res.getFont().deriveFont(Collections.singletonMap(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON)));
return res;
}
}
}

关于java - 如何修改 javax.swing.TreeCellRenderer 以便描边单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49897474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com