我在youtube上练习教程。它在这里 - > http://www.youtube.com/watch?v=QdbKQ5h9yZg&list=PL70BFBF88CE38C556
我检查了下面的代码几十次。但我仍然无法理解为什么JFrame没有正确显示。虽然我把尺寸设置为600x400(或者更大)但是当我运行项目时仍然没有改变,JFrame
大小没有改变,标签也没有显示。问题在哪里?
这是我的代码:
package doanjava;
import java.sql.*;
public class db {
Connection con;
Statement st;
ResultSet rs;
public db(){
connect();
}
public void connect(){
try{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db = "jdbc:odbc:ketnoi";
con = DriverManager.getConnection(db);
st = con.createStatement();
String sql = "select * from HoSoSinhVien";
rs = st.executeQuery(sql);
while(rs.next())
{
String Ho = rs.getString("Ho");
String Ten = rs.getString("Ten");
String Tuoi = rs.getString("Tuoi");
String Lop = rs.getString("Lop");
String SoDienThoai = rs.getString("SoDienThoai");
String DiaChi = rs.getString("DiaChi");
System.out.println(Ho+" "+Ten+" | "+"Tuoi: "+Tuoi+" | "+"Lop :"+Lop+" | "+"SDT: "+SoDienThoai+" | "+"Dia Chi: "+DiaChi);
}
}catch(Exception ex){
}
}
public static void main(String[] args) {
new db();
new gui();
}
}
和
package doanjava;
import javax.swing.*;
public class gui {
JFrame f;
JLabel Ho;
JLabel Ten;
JLabel Tuoi;
JLabel Lop;
JTextField t;
JTextField t1;
JTextField t2;
JTextField t3;
public gui(){
frame();
}
public void frame(){
f = new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Ho = new JLabel("Ho");
Ten = new JLabel("Ten");
Tuoi = new JLabel("Tuoi");
Lop = new JLabel("Lop");
t = new JTextField(10);
t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(10);
JPanel p = new JPanel();
p.add(Ho);
p.add(t);
p.add(Ten);
p.add(t1);
p.add(Tuoi);
p.add(t2);
p.add(Lop);
p.add(t3);
f.add(p);
}
}
答案 0 :(得分:1)
String Ho = rs.getString("Ho");
除了System.out.println();
这些值被丢弃,对JLabel
Ho = new JLabel("Ho");
pack()
。 catch (Exception e) {
..
到:
catch (Exception e) {
e.printStackTrace(); // very informative! ..
答案 1 :(得分:0)
在向setVisible
添加components
后,您必须致电JFrame f = new JFrame();
//set properties
//add other components
f.setVisible(true); //finally
。
{{1}}