首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JFrame不显示任何内容

JFrame不显示任何内容
EN

Stack Overflow用户
提问于 2018-12-14 06:02:54
回答 2查看 42关注 0票数 0

我正试着在图形用户界面上做一个RPG游戏,但进展不是很顺利。在我添加JButton之前,一切都正常工作,并在窗口中正常显示。我不确定在我添加JButton之后发生了什么。标题应该显示在灰色区域,按钮显示在蓝色区域。我尝试过正常运行和使用调试器运行,但没有显示任何文本或按钮。我正在一步一步地遵循here教程,我没有看到任何不对劲的地方。(我知道我已经更改了变量名)。

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

代码语言:javascript
复制
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);

    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-14 21:15:50

我的一个朋友主动提出解决这个问题。他添加了一个叫做“扩展画布”的东西。

修复后的版本如下:

代码语言:javascript
复制
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);

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

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

票数 -1
EN

Stack Overflow用户

发布于 2018-12-15 15:29:00

不要遵循教程教你使用空布局管理器和“手动”设置边界。这不是一个好的做法。

从代码中移除所有边界设置。而不是使用Layout Managers,这就是他们所做的,动态地为您设置界限:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53770715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档