简体   繁体   English

Java GUI JPanel不起作用

[英]Java GUI JPanel Not Working

I really can't figure out why this JPanel "p" isn't appearing? 我真的不知道为什么这个JPanel“ p”没有出现? I thought I coded it right for the JPanel p to be in the middle of the Jframe and should make the whole JFrame RED but it doesn't seem to do that and the buttons and JPanel aren't appearing. 我以为我对它进行了正确的编码,以使JPanel p位于Jframe的中间,并且应该使整个JFrame变成红色,但似乎没有这样做,并且按钮和JPanel也没有出现。 Sorry. 抱歉。 I know I am probably stupid but please help. 我知道我可能很蠢,但是请帮忙。 :? :? Here is the code. 这是代码。

package com.gorillalogic.henry;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Notepad {

    private JFrame f; // creates all GUI components
    private JPanel p;
    private JButton b1;

    public Notepad() {

        gui();
    }

    public void gui() {

        f = new JFrame("Notepad");
        p = new JPanel();

        b1 = new JButton("Quit");
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        f.setSize(600, 400);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        p.setBackground(Color.RED);
        p.add(b1);

        f.add(p, BorderLayout.CENTER);

    }

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

    }

}

Thanks in advance. 提前致谢。 :) :)

p.setOpaque(true);

您需要这样做。

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

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