Cryte
Cryte

Reputation: 3

Java GUI not loading in

So I'm creating a type of trump game for my University project but running into difficulty when I'm trying to load in the interface. The frame itself loads with the different colors (which were used to test the background) but none of my buttons nor progress bars are loading in. I am still fairly new to Java so I have probably missed something. Any help would be greatly appreciated.

package Game;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;

public class GameInterface extends JFrame{

private JFrame mainWindow;
private JPanel middlePanel, bottomPanel, allPanels;
private JButton GenerateCrimeBtn, DisguiseBtn, SleepBtn;
private JProgressBar energyBar, statusBar, actionBar;
private int interval;
private Timer timer;
private JLabel timerBarLabel, questionLabel;

public GameInterface(){
        middlePanel = new JPanel();
        middlePanel.setBackground(Color.gray);
        bottomPanel = new JPanel();
        allPanels = new JPanel();

        allPanels.setLayout(new BoxLayout(allPanels, BoxLayout.Y_AXIS));
        allPanels.add(middlePanel);
        allPanels.add(bottomPanel);

        mainWindow = new JFrame();
        mainWindow.setTitle("Game");
        mainWindow.setSize(1920,1080);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
        mainWindow.add(allPanels);

        middlePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        middlePanel.setLayout(new GridLayout(0,2,10,10));
        middlePanel.add(GenerateCrimeBtn);
        middlePanel.add(DisguiseBtn);
        middlePanel.add(SleepBtn);

        GenerateCrimeBtn = new JButton("Generate Crime");
        DisguiseBtn = new JButton("Disguise");
        SleepBtn = new JButton("Sleep");

        bottomPanel.add(energyBar);
        bottomPanel.add(timerBarLabel);
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        bottomPanel.setLayout(new GridLayout(0,2));

        timerBarLabel = new JLabel("Timer left to answer question; ");
        timerBarLabel.setForeground(Color.darkGray);
        timerBarLabel.setBounds(10,10,50,30);
        energyBar.setBounds(60,10,200,30);
        energyBar = new JProgressBar(0, 100);
}

public JButton getGenerateCrimeBtn() {
        return GenerateCrimeBtn;
}

public JButton getDisguiseBtn() {
        return DisguiseBtn;
}

public JButton getSleepBtn() {
        return SleepBtn;
}

public JPanel getPanels() {
        return allPanels;
}

public Timer getTimer(){
        return timer;
}

public void cancelInterval(){
        interval = -1;
}

public void resetTimer(){
    int delay = 100;
    int period = 100;
    interval = 100;
    energyBar.setValue(100);
    energyBar.repaint();
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask(){
            public void run(){
                    setInterval();
            }
    }, delay, period);
}

private void setInterval() {
    if (interval > 0) {
            interval--;
            energyBar.setValue(interval);
            energyBar.repaint();
    }else if (interval == 0){
            timer.cancel();
            JOptionPane.showMessageDialog(allPanels, "You have failed...");
    }
}
}

Upvotes: 0

Views: 109

Answers (3)

Abduliam Rehmanius
Abduliam Rehmanius

Reputation: 928

I have reviewed your code and below is fixed version of it, I have also commented on some issues that were present in your code:

package Game;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;

