Untitled
java
2 months ago
5.9 kB
5
Indexable
Never
import java.awt.*; import java.awt.event.*; import javax.swing.*; class OnlineTest extends JFrame implements ActionListener { JButton btnNext, btnBookmark; JLabel label; ButtonGroup bg; int m[] = new int[15]; JRadioButton radioButton[] = new JRadioButton[5]; int current = 0, count = 0, x = 1, y = 1, now = 0; OnlineTest(String s) { super(s); label = new JLabel(); add(label); bg = new ButtonGroup(); for (int i = 0; i < 5; i++) { radioButton[i] = new JRadioButton(); add(radioButton[i]); bg.add(radioButton[i]); } btnNext = new JButton("Next Question"); btnBookmark = new JButton("Bookmark Question"); btnNext.addActionListener(this); btnBookmark.addActionListener(this); add(btnNext); add(btnBookmark); set(); label.setBounds(30, 40, 450, 20); radioButton[0].setBounds(50, 80, 450, 20); radioButton[1].setBounds(50, 110, 200, 20); radioButton[2].setBounds(50, 140, 200, 20); radioButton[3].setBounds(50, 170, 200, 20); btnNext.setBounds(100, 240, 150, 30); btnBookmark.setBounds(270, 240, 200, 30); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); setLocation(250, 100); setVisible(true); setSize(700, 600); Color c = new Color(255, 150, 0); getContentPane().setBackground(c); } /* handle all event */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btnNext) { if (qcheck()) count = count + 1; current++; set(); if (current == 14) { btnNext.setEnabled(false); btnBookmark.setText("Result"); } } if (e.getActionCommand().equals("Bookmark Question")) { JButton bk = new JButton("Bookmark " + x); bk.setBounds(480, 20 + 30 * x, 150, 30); add(bk); bk.addActionListener(this); m[x] = current; x++; current++; set(); if (current == 14) btnBookmark.setText("Score"); setVisible(false); setVisible(true); } for (int i = 0, y = 1; i < x; i++, y++) { if (e.getActionCommand().equals("Bookmark" + y)) { if (qcheck()) count = count + 1; now = current; current = m[y]; set(); ((JButton) e.getSource()).setEnabled(false); current = now; } } if (e.getActionCommand().equals("Result")) { if (qcheck()) count = count + 1; current++; JOptionPane.showMessageDialog(this, "You have answered " + count + " Questions correctly"); System.exit(0); } } /* SET Questions with options */ void set() { radioButton[4].setSelected(true); if (current == 0) { label.setText("Que1: Who invented C++?"); radioButton[0].setText("Dennis Ritchie"); radioButton[1].setText("Ken Thompson"); radioButton[2].setText("Brian Kernighan"); radioButton[3].setText("Bjarne Stroustrup"); } // ... Other question setup code ... label.setBounds(30, 40, 450, 20); for (int i = 0, j = 0; i <= 90; i += 30, j++) radioButton[j].setBounds(50, 80 + i, 200, 20); } /* declare right answers. */ boolean qcheck() { if (current == 0) return (radioButton[3].isSelected()); // ... Other answer checks ... return false; } public static void main(String s[]) { SwingUtilities.invokeLater(() -> { JFrame loginFrame = new JFrame("Login"); JTextField usernameField = new JTextField(); JPasswordField passwordField = new JPasswordField(); JButton loginButton = new JButton("Login"); JPanel panel = new JPanel(); panel.setLayout(null); JLabel usernameLabel = new JLabel("Username:"); usernameLabel.setBounds(50, 30, 80, 25); panel.add(usernameLabel); JLabel passwordLabel = new JLabel("Password:"); passwordLabel.setBounds(50, 70, 80, 25); panel.add(passwordLabel); usernameField.setBounds(130, 30, 100, 25); panel.add(usernameField); passwordField.setBounds(130, 70, 100, 25); panel.add(passwordField); loginButton.setBounds(100, 120, 80, 25); panel.add(loginButton); loginFrame.add(panel); loginFrame.setSize(300, 200); loginFrame.setLocationRelativeTo(null); loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); loginFrame.setVisible(true); loginButton.addActionListener(e -> { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); // Add your authentication logic here, e.g., checking against a predefined username and password if (username.equals("yourUsername") && password.equals("yourPassword")) { loginFrame.dispose(); // Close the login window new OnlineTest("Online Exam Portal"); } else { JOptionPane.showMessageDialog(loginFrame, "Invalid login credentials"); } }); }); } }