import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.table.JTableHeader;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
String[] columnName = {"姓名", "年龄", "成绩"};
Object[][] tableValuse = { {"TT", new Integer(16), new Integer(80)}
};
JTable jTable1 = new JTable(tableValuse,columnName);
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jTable1.setCellSelectionEnabled(true);
jTable1.setBounds(new Rectangle(52, 45, 298, 194));
contentPane.add(jTable1);
}
}
列标题为什么无法显示啊???
----------------解决方案--------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.table.JTableHeader;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
String[] columnName = {"姓名", "年龄", "成绩"};
Object[][] tableValuse = { {"TT", new Integer(16), new Integer(80)}
};
JTable jTable1 = new JTable(tableValuse,columnName);
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
// contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jTable1.setCellSelectionEnabled(true);
jTable1.setBounds(new Rectangle(52, 45, 298, 194));
contentPane.add(new JScrollPane(jTable1));
setVisible(true);
}
public static void main(String[] args) {
new Frame1();
}
}
----------------解决方案--------------------------------------------------------
本来想说已经解决了的,想不到冰老大厉害!呵呵
----------------解决方案--------------------------------------------------------
呵呵
----------------解决方案--------------------------------------------------------
程序稍微改了一下又出不来了!
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.JScrollPane;
import javax.swing.table.JTableHeader;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame2 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
Object [][] cell = {{"adf","adsf"}};
String[] str = {"asdf","asdf"};
JScrollPane jsp = new JScrollPane();
JTable jTable1 = new JTable(cell,str);
JTableHeader jth = jTable1.getTableHeader();
public Frame2() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jTable1.setBounds(new Rectangle(78, 54, 250, 153));
jTable1.setBackground(Color.CYAN);
jTable1.setCellSelectionEnabled(true);
contentPane.add(jsp);
jsp.getViewport().add(jth);
jsp.getViewport().add(jTable1);
}
}
----------------解决方案--------------------------------------------------------
不要把你的布局管理器设为null
----------------解决方案--------------------------------------------------------
contentPane.setLayout(null);
这句去掉就没事了
因为你把它设为null后
它里面的组件都 必须设置边框才能被加进来了
所以,不推荐使用此方法来构建界面
----------------解决方案--------------------------------------------------------
还是照着书上打的 想不到会有这样的效果!!晕
----------------解决方案--------------------------------------------------------
有时候,书不一定就是对的
----------------解决方案--------------------------------------------------------