竞赛比赛计划

时间:2015-05-25 16:49:51

标签: java sql swing

我正在为我的大学的一个问答比赛写一个程序,类似于"谁想成为百万富翁"或者" Kaun Banega Crorepati"。 我正在从总共有6列的数据库中检索问题; 1表示问题,4表示选项,1表示正确答案。问题是JLabel的形式和JButton形式的选项。另外,我有Check和Next JButtons。 但是当我尝试从数据库中检索数据时,我得到一个NullPointerException。我在这里发布了代码。

我希望你们能帮助我解决这个问题,这样我就能在学院里为学生成功举办问答比赛。

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.sql.*;

public class KBP{
    public static void main(String[] argv){
        JFrame jf = new Frames();
        jf.pack();
        jf.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        jf.setForeground(Color.WHITE);
        jf.setVisible(true);
    }
}

class Frames extends JFrame implements ActionListener{
    Toolkit tk = Toolkit.getDefaultToolkit();
    ImageIcon logo = new ImageIcon("logo.jpg");

    JLabel questions;
    JButton option1;
    JButton option2;
    JButton option3;
    JButton option4;
    JButton check = new JButton("Check");
    JButton next = new JButton("Next");

    Statement st = null;
    Connection con = null;
    ResultSet rs = null;

    public Frames(){
        super();
        setDefaultCloseOperation(3);
//      System.out.println("                   "+number());


        setSize(((int) tk.getScreenSize().getWidth()),((int) tk.getScreenSize().getHeight()));
        System.out.println("width is             "+getWidth());
        System.out.println("height is            "+getHeight());

        setIconImage(logo.getImage());
        setTitle("Kaun Banega PhysicsPati");

        getContentPane().add(questions);
        questions = new JLabel("Question "+number()+":    "+question(), SwingConstants.CENTER);
        questions.setBounds(0,(int)(getHeight()/5), getWidth(),60);
        System.out.println("bounds of question is            "+questions.getBounds().toString());
        questions.setFont(new Font("ARIAL",1,20));
        questions.setForeground(Color.RED);

        getContentPane().add(option1);
        option1 = new JButton(first());
        option1.setBounds(100,(int)(2*getHeight()/5),500,40);
        System.out.println("bounds of option 1 is            "+option1.getBounds().toString());
//      option1.setBounds((int)(getWidth()/100),(int)(2*getHeight()/5),(int)(2*getWidth()/5)-(int)(getWidth()/100),40);
        option1.setBackground(Color.CYAN);
        option1.addActionListener(this);

        getContentPane().add(option2);
        option2 = new JButton(second());
        option2.setBounds(750,(int)(2*getHeight()/5),500,40);
        System.out.println("bounds of option 2 is            "+option2.getBounds().toString());
//      option2.setBounds((int)(getWidth()/100)+(int)(getWidth()/2),(int)(2*getHeight()/5),(int)(2*getWidth()/5)-(int)(getWidth()/100),40);
        option2.setBackground(Color.CYAN);
        option2.addActionListener(this);

        getContentPane().add(option3);
        option3 = new JButton(third());
        option3.setBounds(100,(int)(3*getHeight()/5),500,40);
        System.out.println("bounds of option 3 is            "+option3.getBounds().toString());
//      option3.setBounds((int)(getWidth()/100),(int)(3*getHeight()/5),(int)(2*getWidth()/2)-(int)(getWidth()/100),40);
        option3.setBackground(Color.CYAN);
        option3.addActionListener(this);

        getContentPane().add(option4);
        option4 = new JButton(fourth());
        option4.setBounds(750,(int)(3*getHeight()/5),500,40);
        System.out.println("bounds of option 4 is            "+option4.getBounds().toString());
//      option4.setBounds((int)(getWidth()/100)+(int)(getWidth()/2),(int)(3*getHeight()/5),(int)(getWidth()/2)-(int)(getWidth()/100),40);
        option4.setBackground(Color.CYAN);
        option4.addActionListener(this);

        getContentPane().add(check);
        check.setBounds((int)(getWidth()/2),(int)(4*getHeight()/5),100,40);
        System.out.println("bounds of check is            "+check.getBounds().toString());
//      check.setSize(100,40);
        check.addActionListener(this);
        check.setVisible(false);


        getContentPane().add(next);
        next.setBounds((int)(getWidth()/2),(int)(4*getHeight()/5),100,40);
        System.out.println("bounds of next is            "+next.getBounds().toString());
//      next.setBounds(683,614,100,40);
        next.addActionListener(this);
//      next.addActionListener(this);
        next.setVisible(false);
        next.setBackground(Color.PINK);

    }

