Hư Vô
Hư Vô

Reputation: 11

JFrame does not display correctly

I was practicing a tutorial on youtube. It's here -> http://www.youtube.com/watch?v=QdbKQ5h9yZg&list=PL70BFBF88CE38C556

I checked the codes below dozens of times. But i still can not understand why JFrame didn't show correctly. Although i put the size is 600x400 (or bigger than) but still nothing changed when i ran the project, JFrame size didn't change, the labels did not show too. Where is the problem?

This is my codes:

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();
    }
}

and

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);
    }
}

Upvotes: 0

Views: 110

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

String Ho = rs.getString("Ho");  

Beyond the System.out.println(); these values are discarded, and have no effect on the JLabel

Ho = new JLabel("Ho");
  1. Don't set the size of top level containers. Instead layout the content & call pack().
  2. Swing GUIs should be started and updated on the EDT.
  3. Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use them consistently.
  4. For better help sooner, post an SSCCE.
  5. Change code of the form:

 catch (Exception e) { 
    ..

to:

catch (Exception e) { 
    e.printStackTrace(); // very informative! ..

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

You have to call setVisible after adding the components to it.

JFrame f = new JFrame();

//set properties

//add other components     


f.setVisible(true); //finally

Upvotes: 0

Related Questions