为什么jTable没有显示列名

packageJDBC_1;importjava.awt.BorderLayout;importjavax.swing.JPanel;importjavax.swing.... package JDBC_1;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTable;
import java.awt.Rectangle;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.lang.Object;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
public class jdbc_1 extends JFrame {
private static final long serialVersionUID = 1L;
private static final int AUTO_RESIZE_ALL_COLUMNS = 0;
private JPanel jContentPane = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JTextField jTextField3 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JTable jTable = null; // @jve:decl-index=0:visual-constraint="650,69"
/**
* This is the default constructor
*/
public jdbc_1() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(616, 480);
this.setContentPane(getJContentPane());
this.setTitle("JTable");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(68, 332, 45, 25));
jLabel4.setText("生日");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(67, 293, 45, 25));
jLabel3.setText("住址");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(68, 251, 45, 25));
jLabel2.setText("姓名");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(69, 207, 45, 25));
jLabel1.setText("学号");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTable(), null);
}
return jContentPane;
}
private JTable getJTable() {
if (jTable == null) {
Object[] columnNames = {"Product","Numbers","Price"};
Object[][] data ={{"Apples", new Integer(5),"5.00"},{"Oranges", new Integer(3),"6.00"},{"Pears", new Integer(2),"4.00"},{"Grapes", new Integer(3),"2.00"}};
jTable = new JTable(data,columnNames);
jTable.setRowHeight(25);
jTable.setPreferredScrollableViewportSize(new Dimension(300,80));
jTable.setFont(new Font("宋体", Font.PLAIN, 12));
jTable.setSize(new Dimension(507, 174));
jTable.setVisible(true);
JScrollPane js=new JScrollPane(jTable);
js.setSize(new Dimension(179, 144));
add(js);
}
return jTable;
}
}
展开
 我来答
百度网友845f74e61
2012-11-16 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1599万
展开全部
帮你改了一下,JTABLE在JScrollPane中才会显示出表头。
---------------------------------------------------------
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class jdbc_1 extends JFrame {
private static final long serialVersionUID = 1L;
private static final int AUTO_RESIZE_ALL_COLUMNS = 0;
private JPanel jContentPane = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JTextField jTextField3 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JTable jTable = null; // @jve:decl-index=0:visual-constraint="650,69"
public static void main(String[] args){
new jdbc_1();
}
/**
* This is the default constructor
*/
public jdbc_1() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(616, 480);
this.setContentPane(getJContentPane());
this.setTitle("JTable");
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(68, 332, 45, 25));
jLabel4.setText("生日");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(67, 293, 45, 25));
jLabel3.setText("住址");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(68, 251, 45, 25));
jLabel2.setText("姓名");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(69, 207, 45, 25));
jLabel1.setText("学号");
jContentPane = new JPanel();
jContentPane.setLayout(null);
JScrollPane js = new JScrollPane(getJTable());
js.setSize(new Dimension(179, 144));
jContentPane.add(js, null);
}
return jContentPane;
}
private JTable getJTable() {
if (jTable == null) {
Object[] columnNames = { "Product", "Numbers", "Price" };
Object[][] data = { { "Apples", new Integer(5), "5.00" },
{ "Oranges", new Integer(3), "6.00" },
{ "Pears", new Integer(2), "4.00" },
{ "Grapes", new Integer(3), "2.00" } };
jTable = new JTable(data, columnNames);
jTable.setRowHeight(25);
jTable.setPreferredScrollableViewportSize(new Dimension(300, 80));
jTable.setFont(new Font("宋体", Font.PLAIN, 12));
jTable.setSize(new Dimension(507, 174));
jTable.setVisible(true);
}
return jTable;
}
}
百度网友12ee0fd
2012-11-16 · TA获得超过4.7万个赞
知道大有可为答主
回答量:1.1万
采纳率:66%
帮助的人:2309万
展开全部
你添加的是jtable:jContentPane.add(getJTable(), null);
不是 JScrollPane
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
luoluoyide
2012-11-16 · TA获得超过397个赞
知道小有建树答主
回答量:529
采纳率:0%
帮助的人:279万
展开全部
显示的效果是怎么样的啊?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式