    public void connect(){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("after classforname");
            con = DriverManager.getConnection("jdbc:odbc:quiz","","");
            System.out.println("after drivermanager");
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM Questions");
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
        }
    }

    public int number(){
        int n =0;
        connect();
        try{
            if (rs.next()){
                n = rs.getInt(1)-1;
            }

        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
        }
        return n;
    }

    public String question(){
        String q = "";
        connect();
        try{
            if (rs.next()){
                q = rs.getString(2);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
        }
        return q;
    }

    public String first(){
        String f = "";
        connect();
        try{
            if (rs.next()){
                f = "A                      "+rs.getString(3);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e4){
//              System.out.println("Exceptions caught : " + e4.toString());
            }
        }
        return f;
    }

    public String second(){
        String s = "";
        connect();
        try{
            if (rs.next()){
                s = "B                      "+rs.getString(4);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e4){
//              System.out.println("Exceptions caught : " + e4.toString());
            }
        }
        return s;
    }

    public String third(){
        String t = "";
        connect();
        try{
            if (rs.next()){
                t = "C                      "+rs.getString(5);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e4){
//              System.out.println("Exceptions caught : " + e4.toString());
            }
        }
        return t;
    }

    public String fourth(){
        String f = "";
        connect();
        try{
            if (rs.next()){
                f = "D                      "+rs.getString(6);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e4){
//              System.out.println("Exceptions caught : " + e4.toString());
            }
        }
        return f;
    }

    public String correct(){
        String c = "";
        connect();
        try{
            if (rs.next()){
                c = rs.getString(7);
            }
        }catch(Exception e){
//          System.out.println("Exception caught : " + e.toString());
        }finally{
            try{
                if(con!=null){
                    con.close();
                } 
            }catch (Exception e2){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(rs!=null){
                    rs.close();
                } 
            }catch (Exception e3){
//              System.out.println("Exceptions caught : " + e2.toString());
            }
            try{
                if(st!=null){
                    st.close();
                } 
            }catch (Exception e4){
//              System.out.println("Exceptions caught : " + e4.toString());
            }
        }
        return c;
    }

    public void actionPerformed(ActionEvent ae){
        if (ae.getSource()==option1){
            option1.setBackground(Color.YELLOW);
            option1.removeActionListener(this);
            option2.removeActionListener(this);
            option3.removeActionListener(this);
            option4.removeActionListener(this);
            check.setVisible(true);
            System.out.println("bound of check is            "+check.getBounds().toString());
            System.out.println(option1.getText());
        }else if (ae.getSource()==option2){
            option2.setBackground(Color.YELLOW);
            option1.removeActionListener(this);
            option2.removeActionListener(this);
            option3.removeActionListener(this);
            option4.removeActionListener(this);
            check.setVisible(true);
            System.out.println("bound of check is            "+check.getBounds().toString());
        }else if (ae.getSource()==option3){
            option3.setBackground(Color.YELLOW);
            option1.removeActionListener(this);
            option2.removeActionListener(this);
            option3.removeActionListener(this);
            option4.removeActionListener(this);
            check.setVisible(true);
            System.out.println("bound of check is            "+check.getBounds().toString());
        }else if (ae.getSource()==option4){
            option4.setBackground(Color.YELLOW);
            option1.removeActionListener(this);
            option2.removeActionListener(this);
            option3.removeActionListener(this);
            option4.removeActionListener(this);
            check.setVisible(true);
            System.out.println("bound of check is            "+check.getBounds().toString());
        }else if (ae.getSource()==check){
            option1.removeActionListener(this);
            option2.removeActionListener(this);
            option3.removeActionListener(this);
            option4.removeActionListener(this);
            check.setVisible(false);
            System.out.println("bounds of check is            "+check.getBounds().toString());
            System.out.println("Checked");
            System.out.println("bound of next is            "+next.getBounds().toString());         
            next.setVisible(true);

            connect();

            if (option1.getText().contains(correct())){
                option1.setBackground(Color.GREEN);
            }else if (option2.getText().contains(correct())){
                option2.setBackground(Color.GREEN);
            }else if (option3.getText().contains(correct())){
                option3.setBackground(Color.GREEN);
            }else if (option4.getText().contains(correct())){
                option4.setBackground(Color.GREEN);
            }

        }else if (ae.getSource()==next){
            System.out.println("Next");
            System.out.println("bounds of next is            "+next.getBounds().toString());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我正在粘贴新代码。除了JButton" Next"以外,一切正常。即使我评论除了这个JButton之外的所有行。

String format with  [INFO] color = rgba(102, 0, 255, 0.506)