codice:# import java.awt.*; import java.awt.event.*; # import javax.swing.*; # import javax.swing.event.*; # import java.util.StringTokenizer; # import java.io.*; # class SQuiz extends JFrame implements ItemListener { /*Variabili*/ Player player; //The player Questions questions; //The questions JLabel question; //The question JRadioButton ans1, ans2, ans3, ans4; //The answers ButtonGroup answers; //The Group of answers JButton Answer; //The Button to Answer at the question. JPanel qPanel, aPanel, APanel; //The Panels for the Game. JRadioButtonMenuItem settingsNormalRBMI, settingsFastRBMI; FileInputStream ladder; ActionListener questionsListeners = new ActionListener() { public void actionPerformed(ActionEvent ae) { try { JOptionPane.showMessageDialog(null, "In totale ci sono " + getTotQ() + " domande." "Domande Totali.", JOptionPane.INFORMATION_MESSAGE); } catch (HeadlessException he) {} } }; int Num; //Register the last num param of MakeGUI(); long start, end; //Used for currentTimeMillis() function. String topTen[] = new String[10]; //The TopTen ladder String topTenPoints[] = new String[10]; //The TopTen points. /** *S-Quiz constructor */ SQuiz() { setTitle("S-Quiz! Riuscirai ad arrivare alla fine?"); setSize(640, 320); setResizable(false); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { JOptionPane.showMessageDialog(null, "Arrivederci!", "S-Quiz", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); JMenuItem fileNewMenuItem = new JMenuItem("Nuova Partita", KeyEvent.VK_N); fileNewMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { newGame(); } }); fileMenu.add(fileNewMenuItem); JMenuItem fileOptionMenuItem = new JMenuItem("Opzioni", KeyEvent.VK_O); JMenuItem fileExitMenuItem = new JMenuItem("Esci", KeyEvent.VK_E); fileExitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, "Arrivederci!", "S-Quiz", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }); fileMenu.add(fileExitMenuItem); JMenu settingsMenu = new JMenu("Impostazioni"); settingsMenu.setMnemonic(KeyEvent.VK_I); settingsNormalRBMI = new JRadioButtonMenuItem("Gioco Normale", true); settingsNormalRBMI.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { settingsFastRBMI.setSelected(false); settingsNormalRBMI.setSelected(true); } }); settingsNormalRBMI.setMnemonic(KeyEvent.VK_N); settingsFastRBMI = new JRadioButtonMenuItem("Gioco Veloce", false); settingsFastRBMI.setMnemonic(KeyEvent.VK_V); settingsFastRBMI.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { settingsNormalRBMI.setSelected(false); settingsFastRBMI.setSelected(true); } }); settingsMenu.add(settingsNormalRBMI); settingsMenu.add(settingsFastRBMI); JMenu ladderMenu = new JMenu("Classifica"); ladderMenu.setMnemonic(KeyEvent.VK_C); JMenuItem ladderLadderMenuItem = new JMenuItem("Classifica", KeyEvent.VK_L); ladderLadderMenuItem.addActionListener(new ActionListener() ; public void actionPerformed(ActionEvent ae) { try { JOptionPane.showMessageDialog(null, " 1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" + " 2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" + " 3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" + " 4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" + " 5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" + " 6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" + " 7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" + " 8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" * " 9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" + "10- " + topTen[9] + ": " + topTenPoints[9] , "Classifica" ,JOptionPane.INFORMATION_MESSAGE); } catch (HeadlessException he) {} } }); ladderMenu.add(ladderLadderMenuItem); JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic(KeyEvent.VK_H); JMenuItem helpStatMenuItem = new JMenuItem("Statistiche", KeyEvent.VK_S); helpStatMenuItem.addActionListener(questionsListeners); helpMenu.add(helpStatMenuItem); JMenuItem helpAboutMenuItem = new JMenuItem("About", KeyEvent.VK_A); helpAboutMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { JOptionPane.showMessageDialog(null, "S-Quiz", "About", JOptionPane.INFORMATION_MESSAGE); } catch (HeadlessException he) {} } }); helpMenu.add(helpAboutMenuItem); menuBar.add(fileMenu); menuBar.add(settingsMenu); menuBar.add(ladderMenu); menuBar.add(helpMenu); setJMenuBar(menuBar); initLadder(); } /** *The main class that Start the Game */ public static void main(String args[]) { SQuiz squiz = new SQuiz(); squiz.setVisible(true); } /*Methods*/ public void newGame() { player = new Player("Unidentified-Error"); try { String tempName; tempName = JOptionPane.showInputDialog("Inserisci il tuo nome:"); player.setName(tempName); } catch (HeadlessException he) { } finally { questions = new Questions(player); GameStart(); } } public void GameStart() { questions.MakeQandA(questions); MakeGUI((int) questions.getCasualNumber()); } public void MakeGUI(final int num) { destroyGUI(); if (num != -1) { Num = num; setLayout(new BorderLayout()); question = new JLabel(questions.questions[player.getCurrentQ()-1][num]); qPanel = new JPanel(); qPanel.setBorder( BorderFactory.createTitledBorder(player.getName() + " - Punteggio + " + player.getPoints() + " - Domanda " + player.getCurrentQ())); qPanel.add(question); ans1 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4]); ans2 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 1]); ans3 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 2]); ans4 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 3]); ans1.addItemListener(this); ans2.addItemListener(this); ans3.addItemListener(this); ans4.addItemListener(this); aPanel = new JPanel(); aPanel.setBorder(BorderFactory.createTitledBorder("Risposte:")); aPanel.add(ans1); aPanel.add(ans2); aPanel.add(ans3); aPanel.add(ans4); answers = new ButtonGroup(); answers.add(ans1); answers.add(ans2); answers.add(ans3); answers.add(ans4); Answer = new JButton("Rispondi!"); Answer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if (settingsNormalRBMI.isSelected()) isExactly(num); } }); APanel = new JPanel(); APanel.setBorder(BorderFactory.createTitledBorder("Rispondi!")); APanel.add(Answer); add(qPanel, BorderLayout.NORTH); add(aPanel, BorderLayout.CENTER); add(APanel, BorderLayout.SOUTH); qPanel.repaint(); aPanel.repaint(); APanel.repaint(); repaint(); setVisible(true); start = System.currentTimeMillis(); } } private void isExactly(int num) { end = System.currentTimeMillis(); if (questions.exactly[player.getCurrentQ()-1][num].equals (ans1.getText()) && ans1.isSelected()) Continue(); else if (questions.exactly[player.getCurrentQ()-1][num].equals (ans2.getText()) && ans2.isSelected()) Continue(); else if (questions.exactly[player.getCurrentQ()-1][num].equals (ans3.getText()) && ans3.isSelected()) Continue(); else if (questions.exactly[player.getCurrentQ()-1][num].equals (ans4.getText()) && ans4.isSelected()) Continue(); else if (!ans1.isSelected() && !ans2.isSelected() && !ans3.isSelected() && !ans4.isSelected()) { try { JOptionPane.showMessageDialog(null, "Devi selezionare una risposta!", "Attenzione!", JOptionPane.WARNING_MESSAGE); } catch (HeadlessException he) {} } else Break(); }

Rispondi quotando