Andriy Ukraine
Andriy Ukraine

Reputation: 27

For loop causing a black screen in JFrame(Eclipse)

Here's my code and I'm trying to do a project for school but it's not working for some odd reason whenever I add a loop it causes the second JFrame to be completely black.. It's supposed to switch between Buttons A B and C once it's clicked

Upvotes: 1

Views: 224

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Taking some wild guesses at you intentions, you are simply thinking to much in a linear fashion, a lot like you would for a console based application, but a GUI is a event driven environment. Something happens, you get told about, you react to.

Basically, you should be calling setVisible last, after you've prepared the base IU.

You need to get rid of your while-loop, it's not going to do what you want it to do. Instead, you need to use a single ActionListener which can, based on the value of x, determine what should happen when any of the buttons are clicked, for example:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class Game implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Game());
    }

    private int counter;
    private int x;

    @Override
    public void run() {
        // TODO Auto-generated method stub
        counter = 0;
        JFrame y = new JFrame("Game");
        y.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton start = new JButton("Start game");
        y.add(start, BorderLayout.SOUTH);
        y.pack();
        JLabel a = new JLabel();
        a.setText("<HTML><body><H1>Instructions</H1><p>Click the 'start' button to start the game" + "<br>Click as many of the red buttons as you can before time runs out!<p></body></HTML>");
        y.add(a);
        y.pack();
        y.setVisible(true);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Timer time = new Timer(10000, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("You got " + counter + " points");
                        System.exit(0);
                    }
                });
                time.start();
                JFrame z = new JFrame("Sequence game");
                FlowLayout fl = new FlowLayout(0, 50, 40);
                z.getContentPane().setLayout(fl);
                JButton a = new JButton("A");
                Font f = a.getFont();
                Font myFont = f.deriveFont(Font.BOLD, f.getSize() * 4);
                a.setSize(200, 100);
                a.setVisible(true);
                JButton b = new JButton("B");
                b.setVisible(true);
                b.setSize(200, 100);
                JButton c = new JButton("C");
                c.setVisible(true);
                c.setSize(200, 100);
                z.setSize(1280, 1024);
                z.getContentPane().add(a);
                z.getContentPane().add(b);
                z.getContentPane().add(c);
                Random r = new Random();

                x = r.nextInt(3);
                ActionListener listener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (x == 0 && e.getSource().equals(a)) {
                            counter++;
                            x = r.nextInt(3);
                            a.setFont(f);
                        } else if (x == 1 && e.getSource().equals(b)) {
                            counter++;
                            x = r.nextInt(3);
                            b.setFont(f);
                        } else if (x == 2 && e.getSource().equals(c)) {
                            counter++;
                            x = r.nextInt(3);
                            c.setFont(f);
                        }

                        switch (x) {
                            case 0:
                                a.setFont(myFont);
                                break;
                            case 1:
                                b.setFont(myFont);
                                break;
                            case 2:
                                b.setFont(myFont);
                                break;
                        }
                        z.revalidate();
                        z.repaint();
                    }
                };

                System.out.println(x);
                switch (x) {
                    case 0:
                        a.setFont(myFont);
                        break;
                    case 1:
                        b.setFont(myFont);
                        break;
                    case 2:
                        b.setFont(myFont);
                        break;
                }
                z.revalidate();
                z.repaint();

                a.addActionListener(listener);
                b.addActionListener(listener);
                c.addActionListener(listener);

                z.pack();
                z.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions