2016-09-26 108 views
1

JTableJScrollPane不會顯示列標題。帶JScrollPane的表格將不會顯示列標題

package jTable; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 

public class Main { 

    JFrame frame ; 
    Main() 
    {  
    frame = new JFrame() ; 
    String [][] names = { 
      {"1000","yamen","develeoper"}, 
      {"2000","aymen","data entry"}, 
      {"3000","mohammed","teacher"} 
      }; 

     String[] header = {"ID","NAME","JOB"};  
     JTable jtb = new JTable(names,header) ;   
     JScrollPane s = new JScrollPane(jtb); 

     jtb.setBounds(40,50,300,400); 
     frame.add(jtb); 

     frame.setSize(400, 500); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    } 

    public static void main(String[] args) { 
     new Main(); 
    } 
} 
+0

[* JTable可能重複將不顯示列標題*](http://stackoverflow.com/q/2320812/230513)。 – trashgod

回答

0

嘗試建立與名稱和標題

tableModel = new DefaultTableModel(names, headers); 

一個TableModel然後分配JTable這個TableModel

jtb.setModel(tableModel); 

,然後添加一個新的JScrollPane在框架

frame.add(new JScrollPane(jtb)); 

至少,這是我在程序中做到的。