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();
}
}
});
}
}