为什么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;
}
} 展开
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;
}
} 展开
3个回答
展开全部
帮你改了一下,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;
}
}
---------------------------------------------------------
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;
}
}
展开全部
你添加的是jtable:jContentPane.add(getJTable(), null);
不是 JScrollPane
不是 JScrollPane
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
显示的效果是怎么样的啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询