Java GUI未加载

时间:2015-04-25 22:26:54

标签: java swing user-interface timer

所以我为我的大学项目创造了一种特朗普游戏,但是当我尝试加载界面时遇到了困难。框架本身加载了不同的颜色(用于测试背景),但我的按钮和进度条都没有加载。我仍然是Java的新手,所以我可能错过了一些东西。任何帮助将不胜感激。

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...");
    }
}
}

3 个答案:

答案 0 :(得分:2)

mainWindow.setVisible(true);

在将所有组件添加到框架之后,这应该是构造函数中的最后一个语句。

编辑:

    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");

您需要在将组件添加到面板之前创建组件,否则变量为null,因此不会向面板添加任何内容。

添加到底部面板的组件的注释相同。

答案 1 :(得分:2)

mainWindow.setVisible(true);

应该在最后

答案 2 :(得分:1)

我已经审核了您的代码,下面是它的修复版本,我还评论了代码中存在的一些问题:

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...");
    }
}
}

请注意,您的窗口中没有显示任何内容,因为您的代码在NullPointerException后被破坏而您的JFrame构造函数未执行所有代码。

P.S。 您必须在MainClass.main(...)方法中显示JFrame GameInterface,如下所示:

public class TestApp {

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

}