JFrame不显示任何内容

时间:2018-12-13 22:02:54

标签: swing user-interface jframe jbutton jlabel

我正在尝试在GUI中制作RPG游戏,但进展不太顺利。 在添加JButton之前,一切正常,并按应显示在窗口中。我不确定添加JButton后会发生什么。 应该在灰色区域显示标题,在蓝色区域显示按钮。香港专业教育学院试图正常运行并与调试器一起运行,没有显示任何文本或按钮。 我正在逐步按照教程here进行操作,但没有发现任何异常。 (我知道我已经更改了变量名)。

我在这里做错了什么?我需要添加任何额外的东西吗?

import javax.swing.*;
import java.awt.*;

public class Game {

    JFrame window;
    Container c;
    JPanel titlePanel;
    JPanel startButtonPanel;
    JLabel titleLabel;
    JButton startButton;
    Font titleFont = new Font("Cooper Black", Font.PLAIN, 90);
    Font buttonFont = new Font("Cooper Black", Font.PLAIN, 32);

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

    public Game() {

        //Main Window
        window = new JFrame();
        window.setSize(800, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(Color.BLACK);
        window.setLayout(null);
        window.setVisible(true);
        c = window.getContentPane();

        //Title Panel
        titlePanel = new JPanel();
        titlePanel.setBounds(100, 100, 600, 150);
        titlePanel.setBackground(Color.GRAY);

        titleLabel = new JLabel("TEXT RPG");
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(titleFont);

        //Start Button Panel
        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(300, 400, 200, 100 );
        startButtonPanel.setBackground(Color.BLUE);

        //Start Button
        startButton = new JButton("START");
        startButton.setBackground(Color.BLACK);
        startButton.setForeground(Color.WHITE);
        startButton.setFont(buttonFont);


        //Add Elements to Window
        titlePanel.add(titleLabel);
        startButtonPanel.add(startButton);

        //Add Elements to Container
        c.add(titlePanel);
        c.add(startButtonPanel);

    }
}

2 个答案:

答案 0 :(得分:1)

不要遵循本教程的内容,该教程教您使用空布局管理器并“手动”设置边界。这不是一个好习惯。
从代码中删除所有边界设置。 而是使用Layout Managers来做,为您动态设置界限:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game {

    JFrame window;
    Container c;
    JPanel titlePanel;
    JPanel startButtonPanel;
    JLabel titleLabel;
    JButton startButton;
    Font titleFont = new Font("Cooper Black", Font.PLAIN, 90);
    Font buttonFont = new Font("Cooper Black", Font.PLAIN, 32);

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

    public Game() {

        //Main Window
        window = new JFrame();
        window.setSize(800, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c = window.getContentPane();
        c.setBackground(Color.BLACK);
        //window.setLayout(null);

        //Title Panel
        titlePanel = new JPanel();   //JPanel uses FlowLayout by default
        //titlePanel.setBounds(100, 100, 600, 150)
        titlePanel.setBackground(Color.GRAY);


        titleLabel = new JLabel("TEXT RPG");
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(titleFont);

        //Start Button Panel
        startButtonPanel = new JPanel();
        //startButtonPanel.setBounds(300, 400, 200, 100 );
        startButtonPanel.setBackground(Color.BLUE);

        //Start Button
        startButton = new JButton("START");
        startButton.setBackground(Color.BLACK);
        startButton.setForeground(Color.WHITE);
        startButton.setFont(buttonFont);

        //Add Elements to Window
        titlePanel.add(titleLabel);
        startButtonPanel.add(startButton);

        //Add Elements to Container
        c.add(titlePanel, BorderLayout.CENTER);          //JFrame content pane uses BorderLayout by default
        c.add(startButtonPanel, BorderLayout.PAGE_END);

        window.pack();
        window.setVisible(true); //invoke after all added and pack() ed
    }
}

不要期望在初次尝试时获得准确的外观。了解如何使用不同的布局管理器及其组合来获得所需的内容。

答案 1 :(得分:-1)

我的一个朋友提供解决此问题的方法。他添加了一个名为“扩展画布”的东西。

此处为固定版本:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Game extends Canvas{
    private static final long serialVersionUID = 1L;
    public static JFrame window;
    public static Container c;
    JPanel titlePanel,startButtonPanel;
    JLabel titleLabel;
    JButton startButton;
    Font titleFont = new Font("Cooper Black", Font.PLAIN,90);
    private static int width = 800;
    private static int height = 600;
    public static String title ="Blueberry's Game";

    /*----------------------------------------------------------------------------------------------------*/
    public static void main(String[] args){
        Game game = new Game();
        Game.window.setResizable(false);
        Game.window.setTitle(Game.title);
        Game.window.add(game);
        Game.window.pack();
        Game.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Game.window.setLocationRelativeTo(null);
        Game.window.setVisible(true);
    }
    /*----------------------------------------------------------------------------------------------------*/
    public Game(){

        window = new JFrame();
        setPreferredSize(new Dimension(width, height));
        window.getContentPane().setBackground(Color.BLACK);
        c = window.getContentPane();

        //////////TITLE PANEL//////////////////////
        titlePanel = new JPanel();
        titlePanel.setBounds(100, 100, 600, 150);
        titlePanel.setBackground(Color.BLACK);
        titleLabel = new JLabel("TEXT RPG");
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setFont(titleFont);

        //////////START BUTTON PANEL//////////////////////
        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(300, 400, 200, 100);
        startButtonPanel.setForeground(Color.BLUE);

        //////////START BUTTON//////////////////////
        startButton = new JButton("START");
        startButton.setBackground(Color.BLACK);
        startButton.setForeground(Color.WHITE);

        //////////ADD ELEMENTS TO WINDOW//////////////////////
        titlePanel.add(titleLabel);
        startButtonPanel.add(startButton);

        //////////ADD ELEMENTS TO CONTAINER//////////////////////
        c.add(titlePanel);
        c.add(startButtonPanel);

    }
    /*----------------------------------------------------------------------------------------------------*/
}

编辑:我还设法查看了为什么窗口在我的问题中没有显示任何内容。我只需要用光标调整窗口大小即可。