介绍:
本次,制作答题系统,要求能够重置题目,提交题目,显示题目,一个页面至少含有五个题目。
照片展示
1.主界面:
代码部分:
数据库操作:
import java.sql.*; import java.util.Scanner; public class SQL { public Connection conn = null; //各个数据库资源初始化 public PreparedStatement psm = null; public Statement stm = null; public ResultSet rs = null; public String user = "root"; //用户和密码 public String password = "123456"; public String url = "jdbc:mysql://localhost:3306/timu?serverTimezone=GMT%2B8&userSSL=false"; //连接数据库的地址 public SQL() { try { Class.forName("com.mysql.cj.jdbc.Driver"); //加载驱动 conn = DriverManager.getConnection(url, user, password); //建立连接 } catch (ClassNotFoundException | SQLException ex) { ex.printStackTrace(); } } public ResultSet select(){ try { String sql = "SELECT * FROM select_data"; //SQL语句 psm = conn.prepareStatement(sql); rs = psm.executeQuery(); //执行sql语句 return rs; } catch (SQLException e) { System.out.println("操作异常"); e.printStackTrace(); return null; } } }
这是连接数据库以及读取出数据库内题目的代码,会返回一个结果集。
主函数:
public class Main { public static void main(String[] args) { SQL sql = new SQL(); //创建SQL对象,对数据库进行操作 Menu menu = new Menu(sql.select()); //创建一个界面类对象,生成GUI界面。 } }
界面设计:
1.定义所需要的变量。
public String label = null; public ResultSet rs = null; public ButtonGroup[] buttonGroups = new ButtonGroup[101]; public JRadioButton[] jRadioButtons = new JRadioButton[401]; public JLabel[] jLabels = new JLabel[101]; public String[] result = new String[101];
2.重写构造函数
public Menu(ResultSet rs) {
this.rs = rs;
createMenu();
}
3.设计界面过程以及显示操作过程。
public void createMenu() { //界面创建 JFrame jf = new JFrame("题目界面"); //创建页面 jf.setSize(1000, 800); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置可关闭 jf.setLocationRelativeTo(null);//窗口居中 jf.setLayout(null); //自定义界面 jf.setVisible(true); jf.setResizable(false); JLabel demo01 = new JLabel(); //标题 jf.add(demo01); demo01.setText("答题系统"); demo01.setFont(new Font("微软雅黑", Font.BOLD,50)); demo01.setBounds(400,50,400,80); JLabel demo02 = new JLabel(); //说明 jf.add(demo02); demo02.setText("(答题期间不能切换屏幕,否则系统将会给予警告!)"); demo02.setFont(new Font("微软雅黑",Font.BOLD,20)); demo02.setBounds(260,130,600,50); JScrollPane js = new JScrollPane(); //滚动条 js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); js.setBounds(10,200,980,450); JPanel jp = new JPanel(); //创建一个子页面,存放题目 js.setViewportView(jp); jf.add(js); jp.setLayout(null); js.setVisible(true); int i = 0; int j = 0; int sum = 0; try { while (rs.next()){ //生成题目,并且创建单选选择题 int x = 50; jLabels[i] = new JLabel(String.valueOf(i + 1) + "、" + rs.getString("tihao")); //题目 jp.add(jLabels[i]); jLabels[i].setBounds(20,i*90,600,40); jLabels[i].setFont(new Font("微软雅黑",Font.BOLD,20)); result[i] = rs.getString("result"); //存结果 buttonGroups[i] = new ButtonGroup(); //组 jRadioButtons[j] = new JRadioButton("A." + rs.getString("timuA")); //选项 jp.add(jRadioButtons[j]); buttonGroups[i].add(jRadioButtons[j]); jRadioButtons[j].setActionCommand(rs.getString("timuA")); jRadioButtons[j].setBounds((j % 4)*200 + 20,i*90 + 40,200,30); jRadioButtons[j++].setFont(new Font("微软雅黑",Font.BOLD,20)); jRadioButtons[j] = new JRadioButton("B." + rs.getString("timuB")); jp.add(jRadioButtons[j]); buttonGroups[i].add(jRadioButtons[j]); jRadioButtons[j].setActionCommand(rs.getString("timuB")); jRadioButtons[j].setBounds((j % 4)*200 + 20,i*90 + 40,200,30); jRadioButtons[j++].setFont(new Font("微软雅黑",Font.BOLD,20)); jRadioButtons[j] = new JRadioButton("C." + rs.getString("timuC")); jp.add(jRadioButtons[j]); buttonGroups[i].add(jRadioButtons[j]); jRadioButtons[j].setActionCommand(rs.getString("timuC")); jRadioButtons[j].setBounds((j % 4)*200 + 20,i*90 + 40,200,30); jRadioButtons[j++].setFont(new Font("微软雅黑",Font.BOLD,20)); jRadioButtons[j] = new JRadioButton("D." + rs.getString("timuD")); jp.add(jRadioButtons[j]); buttonGroups[i].add(jRadioButtons[j]); jRadioButtons[j].setActionCommand(rs.getString("timuD")); jRadioButtons[j].setBounds((j % 4)*200 + 20,i*90 + 40,200,30); jRadioButtons[j++].setFont(new Font("微软雅黑",Font.BOLD,20)); i++; sum++; } } catch (SQLException e) { e.printStackTrace(); } jp.setPreferredSize(new Dimension(600,sum*90)); JButton jButton = new JButton("提交"); //提交按钮 jf.add(jButton); jButton.setFont(new Font("微软雅黑",Font.BOLD,20)); jButton.setBounds(150,670,80,40); JButton jButton01 = new JButton("重新答题"); //重置按钮 jf.add(jButton01); jButton01.setFont(new Font("微软雅黑",Font.BOLD,20)); jButton01.setBounds(400,670,120,40); JLabel jLabel = new JLabel(); //显示答对的题目标签 jf.add(jLabel); jLabel.setText("答对的问题数: "); jLabel.setFont(new Font("微软雅黑",Font.BOLD,23)); jLabel.setBounds(650,670,230,40); int finalSum = sum; jButton01.addActionListener(new AbstractAction() { //重置事件 @Override public void actionPerformed(ActionEvent e) { for (int k = 0; k < finalSum; k++){ buttonGroups[k].clearSelection(); } jLabel.setText("答对的问题数: "); jf.setVisible(false); jf.setVisible(true); } }); jButton.addActionListener(new AbstractAction() { //提交事务 @Override public void actionPerformed(ActionEvent e) { int score = 0; int idx = 0; for (int k = 0;k < finalSum;k++){ if (buttonGroups[k].getSelection() == null){ JOptionPane.showMessageDialog(null,"有部分题目没有选择,请仔细检查后重新提交!"); idx = 1; break; } if (buttonGroups[k].getSelection().getActionCommand().equals(result[k])){ score++; } } System.out.println(score); if (idx == 0){ int i = JOptionPane.showConfirmDialog(null,"你是否要提交吗,提交后成绩不能修改!", "请确定",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (i == 0){ jLabel.setText("答对的问题数: " + String.valueOf(score)); } } } }); }