Eine Prozentanzeige im Quiz Java JFrame |
17.05.2017, 20:42 | Auf diesen Beitrag antworten » |
Zoro77 | Eine Prozentanzeige im Quiz Java JFrame Meine Frage: Liebe Community, ich habe ein Projekt in der Schule. Wir programmieren grade ein Quiz mit dem Java Editior. Wir verwenden dabei das JFrame. Ich habe eine Frage und zwar will ich eine Prozentanzeige erstellen, die immer bei jeder richtigen Antwort vorankommt und bei falscher Antwort gleichbleibt. Am ende des Quizes soll nach der letzten Frage, also in einem neuen Fenster: Ihr Ergebnis ist (zum Beispiel) 40%. Das geht noch besser. Sie sollten mehr Nachrichten lesen Hier ist mein Code der funktioniert nur die Prozentanzeige fehlt. Das hier ist nun das Hauptprogramm: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * * Beschreibung * * @version 1.0 vom 05.05.2017 * @author */ public class QUIZZ extends Frame { // Anfang Attribute private JButton jButton1 = new JButton(); private JLabel jLabel1 = new JLabel(); private JLabel jLabel2 = new JLabel(); // Ende Attribute public QUIZZ() { // Frame-Initialisierung super(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); } }); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setTitle("Formular"); setResizable(false); Panel cp = new Panel(null); add(cp); // Anfang Komponenten jButton1.setBounds(104, 136, 75, 25); jButton1.setText("Los geht's"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1_ActionPerformed(evt); } }); cp.add(jButton1); jLabel1.setBounds(56, 8, 168, 28); jLabel1.setText("Willkommen zu meinem Quiz"); cp.add(jLabel1); jLabel2.setBounds(40, 48, 206, 20); jLabel2.setText("Kannst du alle fragen beantworten?"); cp.add(jLabel2); // Ende Komponenten setVisible(true); } // end of public QUIZZ // Anfang Methoden public static void main(String[] args) { new QUIZZ(); } // end of main public void jButton1_ActionPerformed(ActionEvent evt) { new Erste_Frage(); // TODO hier Quelltext einfügen } // end of jButton1_ActionPerformed // Ende Methoden } // end of class QUIZZ Das hier ist die 1. Frage: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * * Beschreibung * * @version 1.0 vom 05.05.2017 * @author */ public class Erste_Frage extends Frame { // Anfang Attribute private JLabel jLabel1 = new JLabel(); private JRadioButton jRadioButton1 = new JRadioButton(); private JRadioButton jRadioButton2 = new JRadioButton(); private JRadioButton jRadioButton3 = new JRadioButton(); private JRadioButton jRadioButton4 = new JRadioButton(); private ButtonGroup buttonGroup1 = new ButtonGroup(); private JButton jButton1 = new JButton(); // Ende Attribute public Erste_Frage() { // Frame-Initialisierung super(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); } }); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setTitle("Formular"); setResizable(false); Panel cp = new Panel(null); add(cp); // Anfang Komponenten buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); jLabel1.setBounds(48, 16, 200, 20); jLabel1.setText("Wie heißt Obama mit Vornamen?"); cp.add(jLabel1); jRadioButton1.setBounds(56, 72, 100, 20); jRadioButton1.setText("Barack"); jRadioButton1.setOpaque(false); buttonGroup1.add(jRadioButton1); cp.add(jRadioButton1); jRadioButton2.setBounds(56, 120, 100, 20); jRadioButton2.setText("Borack"); jRadioButton2.setOpaque(false); buttonGroup1.add(jRadioButton2); cp.add(jRadioButton2); jRadioButton3.setBounds(56, 160, 100, 20); jRadioButton3.setText("Barrack"); jRadioButton3.setOpaque(false); buttonGroup1.add(jRadioButton3); cp.add(jRadioButton3); jRadioButton4.setBounds(56, 200, 100, 20); jRadioButton4.setText("Barock"); jRadioButton4.setOpaque(false); buttonGroup1.add(jRadioButton4); cp.add(jRadioButton4); jButton1.setBounds(184, 112, 73, 33); jButton1.setText("Weiter"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1_ActionPerformed(evt); } }); cp.add(jButton1); //buttonGroup1.setBounds(16, 120, 33, 28); // Ende Komponenten setVisible(true); } // end of public Erste_Frage // Anfang Methoden public static void main(String[] args) { new Erste_Frage(); } // end of main public String buttonGroup1_getSelectedRadioButtonLabel() { for (java.util.Enumeration<AbstractButton> e = buttonGroup1.getElements(); e.hasMoreElements() { AbstractButton b = e.nextElement(); if (b.isSelected()) return b.getText(); } return ""; } public void jButton1_ActionPerformed(ActionEvent evt) { if (jRadioButton1.isSelected()){ jLabel1.setText("Richtig"); new ZweiteFrage(); } if (jRadioButton2.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } if (jRadioButton3.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } if (jRadioButton4.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } // TODO hier Quelltext einfügen } // end of jButton1_ActionPerformed // Ende Methoden } // end of class Erste_Frage Das hier ist die 2. Frage: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * * Beschreibung * * @version 1.0 vom 05.05.2017 * @author */ public class ZweiteFrage extends Frame { // Anfang Attribute private JLabel jLabel1 = new JLabel(); private ButtonGroup buttonGroup1 = new ButtonGroup(); private JRadioButton jRadioButton1 = new JRadioButton(); private JRadioButton jRadioButton2 = new JRadioButton(); private JRadioButton jRadioButton3 = new JRadioButton(); private JRadioButton jRadioButton4 = new JRadioButton(); private JButton jButton1 = new JButton(); // Ende Attribute public ZweiteFrage() { // Frame-Initialisierung super(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); } }); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y); setTitle("Formular"); setResizable(false); Panel cp = new Panel(null); add(cp); // Anfang Komponenten buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); jLabel1.setBounds(40, 16, 220, 20); jLabel1.setText("Was beinhaltet der (Mikro)Prozessor?"); cp.add(jLabel1); //buttonGroup1.setBounds(-8, 32, 33, 28); jRadioButton1.setBounds(32, 72, 180, 20); jRadioButton1.setText("Arbeitsspeicher"); jRadioButton1.setOpaque(false); cp.add(jRadioButton1); jRadioButton2.setBounds(32, 112, 164, 20); jRadioButton2.setText("Sound - und Grafikkarte"); jRadioButton2.setOpaque(false); cp.add(jRadioButton2); jRadioButton3.setBounds(32, 152, 180, 20); jRadioButton3.setText("Steuer und Rechenwerk"); jRadioButton3.setOpaque(false); cp.add(jRadioButton3); jRadioButton4.setBounds(32, 192, 100, 20); jRadioButton4.setText("Motherboard"); jRadioButton4.setOpaque(false); cp.add(jRadioButton4); jButton1.setBounds(208, 112, 75, 25); jButton1.setText("Weiter"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1_ActionPerformed(evt); } }); cp.add(jButton1); //buttonGroup1.setBounds(16, 120, 33, 28); // Ende Komponenten setVisible(true); } // end of public ZweiteFrage // Anfang Methoden public static void main(String[] args) { new ZweiteFrage(); } // end of main public String buttonGroup1_getSelectedRadioButtonLabel() { for (java.util.Enumeration<AbstractButton> e = buttonGroup1.getElements(); e.hasMoreElements() { AbstractButton b = e.nextElement(); if (b.isSelected()) return b.getText(); } return ""; } public void jButton1_ActionPerformed(ActionEvent evt) { if (jRadioButton3.isSelected()){ jLabel1.setText("Richtig"); new dritteFage(); } if (jRadioButton1.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } if (jRadioButton2.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } if (jRadioButton4.isSelected()){ JOptionPane.showMessageDialog(null,"Falsch","Fehler",JOptionPane.ERROR_MESS AGE); } // TODO hier Quelltext einfügen } // end of jButton1_ActionPerformed // Ende Methoden } // end of class ZweiteFrage Wo kann ich im Code jetzt etwas einbauen dass da nun eine Prozentanzeige erscheint. Falls ihr das ausprobieren wollt, speichert die 3 dateien in einem ordner und startet zuerst das Hauptprogramm QUIZZ Ich bedanke mich vielmals für jede Hilfe Meine Ideen: Ich weiß wirklich nicht wie das geht und einen Ansatz habe ich auch nicht also Bittee ich brauche eure Hilfe Dankeee |
|
|
Verwandte Themen
Die Beliebtesten » |
Die Größten » |
|
Die Neuesten » |
|