日期:2014-05-17  浏览次数:20692 次

请问为什么打了一下代码还是不能在Combobox中输出关于学号的数据呢?
请问为什么打了一下代码还是不能在Combobox中输出关于学号的数据呢?
import java.sql.*;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class school extends Frame implements ActionListener {
private static JComboBox box = new JComboBox();
private JRadioButton nan = new JRadioButton("男");
private JRadioButton nv = new JRadioButton("女");
private JButton dl=new JButton("登录"); 
private static Connection con;
private static Statement sql;
private static ResultSet rs;
private static String xb;
private static String bj;
private static String xh;

public school(){
setLayout(null);
JComboBox box = new JComboBox();
box.setBounds(100, 100, 200, 30);
box.addActionListener(this);

add(box);

JRadioButton nan = new JRadioButton("男");
nan.setBounds(100, 150, 50, 50);
nan.addActionListener(this);
add(nan);

JRadioButton nn = new JRadioButton("女");
nv.setBounds(250, 150, 50, 50);
nv.addActionListener(this);
add(nv);

JButton dl=new JButton("登录");
dl.setBounds(100, 260, 100, 50);
dl.addActionListener(this);
add(dl);

setSize(500,400);
setTitle("海贼王");
setVisible(true);
this.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}


}
);
}
public static void main(String e[]){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e1){
System.out.println(""+e1);
}
try{
con=DriverManager.getConnection("jdbc:odbc:shuju","","");
sql=con.createStatement();
rs=sql.executeQuery("select * from 表1");

while(rs.next()){

String xh=rs.getString(1);//学号是第一列的数据,拿出//
String bj=rs.getString(2);//班级是第二列的数据,拿出//
String xb=rs.getString(3);//性别是第三列的数据,拿出//
box.addItem(xh);//在Combobox中输出关于学号的数据//

}
con.close();

}catch(SQLException e1){

}
new school();

}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}

}


------解决方案--------------------
你的构造函数是:
public school(){
setLayout(null);
JComboBox box = new JComboBox(); // 创建了局部变量box,并生成新的对象,跟之前那个“private static JComboBox box”毫无关系。

把这句话去掉吧。