我不知道为什么运行后的界面很奇怪希望高手帮帮解决import javax.swing.*;
import javax.swing.table.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.Vector;public class TableTest extends JFrame{
public TableTest(Vector columnName,Vector tableValue){
super();
final TableTest1 panel = new TableTest1(columnName,tableValue);
this.setBounds(300,200,300,200);
this.add(panel,BorderLayout.CENTER);
this.setVisible(true);
}
class TableTest1 extends JPanel{
private Vector<String> columnName;
private Vector<Vector<Object>> tableValue;
private int fixed = 1;
public TableTest1(Vector columnName, Vector tableValue){
super();
this.setBounds(300,200,300,200);
this.columnName = columnName;
this.tableValue = tableValue;

Fixed fixedTableModel = new Fixed();
JTable fixedTable = new JTable(fixedTableModel);

Fixed floatingTableModel = new Fixed();
JTable floatingTable = new JTable(floatingTableModel);
floatingTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane sp = new JScrollPane();
sp.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
JViewport viewport = new JViewport();
viewport.setView(fixedTable);
viewport.setPreferredSize(fixedTable.getPreferredSize());
sp.setRowHeader(viewport);
sp.setViewportView(floatingTable);
this.add(sp,BorderLayout.CENTER);


this.setVisible(true);
}
public class Fixed extends AbstractTableModel{
public int getColumnCount(){
return fixed;
}
public int getRowCount(){
return tableValue.size();
}
public Object getValueAt(int row, int column){
return tableValue.get(row).get(column);
}
public String getColumnName(int column){
return columnName.get(column);
}
}

public class Floating extends AbstractTableModel{
public int getColumnCount(){
return columnName.size() - fixed;
}
public int getRowCount(){
return tableValue.size();
}
public Object getValueAt(int row, int column){
return tableValue.get(row).get(column + fixed);
}
public String getColumnName(int column){
return columnName.get(column + fixed);
}
}
}

public static void main(String[] args){
Vector columnName = new Vector();
columnName.add("日期");
for(int i = 0 ; i < 21 ; i++ ){
columnName.add("商品"+i);
}
Vector tableValue = new Vector();
for(int row = 1 ; row < 31 ; row++ ){
Vector rowV = new Vector();
rowV.add(row);
for(int col = 0 ; col < 20 ; col++ ){
rowV.add((int)Math.random()*1000);
}
tableValue.add(rowV);
}
new TableTest(columnName,tableValue);
}
}

解决方案 »

  1.   

    我自己找到了一处错误,但是还不是我要的效果import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import java.util.Vector;public class TableTest extends JFrame{
    public TableTest(Vector columnName,Vector tableValue){
    super();
    final TableTest1 panel = new TableTest1(columnName,tableValue);
    this.setBounds(300,200,300,200);
    this.add(panel,BorderLayout.CENTER);
    this.setVisible(true);
    }
    class TableTest1 extends JPanel{
    private Vector<String> columnName;
    private Vector<Vector<Object>> tableValue;
    private int fixed = 1;
    public TableTest1(Vector columnName, Vector tableValue){
    super();
    this.setBounds(300,200,300,200);
    this.columnName = columnName;
    this.tableValue = tableValue;

    Fixed fixedTableModel = new Fixed();
    JTable fixedTable = new JTable(fixedTableModel);

    Floating floatingTableModel = new Floating();  //
    JTable floatingTable = new JTable(floatingTableModel);
    floatingTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane sp = new JScrollPane();
    sp.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedTable.getPreferredSize());
    sp.setRowHeader(viewport);
    sp.setViewportView(floatingTable);
    this.add(sp,BorderLayout.CENTER);


    this.setVisible(true);
    }
    public class Fixed extends AbstractTableModel{
    public int getColumnCount(){
    return fixed;
    }
    public int getRowCount(){
    return tableValue.size();
    }
    public Object getValueAt(int row, int column){
    return tableValue.get(row).get(column);
    }
    public String getColumnName(int column){
    return columnName.get(column);
    }
    }

    public class Floating extends AbstractTableModel{
    public int getColumnCount(){
    return columnName.size() - fixed;
    }
    public int getRowCount(){
    return tableValue.size();
    }
    public Object getValueAt(int row, int column){
    return tableValue.get(row).get(column + fixed);
    }
    public String getColumnName(int column){
    return columnName.get(column + fixed);
    }
    }
    }

    public static void main(String[] args){
    Vector columnName = new Vector();
    columnName.add("日期");
    for(int i = 0 ; i < 21 ; i++ ){
    columnName.add("商品"+i);
    }
    Vector tableValue = new Vector();
    for(int row = 1 ; row < 31 ; row++ ){
    Vector rowV = new Vector();
    rowV.add(row);
    for(int col = 0 ; col < 20 ; col++ ){
    rowV.add((int)Math.random()*1000);
    }
    tableValue.add(rowV);
    }
    new TableTest(columnName,tableValue);
    }
    }
      

  2.   

    import java.awt.BorderLayout;
    import java.util.Vector;import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JViewport;
    import javax.swing.table.AbstractTableModel;public class TableTest extends JFrame {
    public TableTest(Vector columnName, Vector tableValue) {
    super();
    final TableTest1 panel = new TableTest1(columnName, tableValue);
    this.setBounds(300, 200, 300, 200);
    this.add(panel, BorderLayout.CENTER);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    } class TableTest1 extends JScrollPane {
    private Vector<String> columnName;
    private Vector<Vector<Object>> tableValue;
    private int fixed = 1; public TableTest1(Vector columnName, Vector tableValue) {
    super();
    this.columnName = columnName;
    this.tableValue = tableValue; Fixed fixedTableModel = new Fixed();
    JTable fixedTable = new JTable(fixedTableModel); Floating floatingTableModel = new Floating(); // 
    JTable floatingTable = new JTable(floatingTableModel);
    floatingTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); this.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable
    .getTableHeader());
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedTable.getPreferredSize());
    this.setRowHeader(viewport);
    this.setViewportView(floatingTable); this.setVisible(true);
    } public class Fixed extends AbstractTableModel {
    public int getColumnCount() {
    return fixed;
    } public int getRowCount() {
    return tableValue.size();
    } public Object getValueAt(int row, int column) {
    return tableValue.get(row).get(column);
    } public String getColumnName(int column) {
    return columnName.get(column);
    }
    } public class Floating extends AbstractTableModel {
    public int getColumnCount() {
    return columnName.size() - fixed;
    } public int getRowCount() {
    return tableValue.size();
    } public Object getValueAt(int row, int column) {
    return tableValue.get(row).get(column + fixed);
    } public String getColumnName(int column) {
    return columnName.get(column + fixed);
    }
    }
    } public static void main(String[] args) {
    Vector columnName = new Vector();
    columnName.add("日期");
    for (int i = 0; i < 21; i++) {
    columnName.add("商品" + i);
    }
    Vector tableValue = new Vector();
    for (int row = 1; row < 31; row++) {
    Vector rowV = new Vector();
    rowV.add(row);
    for (int col = 0; col < 21; col++) {
    rowV.add((int) Math.random() * 1000);
    }
    tableValue.add(rowV);
    }
    new TableTest(columnName, tableValue);
    }
    }这样好些么?
      

  3.   


    谢谢大牛的回答,但是我想问的是,TableTest1这个类继承于JScrollPane跟我继承JPanel并且只在JPanel中只放入一个component有什么区别?为什么换了继承方式就可以了
      

  4.   

    还有一个问题final TableTest1 panel = new TableTest1(columnName, tableValue);
    这里不会出问题吗?TableTest1这个类是继承JScrollPane