简体   繁体   English

我正在尝试用Java创建GUI。我创建了框架,但面板没有显示。 怎么了?

[英]I am trying to make a GUI in Java.I created the frame, but the panel doesn't show up. What is wrong?

When everything was in one class, it was working. 当一切都在一堂课时,它就在工作。 I tried to separate them to obtain a more "structured" code, but now only the frame shows up. 我试图分离它们以获得更“结构化”的代码,但是现在只显示框架。

My Main-class: 我的主班:

 public class Main {
    public static void main(String[] args) {
        GraphicInterface gameInterface;
        gameInterface = new GraphicInterface();
   }
}

GraphicInterface-class: GraphicInterface类:

public class GraphicInterface {
    public GraphicInterface() {
        GameFrame gameFrame = new GameFrame();
        MainPanel menuPanel = new MainPanel();
        gameFrame.addPanel(menuPanel);
        menuPanel.setVisible(true);
    }
}

GameFrame-class: GameFrame类:

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameFrame extends JFrame {
    public GameFrame() {
        JFrame gameFrame = new JFrame("Games");
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.setResizable(false);
        gameFrame.getContentPane().setLayout(null);
        gameFrame.setSize(905, 700);
        gameFrame.setVisible(true);
    }

    public void addPanel(JPanel menuPanel) {
        this.add(menuPanel);
    }
}

MainPanel-class: MainPanel类:

import java.awt.Color;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
    public MainPanel() {
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.setBounds(0, 0, 905, 700);
        mainPanel.setBackground(new Color(243, 207, 252));
        mainPanel.setVisible(true);
    }
}

You intend to use a GameFrame object as your JFrame , but GameFrame creates a brand new other frame for no reason. 您打算将GameFrame对象用作JFrame ,但是GameFrame创建了一个全新的其他框架。 So just avoid this and use only your GameFrame : 因此,避免这种情况,只使用您的GameFrame

public GameFrame() {
    super("Games");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    getContentPane().setLayout(null);
    setSize(905, 700);
    setVisible(true);
}

In the same way , you want to use a MainPanel object as a container, but it creates a new JPanel which will be discarded without being used at all, so as above : 同样,您希望将MainPanel对象用作容器,但是它将创建一个新的JPanel ,该JPanel将被丢弃而根本不使用,如下所示:

public MainPanel() {
    setLayout(null);
    setBounds(0, 0, 905, 700);
    setBackground(new Color(243, 207, 252));
    setVisible(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在用Java中的“ 0”替换Evens,我在做什么错? - I am trying to replaceEvens with a “0” in java, what am I doing wrong? 我试图用Java做一个GUI。 它没有出现 - I tried to make a gui in java. It didnt show up 我在做什么错,全部显示在一行上? - What am I doing wrong, it all show up on one line? 试图在Java中测试矩形的交集,我做错了什么? - Trying to test rectangles for intersection in Java, what am I doing wrong? 试图在 Java 中使用数组,但不知道我做错了什么 - trying to work with arrays in java but don't know what i am doing wrong Java:Paint应用程序,代码不起作用,我在做什么错? - Java: Paint application, the code doesn't work, what am I doing wrong? 官方 Java 练习解决方案不起作用……我做错了什么? - Official Java excercize solution doesn't work… what I am doing wrong? 试图在计时器上升时降低耐力 go。 我希望它每 10 秒 go 下降 2 - Trying to make stamina go down as timer goes up. I want it to go down by 2 every 10 seconds 为什么我的 GUI 不显示? 我正在尝试制作井字游戏,但我的 GUI 没有出现 - Why is my GUI not showing up? I am trying to make a tic tac toe board but my GUI is not showing up Android R文件不会显示。 我将图片导入格式错误的drawable中,这弄乱了我的每个项目 - Android R file wont show up. I imported pictures into drawable with the wrong format that messed up every single one of my projects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM