我有两个JFrame,第一个用于使用JTable显示SQL表,第二个用于更新SQL表上的数据。在第一帧上,有一个用于显示第二帧的按钮,它具有单选按钮。但是,我不能将其设置为true。我想发生的事是根据我从Label那里获得的值(其中Label的值来自数据库)将单选按钮设置为true。这是我所做的:

第一个JFRAME:

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
 String hours = null;
  try
    {
        DbUpdate up = new DbUpdate();

        // To connect on SQL and get the JTable's value
        int get = (int)jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
        String query= "SELECT * FROM roominfo WHERE CustomerNo = '"+get+"' " ;

        String url = "jdbc:mysql://localhost:3306/adve";
        Connection conn = DriverManager.getConnection(url,"root","sa");
        Statement st = conn.createStatement();
        rs = st.executeQuery(query);

        while(rs.next())
        {
        hours= rs.getString("Hours"); // This is where I can get the value for hours and to be passed on a label
        }
         up.jLabel12.setText(hours); //I set on a Jlabel for the next frame
        }
        catch(SQLException e){
    JOptionPane.showMessageDialog(null, "Please select a record to update");
    }

}


第二个JFRAME:

  public void set(){ //THIS SUPPOSE TO SET THE BUTTONS BASED ON THE VALUE OF THE LABEL
if (jLabel12.getText().equals("12-Hours")) { // if 12-Hours, Rdn12 should be true or selected
   Rdn12.isSelected();
   Rdn12.setSelected(true);
   Rdn24.setSelected(false);
 }
 else if (jLabel12.getText().equals("24-Hours")) { // if 24-Hours, Rdn24 should be true or selected
   Rdn12.setSelected(false);
   Rdn24.setSelected(true);
 }
  jTextField1.setEditable(false);
  jLabel20.setVisible(false);

}


但是,单选按钮仍然不会被选中。我究竟做错了什么?有什么建议么?请帮忙。

最佳答案

您应该执行以下操作:

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String hours = null;
 try
    {
        DbUpdate up = new DbUpdate();

        int get = (int)jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
        String query= "SELECT * FROM roominfo WHERE CustomerNo = '"+get+"' " ;

        String url = "jdbc:mysql://localhost:3306/adv";
        Connection conn = DriverManager.getConnection(url,"root","sa");
        Statement st = conn.createStatement();
        rs = st.executeQuery(query);


        while(rs.next())
        {
         hours= rs.getString("Hours");
}
 if (hours.equals("12-Hours")) {
        // you can change the fields of the second frame directly in here
        up.Rdn12.isSelected();
        up.Rdn12.setSelected(true);
        up.Rdn24.setSelected(false);

    } else if (hours.equals("24-Hours")) {
        up.Rdn24.isSelected();
        up.Rdn12.setSelected(false);
        up.Rdn24.setSelected(true);
    }

    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null, "Please select a record to update");
    }


}

09-25 21:22