1

我的代码存在特定问题。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;

class ver_his_sign extends JFrame {

    ver_his_sign() {

        JTable table = new JTable();
        JScrollPane scroll = new JScrollPane();

        DefaultTableModel model = new DefaultTableModel(get_data(), get_header());

        JFrame hk = new JFrame("Historial de Significados");
        Image icon = Toolkit.getDefaultToolkit().getImage("JLPT.jpg");
        ImageIcon ima = new ImageIcon("JLPT.jpg");
        table = new JTable(model) {
            public boolean isCellEditable(int row, int col) {
                switch (col) {
                    case 0:
                        return false;
                    case 1:
                        return false;
                    case 2:
                        return true;
                    default:
                        return false;
                }
            }

            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    default:
                        return Boolean.class;
                }
            }
        };
        table.setFont(new Font("Microsoft JhengHei", Font.BOLD, 13)); // a custom renderer which uses a special font
        DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
            Font font = new Font("Microsoft JhengHei", Font.BOLD, 50);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setFont(font);
                setForeground(Color.blue);
                return this;
            }
        };
        table.getColumnModel().getColumn(0).setCellRenderer(r);
        TableColumn column = null;
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) table.getDefaultRenderer(Object.class);
        defaultRenderer.setHorizontalAlignment(JLabel.CENTER);
        defaultRenderer.setFont(new Font("Microsoft JhengHei", Font.BOLD, 50));
        tcr.setFont(new Font("Microsoft JhengHei", Font.BOLD, 50));
        for (int i = 0; i < 3; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setPreferredWidth(200);
            } else {
                if (i == 1) {
                    column.setPreferredWidth(100);
                } else {
                    column.setPreferredWidth(300);
                }
            }
        }
        table.setRowHeight(table.getRowHeight() + 70);
        table.setModel(model);
        scroll.add(table);
        this.add(scroll);
        this.setTitle("Historial de Significados");
        this.setSize(1350, 700);
        this.setIconImage(icon);
        this.setVisible(true);
        this.setLocationRelativeTo(null);

        scroll.setViewportView(table);
    }

    Object[][] get_data() {
        Object data[][] = new Object[][]{
            {"Uno, 1", "\u4e00", true},
            {"Uno, 1", "\u4e01", true},
            {"Uno, 1", "\u4e02", true},
            {"Uno, 1", "\u4e03", true},
            {"Uno, 1", "\u4e04", true}
        };
        return data;
    }

    String[] get_header() {
        String header[] = new String[]{"KANJI", "SIGNIFICADO", "Agregar"};
        return header;
    }
}

然后会发生图像上出现的情况。我需要 Kanji 列居中对齐,但由于某种原因DefaultTableCellRenderer,`tcr,没有按预期工作,但默认渲染器按预期工作。

有什么建议/帮助吗?

在此处输入图像描述

4

1 回答 1

9

我已将您的示例缩减为要点:

  • 您的自定义渲染器r应该调整对齐方式以及大小和颜色。

  • 在模型中覆盖模型方法,而不是在视图中。

  • Swing GUI 对象应该事件分派线程上构建和操作。

  • 根据需要使用deriveFont()

  • 另请参阅本教程部分,了解如何选择渲染器

图片

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;
import javax.swing.JLabel;

class ver_his_sign extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ver_his_sign();
            }
        });
    }

    ver_his_sign() {

        DefaultTableModel model = new DefaultTableModel(get_data(), get_header()) {
            @Override
            public boolean isCellEditable(int row, int col) {
                switch (col) {
                    case 0:
                        return false;
                    case 1:
                        return false;
                    case 2:
                        return true;
                    default:
                        return false;
                }
            }

            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    default:
                        return Boolean.class;
                }
            }
        };
        DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object
                value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
                setForeground(Color.blue);
                setHorizontalAlignment(JLabel.CENTER);
                setFont(getFont().deriveFont(50f));
                return this;
            }
        };
        JTable table = new JTable(model);
        table.setRowHeight(table.getRowHeight() + 50);
        table.getColumnModel().getColumn(1).setCellRenderer(r);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(new JScrollPane(table));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    Object[][] get_data() {
        Object data[][] = new Object[][]{
            {"Uno, 1", "\u4e00", true},
            {"Uno, 1", "\u4e01", true},
            {"Uno, 1", "\u4e02", true},
            {"Uno, 1", "\u4e03", true},
            {"Uno, 1", "\u4e04", true}
        };
        return data;
    }

    String[] get_header() {
        String header[] = new String[]{"SIGNIFICADO", "KANJI", "Agregar"};
        return header;
    }
}
于 2013-06-18T09:03:01.393 回答