import java.awt.event.*; import java.awt.*; import javax.swing.*; public class NumberGameFrame extends JFrame implements ActionListener { private JButton okButton; private JLabel outPutLabel; private JTextField inputTextField; private JProgressBar progressBar; private int zufall; private int anzahlVersuche; public NumberGameFrame() { okButton = new JButton("O.K."); inputTextField = new JTextField(5); outPutLabel = new JLabel("Raten Sie ein Zahl zwischen 1 und 100"); progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 10); progressBar.setStringPainted(true); progressBar.setString("Los gehts!"); JPanel panel = new JPanel(); panel.add(inputTextField); panel.add(okButton); Container c = getContentPane(); c.add(outPutLabel, BorderLayout.NORTH); c.add(panel, BorderLayout.CENTER); c.add(progressBar, BorderLayout.SOUTH); okButton.addActionListener(this); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); zufall = ((int)(Math.random()*100)); //System.out.println(zufall); } public static void main(String[] args) { NumberGameFrame f = new NumberGameFrame(); f.pack(); f.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = inputTextField.getText(); int zahl = 0; try { zahl = Integer.parseInt(s); } catch (NumberFormatException nfE) {} anzahlVersuche++; progressBar.setValue(anzahlVersuche); if (zahl != zufall) { if (zahl < zufall) { outPutLabel.setText("Die Zahl war zu klein"); } if (zahl > zufall) { outPutLabel.setText("Die Zahl war zu gross"); } inputTextField.setText(""); inputTextField.requestFocus(); int restVersuche = 10 - anzahlVersuche; progressBar.setString ("Sie haben noch " + restVersuche + " Versuche"); } else { outPutLabel.setText("Die Zahl ist: " + zufall); inputTextField.setEnabled(false); okButton.setEnabled(false); } } }