Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Quiz;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- public class MainWindow extends JFrame {
- private JPanel contentPane;
- private JButton anwser1;
- private JButton anwser2;
- private JPanel panel;
- private JLabel messageLabel;
- private JLabel messageLabel2;
- /**
- * Launch the application.
- */
- /**
- * Create the frame.
- */
- public MainWindow() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- JPanel panel = new JPanel();
- contentPane.add(panel, BorderLayout.CENTER);
- try{
- String url = "*************";
- String username = "*************";
- String password = "*************";
- Connection conn = DriverManager.getConnection(url,username,password);
- Statement st = conn.createStatement();
- st.executeQuery("select * from Quiz");
- ResultSet rs = st.getResultSet();
- while(rs.next()) {
- messageLabel = new JLabel(rs.getString("Question"));
- anwser1 = new JButton(rs.getString("Wrong"));
- anwser2 = new JButton(rs.getString("Answer"));
- anwser1.addActionListener(new ButtonListener());
- anwser2.addActionListener(new ButtonListener());
- panel = new JPanel();
- panel.add(messageLabel);
- panel.setBounds(100, 130, 150, 30);
- panel.add(anwser1);
- panel.add(anwser2);
- add(panel);
- setVisible(true);
- setTitle("Quiz");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- anwser1.addActionListener(new ButtonListener());
- anwser2.addActionListener(new ButtonListener());
- panel = new JPanel();
- panel.add(messageLabel);
- panel.add(anwser1);
- panel.add(anwser2);
- add(panel);
- setVisible(true);
- }
- catch(Exception e){
- System.out.println(e);
- }
- }
- private class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- try{
- String url = "*************";
- String username = "*********";
- String password = "************";
- Connection conn = DriverManager.getConnection(url,username,password);
- Statement st = conn.createStatement();
- st.executeQuery("select * from Quiz");
- ResultSet rs = st.getResultSet();
- while(rs.next()) {
- String actionCommand = e.getActionCommand();
- if (actionCommand.equals(rs.getString("Wrong")))
- {
- JOptionPane.showMessageDialog(null, "Wrong Answer");
- }
- else if (actionCommand.equals(rs.getString("Answer")))
- {
- JOptionPane.showMessageDialog(null, "Correct");
- }
- }
- }
- catch(Exception a){
- System.out.println(a);
- }
- }
- }
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- MainWindow frame = new MainWindow();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment