简体   繁体   English

扩展JFrame时,JPanel无法正确显示

[英]JPanel does not appear correctly while extending JFrame

This is my homework. 这是我的作业。

My directions: Class GuessGame: This class contains the main function which creates a JFrame object, sets up the JFrame's size, visibility and exit. 我的指导:类GuessGame:该类包含创建JFrame对象,设置JFrame的大小,可见性和退出的主要函数。

Class GuessGameFrame: GuessGameFrame class extends JFrame. GuessGameFrame类:GuessGameFrame类扩展了JFrame。 So the GuessGameFrame class has all variables and methods defined in class JFrame. 因此GuessGameFrame类具有在JFrame类中定义的所有变量和方法。

I am not 100% sure what my problem is but I believe the JPanel is not working correctly 我不是100%知道我的问题是什么,但我认为JPanel无法正常工作

Any help would greatly be appreciated, if I left out anything important please let me know and I can add more detail. 任何帮助将不胜感激,如果我有任何重要的遗漏,请告诉我,我可以添加更多细节。

main class: 主类:

public class GuessGame {

             public static void main(String[] args){
                  GuessGameFrame localGuessGameFrame = new GuessGameFrame();
                  localGuessGameFrame.setVisible(true); //set visible
                  localGuessGameFrame.setSize(380,175); // set size 380 width 175 height
                  localGuessGameFrame.setDefaultCloseOperation(localGuessGameFrame.EXIT_ON_CLOSE); // needed to enable the red X button to close the program
             }
}

extended class: 扩展类:

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

public class GuessGameFrame extends JFrame
    {

    private int lastDistance; // distance between last guess and number
    private Color background; // background color of application

    private JFrame f;
    static JPanel p;
    private JButton b1;
    private JLabel l1;
    private JLabel l2;
    JTextField t1 = new JTextField(8);
    private JLabel l3;

    //declare constructor
    public GuessGameFrame()
    {
    //create our JFrame
    f = new JFrame("Guessing game");

    //create our panel
        p = new JPanel();
        p.setBackground(Color.MAGENTA); // background color is magenta

        //create our labels
        l1 = new JLabel("I have a number between 1 and 1000.");
        l2 = new JLabel("Can you guess my number? Enter your first Guess:.");
        l3 = new JLabel("Guess result appears here.");
        //create our button
        b1 = new JButton("New Game");

        //add button and label to panel
        p.add(l1); // Adds label 1
        p.add(l2); // adds label 2
        p.add(t1); // adds textfield 1
        p.add(l3); // adds label 3
        p.add(b1); // adds button 1

        //add panel to JFrame
        f.add(p);
    }
}

Screenshot of the output window when executed: 执行时输出窗口的屏幕截图: 在此处输入图片说明

I expect it to look like this: 我希望它看起来像这样:

在此处输入图片说明

You need remove JFrame f in GuessGameFrame class because GuessGameFrame is already a JFrame : 您需要在GuessGameFrame类中删除JFrame f ,因为GuessGameFrame已经是JFrame

And update your code: 并更新您的代码:

f = new JFrame("Guessing game"); to this.setTitle("Guessing game"); this.setTitle("Guessing game");

f.add(p); to this.getContentPane().add(p); this.getContentPane().add(p);

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

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