首先,这是我的代码:
public void getGK()
{
add(p4);
setSize(1200,700);
setLocation(30,17);
p4.setVisible(true);
p4.setLayout(null);
final CheckboxGroup cg=new CheckboxGroup();
final JLabel q=new JLabel();
final JButton b6=new JButton("Submit");
b6.setBounds(150,250,80,50);
q.setBounds(40,40,1000,50);
q.setForeground(Color.RED);
q.setFont(new Font("Castellar",Font.BOLD,15));
p4.add(l1);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
co=DriverManager.getConnection("jdbc:odbc:Quizdata","","");
st=co.createStatement();
final String s2="SELECT * FROM GK Where ID="+lx[n];
re=st.executeQuery(s2);
q.setText(re.getString(2));
p4.add(q);
final Checkbox c5=new Checkbox();
final Checkbox c1=new Checkbox(re.getString(3),cg,false);
final Checkbox c2=new Checkbox(re.getString(4),cg,false);
final Checkbox c3=new Checkbox(re.getString(5),cg,false);
final Checkbox c4=new Checkbox(re.getString(6),cg,false);
c1.setBounds(40,100,400,20);
c2.setBounds(40,130,400,20);
c3.setBounds(40,160,400,20);
c4.setBounds(40,190,400,20);
p4.add(c1);
p4.add(c2);
p4.add(c3);
p4.add(c4);
p4.add(b6);
re.;
final String s3=re.getString("Answer");
b6.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
if(s3.equals(cg.getSelectedCheckbox().getLabel()))
{
System.out.println("THis is correct bwoy " +cg.getSelectedCheckbox().getLabel());
n++;
score++;
remove(p4);
p4.removeAll();
if(n<10)
{
getGK();
}
else
{
System.out.println("Thanks for playing");
}
}
else
{
System.out.println("You are wrong");
}
}
});
co.close();
st.close();
}
catch(Exception z)
{
JOptionPane.showMessageDialog(null,z);
}
}
如您所见,我没有关闭resultset re,但我仍然收到Resulset is closed 异常..如何解决它。代码还有一个问题,当面板删除其所有组件然后再次添加新组件时,复选框可以正常工作,但标签不会改变,我必须最大化框架才能更改它。请尽快解决这个问题。
PS-这里 lx[] 和 n 是通过这段代码来的:
ArrayList<Integer> num=new ArrayList<Integer>();
for(int it=1;it<=10;it++)
{ num.add(it);}
for(int jt=0;jt<lx.length;++jt)
{
int i=(int)(Math.random()*num.size());
lx[jt]=num.get(i);
num.remove(i);
}