简体   繁体   English

JPanel 未在 Java 中的 JFrame 内显示

[英]JPanel not showing inside JFrame in Java

I know this question has been asked a lot and I have done my research but still can not find anything.我知道这个问题已经被问了很多,我已经完成了我的研究,但仍然找不到任何东西。 Below is proof of this before anyone gets upset:以下是在任何人不高兴之前的证明:

I found this link: https://coderanch.com/t/563764/java/Blank-Frame-Panel我找到了这个链接: https://coderanch.com/t/563764/java/Blank-Frame-Panel

and this: Adding panels to frame, but not showing when app is run这: 将面板添加到框架,但不显示应用程序何时运行

and this: Why shouldn't I call setVisible(true) before adding components?还有这个: 为什么我不应该在添加组件之前调用 setVisible(true) ?

and this: JPanel not showing in JFrame?这: JPanel 没有显示在 JFrame 中?

But the first question says use repaint which I tried with no fix and the second and third to last questions talk about putting setVisible after components added which I have.但是第一个问题说使用重绘,我尝试过没有修复,倒数第二个和倒数第三个问题谈论在我添加的组件之后放置setVisible

The last one talks about making the users JPanelArt a extension (done so) of JPanel instead of JFrame and not to make a frame in a frame constructor (also have not done)最后一个谈到让用户JPanelArt成为JPanel的扩展(这样做)而不是JFrame并且不在框架构造函数中制作框架(也没有完成)

When ever I run this I just get a blank frame, as if the panel was never added in.每当我运行它时,我都会得到一个空白框架,就好像从未添加过面板一样。

I apologise if I have missed something in those links.如果我错过了这些链接中的某些内容,我深表歉意。 Anyway below is my classes:无论如何,下面是我的课程:

GUI.java (extends JFrame) GUI.java(扩展 JFrame)

package javaapplication2;

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

public class GUI extends JFrame{

public GUI(String name) {
    super(name);

    getContentPane().setLayout(null);

    JPanel myPanel1 = new GUIPanel();
    myPanel1.setLocation(20, 20);
    getContentPane().add(myPanel1);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setResizable(true);
}

public static void main(String[] args) {
    JFrame frame = new GUI("Game");
    frame.setVisible(true);
}
}

GUIPanel.java (extends JPanel) GUIPanel.java(扩展 JPanel)

package javaapplication2;

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


public class GUIPanel extends JPanel {

JButton start;
JButton inspect1;
JButton inspect2;
JButton inspect3;
JButton suspect;

public GUIPanel() {
    setLayout(new BorderLayout());

    start = new JButton("Start Game");
    inspect1 = new JButton("Inspect 1");
    inspect2 = new JButton("Inspect 2");
    inspect3 = new JButton("Inspect 3");
    suspect = new JButton("Choose Suspect");

    add(start, BorderLayout.WEST);
    add(inspect1, BorderLayout.WEST);
    add(inspect2, BorderLayout.WEST);
    add(inspect3, BorderLayout.WEST);
    add(suspect, BorderLayout.WEST);

}
}

I know it is very simple, but that is because I am following a tutorial from my lecturer to get the hang of things as I previously used a GUI builder which someone in this community pointed out to me is not good to start with (very correct!)我知道这很简单,但那是因为我正在按照我的讲师的教程来掌握事情的窍门,因为我之前使用了一个 GUI 构建器,这个社区中的某个人向我指出这不是一个好的开始(非常正确!)

The issue lies in your GUI class when you call getContentPane().setLayout(null) .当您调用getContentPane().setLayout(null)时,问题出在您的GUI class 中。 Because of this method call, your JFrame is not displaying anything.由于此方法调用,您的 JFrame 没有显示任何内容。 If you remove it, your elements should show up.如果你删除它,你的元素应该会出现。

I also noticed that you were setting each JButton to have a constraint of BorderLayout.WEST .我还注意到您将每个 JButton 设置为具有BorderLayout.WEST约束。 This will most likely put your JButtons on top of each other and render only one of them.这很可能会将您的 JButton 叠放在一起,并且只渲染其中一个。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM