JBuild做考试系统平台,怎样接收单选框(A,B,C,D)值并怎样与试题数据库中的答案比较,
答案:3 悬赏:20 手机版
解决时间 2021-03-17 22:25
- 提问者网友:箛茗
- 2021-03-17 02:26
JBuild做考试系统平台,怎样接收单选框(A,B,C,D)值并怎样与试题数据库中的答案比较,
最佳答案
- 五星知识达人网友:毛毛
- 2021-03-17 03:31
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class test2 extends JFrame implements ActionListener{
JRadioButtonMenuItem jrbm=new JRadioButtonMenuItem();
JRadioButton rb_A=new JRadioButton();
JRadioButton rb_B=new JRadioButton();
JRadioButton rb_C=new JRadioButton();
JRadioButton rb_D=new JRadioButton();
ButtonGroup bg=new ButtonGroup();
JButton action=new JButton("确 定");
Container c=new Container();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="";
public test2(){
setTitle("考试系统");
setBounds(200,200,300,300);
String[] answer=select("1");
rb_A.setBounds(10,10,200,20);
rb_B.setBounds(10,50,200,20);
rb_C.setBounds(10,90,200,20);
rb_D.setBounds(10,130,200,20);
rb_A.setText(answer[0]);
rb_B.setText(answer[1]);
rb_C.setText(answer[2]);
rb_D.setText(answer[3]);
bg.add(rb_A);
bg.add(rb_B);
bg.add(rb_C);
bg.add(rb_D);
action.setBounds(60,200,100,30);
action.addActionListener(this);
c.add(rb_A);
c.add(rb_B);
c.add(rb_C);
c.add(rb_D);
c.add(action);
getContentPane().add(c);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConn(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public String[] select(String id){
String[] question=new String[4];
sql="select question_A,question_B,question_C,question_D from answer where id="+id;
try{
conn=getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
question[0]=rs.getString("question_A");
question[1]=rs.getString("question_B");
question[2]=rs.getString("question_C");
question[3]=rs.getString("question_D");
}
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
return question;
}
public void actionPerformed(ActionEvent e){
Object o=e.getSource();
String str="";
if(o==action){
if(rb_A.isSelected()){
str="A";
}
if(rb_B.isSelected()){
str="B";
}
if(rb_C.isSelected()){
str="C";
}
if(rb_D.isSelected()){
str="D";
}
int isTrue=check("1",str);
if(isTrue==1){
JOptionPane.showMessageDialog(null,"回答正确");
}else{
JOptionPane.showMessageDialog(null,"回答错误");
}
}
}
public int check(String id,String value){
int isTrue=0;
String choose="";
sql="select choose from answer where id="+id+"";
try{
conn=getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
choose=rs.getString("choose").trim();
}
if(choose.equals(value)){
isTrue=1;
}
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
return isTrue;
}
public static void main(String arg[]){
test2 tt=new test2();
tt.setVisible(true);
}
}
费了点时间,终于给你写好了,你看看把,我估计你是难在buttongroup读radio的值这一快了,这是我的做法,就是buuttonGroup只是确定在一个组里,读radio时用radio.selection()读出来就行了
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class test2 extends JFrame implements ActionListener{
JRadioButtonMenuItem jrbm=new JRadioButtonMenuItem();
JRadioButton rb_A=new JRadioButton();
JRadioButton rb_B=new JRadioButton();
JRadioButton rb_C=new JRadioButton();
JRadioButton rb_D=new JRadioButton();
ButtonGroup bg=new ButtonGroup();
JButton action=new JButton("确 定");
Container c=new Container();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="";
public test2(){
setTitle("考试系统");
setBounds(200,200,300,300);
String[] answer=select("1");
rb_A.setBounds(10,10,200,20);
rb_B.setBounds(10,50,200,20);
rb_C.setBounds(10,90,200,20);
rb_D.setBounds(10,130,200,20);
rb_A.setText(answer[0]);
rb_B.setText(answer[1]);
rb_C.setText(answer[2]);
rb_D.setText(answer[3]);
bg.add(rb_A);
bg.add(rb_B);
bg.add(rb_C);
bg.add(rb_D);
action.setBounds(60,200,100,30);
action.addActionListener(this);
c.add(rb_A);
c.add(rb_B);
c.add(rb_C);
c.add(rb_D);
c.add(action);
getContentPane().add(c);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Connection getConn(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","sa");
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public String[] select(String id){
String[] question=new String[4];
sql="select question_A,question_B,question_C,question_D from answer where id="+id;
try{
conn=getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
question[0]=rs.getString("question_A");
question[1]=rs.getString("question_B");
question[2]=rs.getString("question_C");
question[3]=rs.getString("question_D");
}
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
return question;
}
public void actionPerformed(ActionEvent e){
Object o=e.getSource();
String str="";
if(o==action){
if(rb_A.isSelected()){
str="A";
}
if(rb_B.isSelected()){
str="B";
}
if(rb_C.isSelected()){
str="C";
}
if(rb_D.isSelected()){
str="D";
}
int isTrue=check("1",str);
if(isTrue==1){
JOptionPane.showMessageDialog(null,"回答正确");
}else{
JOptionPane.showMessageDialog(null,"回答错误");
}
}
}
public int check(String id,String value){
int isTrue=0;
String choose="";
sql="select choose from answer where id="+id+"";
try{
conn=getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
choose=rs.getString("choose").trim();
}
if(choose.equals(value)){
isTrue=1;
}
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
return isTrue;
}
public static void main(String arg[]){
test2 tt=new test2();
tt.setVisible(true);
}
}
费了点时间,终于给你写好了,你看看把,我估计你是难在buttongroup读radio的值这一快了,这是我的做法,就是buuttonGroup只是确定在一个组里,读radio时用radio.selection()读出来就行了
全部回答
- 1楼网友:由着我着迷
- 2021-03-17 04:42
4个RadioButton添加到同一个RadioButtonGroup中,这样就可以保证4个中只能选取其中一个.
你可以自定义一个组件,继承于JRadioButton,添加一个属性,这个属性用来保存该RadioButton代表的选项(A/B/C/D),在判断时就可以取这个属性值与数据库比较.
你可以自定义一个组件,继承于JRadioButton,添加一个属性,这个属性用来保存该RadioButton代表的选项(A/B/C/D),在判断时就可以取这个属性值与数据库比较.
- 2楼网友:千杯敬自由
- 2021-03-17 04:01
这个要看你是通过jsp还是servlet来获取单选框的数值了,用jsp的话就是request("单选框name"),用servlet的话就是重写HttpServlet类的doGet方法,通过request.parameter("单选框name")获取
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