java.swing 图形用户界面 怎样添加表格

java.swing 图形用户界面 怎样添加表格,第1张

public class Stmessege1 extends JFrame {

private static final long serialVersionUID = 1L

private JTable table// 声明表格

void Stmessege11() {

setSize(460, 320)

setVisible(true)

}

public Vector getDataVector() throws SQLException, ClassNotFoundException {

Connection conn = null

java.sql.Statement stmt = null

Class.forName("com.mysql.jdbc.Driver")

String dburl = "jdbc:mysql://localhost:3306/hp"

String dbuser = "root"

String dbpassword = "19920102"

conn = DriverManager.getConnection(dburl, dbuser, dbpassword)

stmt = conn.createStatement()

String sql = "select* from xsgli"

ResultSet rs = stmt.executeQuery(sql)

Vector dataVector = new Vector()

while (rs.next()) {

Vector rowVector = new Vector()

for (int i = 1i <= 7i++) {

if (i == 2 || i == 4) {

rowVector.add(String.valueOf(rs.getInt(i)))

} else {

rowVector.add(rs.getString(i))

}

}

dataVector.add(rowVector)

}

return dataVector

}

public Vector getColumnVector() {

Vector columnVector = new Vector()

columnVector.add("姓名")

columnVector.add("学号")

columnVector.add("性别")

columnVector.add("班级")

columnVector.add("系别")

columnVector.add("成绩")

columnVector.add("专业")

return columnVector

}

public Stmessege1() throws SQLException, ClassNotFoundException {

super()

setTitle("学生学籍数据显示")

setBounds(100, 100, 382, 223)

// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Connection conn = null

Statement stmt = null

Class.forName("com.mysql.jdbc.Driver")

String dburl = "jdbc:mysql://localhost:3306/hp"

String dbuser = "root"

String dbpassword = "19920102"

conn = DriverManager.getConnection(dburl, dbuser, dbpassword)

java.sql.Statement st = conn.createStatement()

String sql = "select* from xsgli"

ResultSet rs = st.executeQuery(sql)

Vector columnVector = getColumnVector()

Vector dataVector = getDataVector()

final JScrollPane scrollPane = new JScrollPane()

getContentPane().add(scrollPane, BorderLayout.CENTER)

table = new JTable(dataVector, columnVector)

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)

scrollPane.setViewportView(table)

}

}

希望能帮到你,谢谢采纳

每次点击的时候都重新创建MyTable并且加到Frame里面,看下是不是被之前加入的Table盖住了。

或者换个写法,窗口初始化的时候同时创建表格,点击添加数据直接更新数据就行了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11942357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存