你好朋友这是我的程序一切都很好。但我无法在 JTable 顶部显示列名。我应该做哪些改变来展示它?并且还告诉我如何给这张桌子提供滚动条(垂直的,水平的)。
提前致谢..
报告1.Java:
package com.ash;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
public class Report1 extends JFrame {
Object[][] data = null;
private JPanel contentPane;
/**
* Launch the application.
*/
ResultSet rs,rs1;
Statement stmt=null;
int k=0;
PreparedStatement pr;
public JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Report1 frame = new Report1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Report1() {
setTitle("Report");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 953, 616);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
table.setBounds(100, 136, 810, 431);
contentPane.add(table);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
NewPatient.con=DbConnPa.getConObj();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="SELECT COUNT (*) FROM newpatient1";
try {
pr=NewPatient.con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs=pr.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count = 0;
try {
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count);
data= new Object [count][6];
try {
stmt=NewPatient.con.createStatement();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String sql1="SELECT * FROM newpatient1";
Object[] columns={"FirstName","LastName","Age","RefDoctor","Sex","RegId"};
try {
rs1=stmt.executeQuery(sql1);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(rs1.next()){
for(int j=0;j<6;j++){
try {
data[k][j]=rs1.getString(j+1);
System.out.println("i="+"j="+j);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
k++;}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table.setModel(new DefaultTableModel(data, columns));
}
});
btnNewButton.setBounds(382, 47, 89, 23);
contentPane.add(btnNewButton);
}
}