public class GameInterface extends JFrame{

//Potential problem: You don't probably need a main window here, as your GameInterface JFrmae is already acting as a mainWindow
//private JFrame mainWindow;
private JPanel middlePanel, bottomPanel, allPanels;
private JButton GenerateCrimeBtn, DisguiseBtn, SleepBtn;
private JProgressBar energyBar, statusBar, actionBar;
private int interval;
private Timer timer;
private JLabel timerBarLabel, questionLabel;

public GameInterface(){
        middlePanel = new JPanel();
        middlePanel.setBackground(Color.gray);
        bottomPanel = new JPanel();
        allPanels = new JPanel();

        allPanels.setLayout(new BoxLayout(allPanels, BoxLayout.Y_AXIS));
        allPanels.add(middlePanel);
        allPanels.add(bottomPanel);

        //Potential problem: GameInterface is already a JFrame subclass, so no need to create a 
        // new instance (mainWindow = new JF...) and that too is not being made visible anywhere
        //(you have to call jframeInstance.setVisible(true); to actually make it visible on screen)
        //mainWindow = new JFrame();
        this.setTitle("Game");
        this.setSize(640,360);//adjusted the window frame to fit on my monitor resolution
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.add(allPanels);

        //Potential problem, uninitialized components: initialize the components before they can be used
        GenerateCrimeBtn = new JButton("Generate Crime");
        DisguiseBtn = new JButton("Disguise");
        SleepBtn = new JButton("Sleep");

        timerBarLabel = new JLabel("Timer left to answer question; ");
        timerBarLabel.setForeground(Color.darkGray);
        timerBarLabel.setBounds(10,10,50,30);
        energyBar = new JProgressBar(0, 100);//Problem: first initialise the components, you have to instantiate (x = new X()) an object by executing its constructor before you can actually use it!
        energyBar.setBounds(60,10,200,30);

        middlePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        middlePanel.setLayout(new GridLayout(0,2,10,10));
        middlePanel.add(GenerateCrimeBtn);//problem: you actually added the buttons before initialising them!
        middlePanel.add(DisguiseBtn);
        middlePanel.add(SleepBtn);

        bottomPanel.add(energyBar);
        bottomPanel.add(timerBarLabel);
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        bottomPanel.setLayout(new GridLayout(0,2));
}

//all same code as your question below
public JButton getGenerateCrimeBtn() {
        return GenerateCrimeBtn;
}

public JButton getDisguiseBtn() {
        return DisguiseBtn;
}

public JButton getSleepBtn() {
        return SleepBtn;
}

public JPanel getPanels() {
        return allPanels;
}

public Timer getTimer(){
        return timer;
}

public void cancelInterval(){
        interval = -1;
}

public void resetTimer(){
    int delay = 100;
    int period = 100;
    interval = 100;
    energyBar.setValue(100);
    energyBar.repaint();
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask(){
            public void run(){
                    setInterval();
            }
    }, delay, period);
}

private void setInterval() {
    if (interval > 0) {
            interval--;
            energyBar.setValue(interval);
            energyBar.repaint();
    }else if (interval == 0){
            timer.cancel();
            JOptionPane.showMessageDialog(allPanels, "You have failed...");
    }
}
}

Note that nothing was being displayed in your window because your code was getting broken after a NullPointerException and your JFrame constructor wasn't executing all the code.

p.s. You will have to make the your JFrame the GameInterface visible in a MainClass.main(...) method somewhat like below:

public class TestApp {

    public static void main(String[] args) {
        GameInterface gi=new GameInterface();
        gi.setVisible(true);
    }

}

Upvotes: 1

camickr
camickr

Reputation: 324108

mainWindow.setVisible(true);

This should be the last statement in the constructor, after all the components have been added to the frame.

Edit:

    generateCrimeBtn = new JButton("Generate Crime");
    disguiseBtn = new JButton("Disguise");
    sleepBtn = new JButton("Sleep");

    middlePanel.add(generateCrimeBtn);
    middlePanel.add(disguiseBtn);
    middlePanel.add(sleepBtn);

    //GenerateCrimeBtn = new JButton("Generate Crime");
    //DisguiseBtn = new JButton("Disguise");
    //SleepBtn = new JButton("Sleep");

You need to create the component BEFORE adding it to the panel otherwise the variable is null, so nothing gets added to the panel.

Same comment for the components added to the bottom panel.

Upvotes: 2

jordanbana
jordanbana

Reputation: 97

mainWindow.setVisible(true);

Should be at the end

Upvotes: 2

Related Questions