package Table;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableModel2 implements ActionListener{
JTable t = null;
public TableModel2() {
// TODO Auto-generated constructor stub
JFrame f = new JFrame("DataModel");
JButton b1 = new JButton("数学老师");
b1.addActionListener(this);
JButton b2 = new JButton("学生阿呆");
b2.addActionListener(this);
JPanel panel = new JPanel();
panel.add(b1);
panel.add(b2);
t = new JTable(new MyTable(1));
t.setPreferredScrollableViewportSize(new Dimension(550, 30));
JScrollPane s = new JScrollPane(t);
f.getContentPane().add(panel,BorderLayout.NORTH);
f.getContentPane().add(s,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TableModel2();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("学生阿呆"))
t.setModel(new MyTable(1));
if(e.getActionCommand().equals("数学老师"))
t.setModel(new MyTable(2));
t.revalidate();
}
}
class MyTable extends AbstractTableModel{
Object[][] p1 = {
{
"阿呆","1234",new Integer(66),new Integer(50),new Integer(116),false,false
}
};
String[] n1 = {
"姓名","学号","语文","数学","总分","及格","作弊"
};
Object[][] p2 = {
{
"阿呆","1234",new Integer(50),false,false,"01234"
},
{
"阿瓜","1235",new Integer(75),true,false,"05678"
}
};
String[] n2 = {
"姓名","学号","数学","及格","作弊","电话"
};
int model = 1;
public MyTable(int i) {
// TODO Auto-generated constructor stub
int model = i;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
if(model == 1)
return p1.length;
else
return p2.length;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
if(model == 1)
return n1.length;
else
return n2.length;
}
@Override
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
if(model == 1)
return p1[row][col];
else
return p2[row][col];
}
public String getColumnName(int col){
if(model == 1)
return n1[col];
else
return n2[col];
}
public Class getColumnClass,s(int c){
return getValueAt(0,c).getClass();
}
}
------解决思路----------------------
不知道从哪来的代码,只要改成: public Class getColumnClass(int c){
就可以了
------解决思路----------------------
你是想要这结果吗?
代码如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableModel2 implements ActionListener{
JTable t = null;
public TableModel2() {
// TODO Auto-generated constructor stub
JFrame f = new JFrame("DataModel");
JButton b1 = new JButton("数学老师");
b1.addActionListener(this);
JButton b2 = new JButton("学生阿呆");
b2.addActionListener(this);
JPanel panel = new JPanel();
panel.add(b1);
panel.add(b2);
t = new JTable(new MyTable(1));
t.setPreferredScrollableViewportSize(new Dimension(550, 30));
JScrollPane s = new JScrollPane(t);
f.getContentPane().add(panel,BorderLayout.NORTH);
f.getContentPane().add(s,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TableModel2();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("学生阿呆"))
t.setModel(new MyTable(1));
if(e.getActionCommand().equals("数学老师"))
t.setModel(new MyTable(2));
t.revalidate();
}
}
class MyTable extends AbstractTableModel{
Object[][] p1 = {
{
"阿呆","1234",new Integer(66),new Integer(50),new Integer(116),false,false
}
};
String[] n1 = {
"姓名","学号","语文","数学","总分","及格","作弊"
};
Object[][] p2 = {
{
"阿呆","1234",new Integer(50),false,false,"01234"
},
{
"阿瓜","1235",new Integer(75),true,false,"05678"
}
};
String[] n2 = {
"姓名","学号","数学","及格","作弊","电话"
};
int model = 1;
public MyTable(int i) {
// TODO Auto-generated constructor stub
int model = i;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
if(model == 1)
return p1.length;
else
return p2.length;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
if(model == 1)
return n1.length;
else
return n2.length;
}
@Override
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
if(model == 1)
return p1[row][col];
else
return p2[row][col];
}
public String getColumnName(int col){
if(model == 1)
return n1[col];
else
return n2[col];
}
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
}
------解决思路----------------------
package Table;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TableModel2 implements ActionListener{
JTable t = null;
public TableModel2() {
JFrame f = new JFrame("DataModel");
JButton b1 = new JButton("数学老师");
b1.addActionListener(this);
JButton b2 = new JButton("学生阿呆");
b2.addActionListener(this);
JPanel panel = new JPanel();
panel.add(b1);
panel.add(b2);
t = new JTable(new MyTable(1));
t.setPreferredScrollableViewportSize(new Dimension(550, 30));
JScrollPane s = new JScrollPane(t);
f.getContentPane().add(panel,BorderLayout.NORTH);
f.getContentPane().add(s,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new TableModel2();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("学生阿呆"))
t.setModel(new MyTable(1));
if(e.getActionCommand().equals("数学老师"))
t.setModel(new MyTable(2));
t.revalidate();
}
}
class MyTable extends AbstractTableModel{
Object[][] p1 = {
{
"阿呆","1234",new Integer(66),new Integer(50),new Integer(116),false,false
}
};
String[] n1 = {
"姓名","学号","语文","数学","总分","及格","作弊"
};
Object[][] p2 = {
{
"阿呆","1234",new Integer(50),false,false,"01234"
},
{
"阿瓜","1235",new Integer(75),true,false,"05678"
}
};
String[] n2 = {
"姓名","学号","数学","及格","作弊","电话"
};
int model = 1;
public MyTable(int i) {
// int model = i;
// 上面这句没有任何意义,改成下面这句
this.model = i;
}
public int getRowCount() {
if(model == 1)
return p1.length;
else
return p2.length;
}
public int getColumnCount() {
if(model == 1)
return n1.length;
else
return n2.length;
}
public Object getValueAt(int row, int col) {
if(model == 1)
return p1[row][col];
else
return p2[row][col];
}
public String getColumnName(int col){
if(model == 1)
return n1[col];
else
return n2[col];
}
public Class<?> getColumnClass(int c){
return getValueAt(0,c).getClass();
}
}
改成这样,不会有任何问题。