【问题标题】:Java GUI not loading inJava GUI 未加载
【发布时间】:2015-04-26 13:22:39
【问题描述】:

所以我正在为我的大学项目创建一种王牌游戏,但在尝试加载界面时遇到了困难。框架本身加载了不同的颜色(用于测试背景),但我的按钮和进度条都没有加载。我对 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...");
    }
}
}

【问题讨论】:

  • 也使用SwingUtilities.invokeLater() 创建框架。
  • 变量名不应以大写字符开头。除了(GenerateCrimeBtn、DisguiseBtn、SleepBtn)之外,您的大多数变量都是正确的。保持一致!
  • 考虑使用 Swing Timer 而不是 java.util.Timer

标签: java swing user-interface timer


【解决方案1】:
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");

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

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

【讨论】:

  • 试过了,根本加载不出来。我收到以下错误。 Edit 原来我无法发布错误,因为它超出了字符数限制。
  • 堆栈跟踪的前几行告诉您程序中导致错误的行。
  • 我尝试将 mainWindow.setVisible(true) 进一步向下移动,但框架根本不会出现。
  • @Cryte,见编辑。如果您仍然遇到问题,请进行所有建议的更改并重新发布显示更改的新代码。
【解决方案2】:
mainWindow.setVisible(true);

应该在最后

【讨论】:

    【解决方案3】:

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

    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 构造函数没有执行所有代码。

    附言 您必须使您的 JFrameGameInterface 在 MainClass.main(...) 方法中可见,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2013-03-18
      • 2011-07-31
      • 1970-01-01
      • 2012-09-22
      相关资源
      最近更新 更多