Problem bei Übergabe von Variablen

Zu anfang des Spiels will ich die Spielfeldgröße durch einen Knopfdruck übergeben .
Nur irgendwie schaff ich es nicht.
Was soll ich tun damit es klappt ?
Java:
    JPanel startpanel=new JPanel();
        JButton Leicht=new JButton("Leicht");
        JButton Mittel=new JButton("Mittel");
        JButton Schwierig=new JButton("Schwierig");
        JFrame fenster = new JFrame();
       
        int spielfeldgroesse;
       
        public  Startbildschirm() {
           
       
            startpanel.setLayout(new GridLayout(3,1));
            Leicht.addActionListener(this);
            Leicht.setActionCommand("Leicht");
            Mittel.addActionListener(this);
            Mittel.setActionCommand("Mittel");
            Schwierig.addActionListener(this);
            Schwierig.setActionCommand("Schwierig");
            startpanel.add(Leicht);
            startpanel.add(Mittel);
            startpanel.add(Schwierig);
            fenster.getContentPane().add(startpanel);
            fenster.setVisible(true);
            fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fenster.pack();
            fenster.setLocationRelativeTo(null);
     
        }
       
     
        public void actionPerformed(ActionEvent arg0) {
     
            String Schwierigkeit = arg0.getActionCommand();
            if(Schwierigkeit.equals("Leicht")){
                spielfeldgroesse = 25;
                Reactor rleicht = new Reactor(spielfeldgroesse);
            }
            if(Schwierigkeit.equals("Mittel")){
                spielfeldgroesse = 49;
                Reactor rmittel = new Reactor(spielfeldgroesse);
            }
            if(Schwierigkeit.equals("Schwierig")){
                spielfeldgroesse = 81;
                Reactor rschwierig = new Reactor(spielfeldgroesse);
            }
        }
        public static void main(String[] args) {
            Startbildschirm sb = new Startbildschirm();
     
        }
    }
