设计要点:
自定义单元格类: SexCellRenderer
package swing01;
/*
* 表格里得数据
*/
public class Student
{
public String id;
public String name;
public boolean sex;
public String birthday;
public String cellphone;
}
package swing01;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
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.TableCellRenderer;
import javax.swing.table.TableColumn;
public class MyFrame extends JFrame
{
//Model:负责数据
DefaultTableModel tableModel=new DefaultTableModel();
//View:负责显示,创建JTable的时候指定一个Model
JTable table=new JTable(tableModel);
public MyFrame(String title)
{
super(title);
JPanel root=new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
//添加至滚动面板
JScrollPane scrollPane=new JScrollPane(table);
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());
//添加数据行1
Student stu=new Student();
stu.id="20175104014";
stu.name="郭少";
stu.sex=true;
stu.birthday="2019-7-23";
stu.cellphone="15137724640";
addTabelRow(stu);
//添加数据行2
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)
{
//方法01
//import java.util.Vector是个泛型,表示数组
// Vector<Object>rowData=new Vector<>();
// rowData.add(stu.id);
// rowData.add(stu.name);
// rowData.add(stu.sex);
// rowData.add(stu.birthday);
// rowData.add(stu.cellphone);
// tableModel.addRow(rowData);
//方法2
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);
}
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;
if(sex==true)
this.setText("男");
else
this.setText("女");
//背景
this.setOpaque(true);
if(isSelected)
{
this.setBackground(table.getSelectionBackground());
this.setForeground(table.getSelectionForeground());
}
else
{
this.setBackground(table.getBackground());
this.setForeground(table.getForeground());
}
return this;
}
}
}
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指一个窗口,构造方法的参数为窗口标题
JFrame frame = new MyFrame("信息列表");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口的其他参数,如窗口大小
frame.setSize(400, 300);
// 显示窗口
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}