Swing-JTable(单元格的显示)2/9
发布日期:2021-05-14 04:14:19 浏览次数:18 分类:精选文章

本文共 5974 字,大约阅读时间需要 19 分钟。

������Swing���������������������������������

������Java Swing���������������������������������������������������������������������������������������������������������������������������������������Swing������������������������������������������������������������������������������������������������������

���������������������������������������������������������

  • ������Java������������
  • ������Swing������������������������������������
  • ���NetBeans IDE���������������

������������������������������

���������������������������������Student���������������������������������������������������������������������������

  • id: ������������������������������
  • name: ���������������������
  • sex: ���������������������
  • birthday: ���������������������������
  • cellphone: ������������������������
  • ������������������������������������������Student������������������NetBeans���������������

    package swing01;
    public class Student {
    public String id;
    public String name;
    public boolean sex;
    public String birthday;
    public String cellphone;
    }

    ���������������������������������������������

    ������������������������������������������TableCellRenderer������������������������������������������������������������������������������"���"���"���"������������������������������������������������

    ������������������������JLabel��������������������������������������� scheme���

    static class SexCellRenderer extends JLabel implements TableCellRenderer {
    public SexCellRenderer() {
    this.setHorizontalAlignment(SwingConstants.CENTER);
    this.setFont(this.getFont().deriveFont(Font.PLAIN));
    this.setBackground(new Color(0,0,0,0));
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    boolean sex = (boolean) value;
    this.setText(sex ? "���" : "���");
    this.setOpaque(true);
    if (isSelected) {
    this.setBackground(table.getSelectionBackground());
    this.setForeground(table.getSelectionForeground());
    } else {
    this.setBackground(table.getBackground());
    this.setForeground(table.getForeground());
    }
    return this;
    }
    }

    ���������������������������������������

    ������������������������������MyFrame���������������������������������������������������������������������������������������������

    package swing01;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.util.Vector;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingConstants;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableCellRenderer;
    public class MyFrame extends JFrame {
    public MyFrame(String title) {
    super(title);
    JPanel root = new JPanel();
    this.setContentPane(root);
    root.setLayout(new BorderLayout());
    JScrollPane scrollPane = new JScrollPane();
    table.setFillsViewportHeight(true);
    table.setRowSelectionAllowed(true);
    table.setRowHeight(30);
    root.add(scrollPane, BorderLayout.CENTER);
    tableModel.addColumn("������");
    tableModel.addColumn("������");
    tableModel.addColumn("������");
    tableModel.addColumn("������������");
    tableModel.addColumn("���������");
    TableColumn column2 = table.getColumnModel().getColumn(2);
    column2.setCellRenderer(new SexCellRenderer());
    Student stu = new Student();
    stu.id = "20175104014";
    stu.name = "������";
    stu.sex = true;
    stu.birthday = "2019-7-23";
    stu.cellphone = "15137724640";
    addTabelRow(stu);
    Student stu2 = new Student();
    stu2.id = "20175062026";
    stu2.name = "������";
    stu2.sex = false;
    stu2.birthday = "2019-7-23";
    stu2.cellphone = "17837724640";
    addTabelRow(stu2);
    }
    private void addTabelRow(Student stu) {
    Object[] rowData = new Object[5];
    rowData[0] = stu.id;
    rowData[1] = stu.name;
    rowData[2] = stu.sex;
    rowData[3] = stu.birthday;
    rowData[4] = stu.cellphone;
    tableModel.addRow(rowData);
    }
    }

    ���������������������������

    ���������������������������MyDemo������������������������������������

    package swing01;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    public class MyDemo {
    private static void createGUI() {
    JFrame frame = new MyFrame("������������");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createGUI();
    }
    });
    }
    }

    ���������������������������������

    • ������������������: ������������������������TableCellRenderer���������������������������������������������������������������
    • ������������: ������DefaultTableModel���������������������������������������������������������������������������������
    • ������������: ������NetBeans������������������������������������������������������������������������������
    • ������������: ���������������������������������������������������������������

    ������

    ���������������������������������Swing���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Swing������������������������������������������������������������

    上一篇:Swing-JTable(选中状态的显示)3/9
    下一篇:Swing-JTable(表格控件)1/9

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月11日 11时27分37秒