java 在JLabel中显示数据库中的信息 无法显示 哪里错了
packageteacher_Main;importjava.awt.Container;importjava.sql.Connection;importjava.sql...
package teacher_Main;
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class imformation_Teacher extends JFrame
{
private JLabel Tname;
private JLabel Tno;
private JLabel Tage;
private JLabel Tsex;
private JLabel Tdept;
private JTextField name;
private JTextField no;
private JTextField age;
private JTextField sex;
private JTextField dept;
private JLabel imageJlabel;
imformation_Teacher()
{
super("个人信息");
Container c=getContentPane();
c.setLayout(null);
Tname=new JLabel("姓名");
Tno=new JLabel("教工号");
Tage=new JLabel("年龄");
Tsex=new JLabel("性别");
Tdept=new JLabel("专业");
name=new JTextField();
no=new JTextField();
age=new JTextField();
sex=new JTextField();
dept=new JTextField();
imageJlabel=new JLabel();
imageJlabel.setIcon(new ImageIcon("imageJlabel.jpg"));
imageJlabel.setSize(780,580);
imageJlabel.setLocation(0, 0);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try
{
Connection conn = DriverManager.getConnection("jdbc:odbc:mydatanase");
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Teacher where 姓名='张卫庆' ");
name.setText(rs.getString("姓名"));
no.setText(rs.getString("教工号"));
age.setText(rs.getString("年龄"));
sex.setText(rs.getString("性别"));
dept.setText(rs.getString("系别"));
}
catch(Exception e)
{
e.getStackTrace();
}
name.setSize(170,40);
name.setLocation(210, 50);
/*try{
name.setText("ddd");
}
catch(Exception e)
{
e.getStackTrace();
}*/
no.setLocation(210, 100);
age.setLocation(210, 150);
sex.setLocation(210, 200);
dept.setLocation(210, 250);
Tname.setLocation(100, 50);
Tno.setLocation(100, 100);
Tage.setLocation(100, 150);
Tsex.setLocation(100, 200);
Tdept.setLocation(100, 250);
c.add(Tname);
c.add(name);
c.add(Tno);
c.add(no);
c.add(Tage);
c.add(age);
c.add(Tsex);
c.add(sex);
c.add(Tdept);
c.add(dept);
c.add(imageJlabel);
this.setVisible(true);
this.setBounds(0,0,600,400);
}
}
不好意思 不是在JLabel里显示 是再JTextField里显示 展开
import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class imformation_Teacher extends JFrame
{
private JLabel Tname;
private JLabel Tno;
private JLabel Tage;
private JLabel Tsex;
private JLabel Tdept;
private JTextField name;
private JTextField no;
private JTextField age;
private JTextField sex;
private JTextField dept;
private JLabel imageJlabel;
imformation_Teacher()
{
super("个人信息");
Container c=getContentPane();
c.setLayout(null);
Tname=new JLabel("姓名");
Tno=new JLabel("教工号");
Tage=new JLabel("年龄");
Tsex=new JLabel("性别");
Tdept=new JLabel("专业");
name=new JTextField();
no=new JTextField();
age=new JTextField();
sex=new JTextField();
dept=new JTextField();
imageJlabel=new JLabel();
imageJlabel.setIcon(new ImageIcon("imageJlabel.jpg"));
imageJlabel.setSize(780,580);
imageJlabel.setLocation(0, 0);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱动
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try
{
Connection conn = DriverManager.getConnection("jdbc:odbc:mydatanase");
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Teacher where 姓名='张卫庆' ");
name.setText(rs.getString("姓名"));
no.setText(rs.getString("教工号"));
age.setText(rs.getString("年龄"));
sex.setText(rs.getString("性别"));
dept.setText(rs.getString("系别"));
}
catch(Exception e)
{
e.getStackTrace();
}
name.setSize(170,40);
name.setLocation(210, 50);
/*try{
name.setText("ddd");
}
catch(Exception e)
{
e.getStackTrace();
}*/
no.setLocation(210, 100);
age.setLocation(210, 150);
sex.setLocation(210, 200);
dept.setLocation(210, 250);
Tname.setLocation(100, 50);
Tno.setLocation(100, 100);
Tage.setLocation(100, 150);
Tsex.setLocation(100, 200);
Tdept.setLocation(100, 250);
c.add(Tname);
c.add(name);
c.add(Tno);
c.add(no);
c.add(Tage);
c.add(age);
c.add(Tsex);
c.add(sex);
c.add(Tdept);
c.add(dept);
c.add(imageJlabel);
this.setVisible(true);
this.setBounds(0,0,600,400);
}
}
不好意思 不是在JLabel里显示 是再JTextField里显示 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询