Java:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Reactor  implements ActionListener  {
     
        JButton[] btns = new JButton[25];
        int punktzahl = 0;
        int spielfeldgroesse;
        int sekundenzähler = 0;
        int limit = 1000000000;
        int wartezeit = 1000;
       
       
        int mommentaneZahl ;
        boolean start = false;
        JLabel label=new JLabel();
        JPanel verliererpanel = new JPanel();
        JPanel unterverliererpanel = new JPanel();
        JPanel panel = new JPanel();
        JTextField text1=new JTextField("Du hast verloren !");
        JTextField text2=new JTextField("Deine Punktzahl :"+punktzahl+"");
        JPanel spielfeld = new JPanel();
        JFrame f = new JFrame();
        JButton button=new JButton("OK");
       
        public Reactor(int spielfeldgröße) {
     
            initializeButtons();
            int spielfeldlaenge = (int) Math.sqrt(spielfeldgröße);
           
            button.addActionListener(this);
            button.setActionCommand("OK");
            panel.setLayout(new BorderLayout());
            verliererpanel.setLayout(new GridLayout(2,1));
            unterverliererpanel.setLayout(new GridLayout(2,1));
            unterverliererpanel.add(text1);
            text1.setHorizontalAlignment(0);
            unterverliererpanel.add(text2);
            text2.setHorizontalAlignment(0);
            verliererpanel.add(unterverliererpanel,BorderLayout.CENTER);
            verliererpanel.add(button);
            spielfeld.setLayout(new GridLayout(spielfeldlaenge,spielfeldlaenge));
     
            for (int zeile = 0; zeile < spielfeldgröße; zeile++) {
     
                spielfeld.add(btns[zeile]);
     
                btns[zeile].addActionListener(this);
                btns[zeile].setBackground(Color.WHITE);
                btns[zeile].setText("" + (zeile) + "");
     
                btns[zeile].setActionCommand("" + zeile + "");
     
            }
            btns[((spielfeldgröße-1)/2)].setBackground(Color.RED);
            mommentaneZahl = ((spielfeldgröße-1)/2);
            panel.add(spielfeld, BorderLayout.CENTER);
            panel.add(label, BorderLayout.NORTH);
            f.getContentPane().add(panel);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
     
        }
     
        private void initializeButtons() {
            for (int a = 0; a < spielfeldgroesse; a++) {
                btns[a] = new JButton(" ");
            }
            while (start == true) {
                while (sekundenzähler < limit) {
             sekundenzähler++;
           
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               
                }
             }
        }
     
        @Override
        public void actionPerformed(ActionEvent arg0) {
     
            String gedrückteZahl = arg0.getActionCommand();
           
           
            if(gedrückteZahl.equals("OK")){
                Startbildschirm st=new Startbildschirm();
            }
           
           
            start = true;
            if (gedrückteZahl.equals(""+mommentaneZahl+"")) {
               
     
                punktzahl++;
                label.setText(""+punktzahl+"");
               
               
               
                if (punktzahl > 5) {
                    int zufallszahlextra1 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra1].setBackground(Color.BLUE);
                }
                if (punktzahl > 10) {
                    int zufallszahlextra2 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra2].setBackground(Color.GREEN);
                }
                if (punktzahl > 15) {
                    int zufallszahlextra3 = (int) (Math.random() * (spielfeldgroesse));
                    btns[zufallszahlextra3].setBackground(Color.YELLOW);
                }
                if (punktzahl > 20) {
                    int zufallszahlextra4 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra4].setBackground(Color.ORANGE);
                }
                if (punktzahl > 25) {
                    int zufallszahlextra5 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra5].setBackground(Color.BLACK);
                }
                if (punktzahl == 30) {
                    for (int zeile = 0; zeile < spielfeldgroesse; zeile++) {
                        btns[zeile].setBackground(Color.WHITE);
                }
                }
                if (punktzahl >30) {
                    int zufallszahlextra6 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra6].setBackground(Color.PINK);
                    int zufallszahlextra7 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra7].setBackground(Color.MAGENTA);
                }
                if (punktzahl >50) {
                    int zufallszahlextra8 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra8].setBackground(Color.CYAN);
                    int zufallszahlextra9 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra9].setBackground(Color.BLUE);
                }
                if (punktzahl >100) {
                    int zufallszahlextra8 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra8].setBackground(Color.CYAN);
                    int zufallszahlextra9 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra9].setBackground(Color.BLUE);
                    int zufallszahlextra6 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra6].setBackground(Color.PINK);
                    int zufallszahlextra10 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra10].setBackground(Color.MAGENTA);
                    int zufallszahlextra11 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra11].setBackground(Color.BLACK);
                    int zufallszahlextra12 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra12].setBackground(Color.GREEN);
                   
                }
                if (punktzahl >150) {
                    int zufallszahlextra13 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra13].setBackground(Color.CYAN);
                    int zufallszahlextra14= (int) (Math.random() * (spielfeldgroesse));
                    btns[zufallszahlextra14].setBackground(Color.BLUE);
                    int zufallszahlextra15 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra15].setBackground(Color.PINK);
                    int zufallszahlextra16 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra16].setBackground(Color.MAGENTA);
                    int zufallszahlextra17 = (int) (Math.random() * (spielfeldgroesse ));
                    btns[zufallszahlextra17].setBackground(Color.BLACK);
                    int zufallszahlextra18 = (int) (Math.random() * (spielfeldgroesse));
                    btns[zufallszahlextra18].setBackground(Color.GREEN);
                    int zufallszahlextra19 = (int) (Math.random() * (spielfeldgroesse));
                    btns[zufallszahlextra19].setBackground(Color.YELLOW);
                   
                }
                btns[mommentaneZahl].setBackground(Color.WHITE);
                int zufallszahl = (int) (Math.random() * (spielfeldgroesse));
                btns[zufallszahl].setBackground(Color.RED);
                mommentaneZahl = zufallszahl;
                System.out.print(""+sekundenzähler+"");
                sekundenzähler = 0;
            }
     
            else {
                for (int zeile = 0; zeile < spielfeldgroesse; zeile++) {
     
                    btns[zeile].removeActionListener(this);
                    start = false;
                    f.remove(panel);
                    f.add(verliererpanel, BorderLayout.CENTER);
                   
               
                }
            }
     
        }
    }
 

Neue Themen


Zurück
Oben