阅读更多
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
public class JTableDemo
{
public static void main (String[] args)
{
Connection con;
Statement stmt;
ResultSet rs;
Vector vector1=new Vector();
Vector vector3=new Vector();
vector1.addElement("学号");
vector1.addElement("姓名");
vector1.addElement("专业");
vector1.addElement("年龄");
vector1.addElement("家庭住址");
vector1.addElement("联系电话");
vector1.addElement("期末总评");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123");
stmt=con.createStatement();
rs=stmt.executeQuery("select * from student order by sno");
while(rs.next())
{
Vector vector2=new Vector();
vector2.addElement(rs.getString(1));
vector2.addElement(rs.getString(2));
vector2.addElement(rs.getString(3));
vector2.addElement(rs.getString(4));
vector2.addElement(rs.getString(5));
vector2.addElement(rs.getString(6));
vector2.addElement(rs.getString(7));
vector3.addElement(vector2);
}
}catch(SQLException ee){
ee.printStackTrace();
}
JTable student = new JTable (vector3,vector1);
student.setPreferredScrollableViewportSize(new Dimension(1000, 100));//设置表格的大小
student.setRowHeight (30);//设置每行的高度为20
student.setSelectionForeground (Color.black);//设置所选择行的前景色
student.setGridColor (Color.black);//设置网格线的颜色
student.setShowGrid (true);//是否显示网格线
student.setShowHorizontalLines (true);//是否显示水平的网格线
student.setShowVerticalLines (true);//是否显示垂直的网格线
student.doLayout ();//使表布局行与列
student.setBackground (Color.lightGray);//表格背景
JScrollPane pane3 = new JScrollPane (student);
JPanel panel = new JPanel (new GridLayout (0, 1));
panel.setPreferredSize (new Dimension (1000,400));
panel.add (pane3);
JFrame frame = new JFrame ("JTableDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);//将Panel加入窗口中
frame.setBounds(200, 200, 1000, 300);
frame.setVisible(true);
}
}
- 大小: 42.8 KB