简体   繁体   English

JPanel未显示在JFrame上

[英]JPanel not showing up on JFrame

I'm trying to draw some filled rectangles on a JPanel and add that panel to a JFrame but when I run the program all I see is the JFrame. 我试图在JPanel上绘制一些填充的矩形并将该面板添加到JFrame,但是当我运行程序时,我看到的只是JFrame。 I added the JPanel using frame.add(new RectanglePanel()); 我使用frame.add(new RectanglePanel());添加了JPanel frame.add(new RectanglePanel()); so I'm not sure why the panel is not showing up. 所以我不确定为什么面板没有显示。

Frame class: 车架类:

package h02;

import javax.swing.*;

// frame and its properties

public class RectangleFrame extends JFrame {

public RectangleFrame() {

    // JFrame object

    JFrame frame = new JFrame();

    // properties

    frame.setSize(300, 200);
    frame.setLocationRelativeTo(null); // frame in centre
    frame.setTitle("Drawing rectangles");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new RectanglePanel());
    frame.setVisible(true);

}


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

    }

}

Panel class: 小组课:

package h02;

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

public class RectanglePanel extends JPanel {

// drawing the rectangles

public void PaintComponent(Graphics g) {
    super.paintComponent(g);

    g.fillRect(110, 110, 20, 20);
    g.fillRect(10, 10, 30, 120);
    g.fillRect(60, 10, 60, 100);
    g.fillRect(150, 10, 10, 20);
    g.fillRect(240, 10, 10, 20);
    g.fillRect(190, 30, 80, 30);
    g.fillRect(210, 50, 60, 20);
    g.fillRect(190, 70, 80, 50);
    g.fillRect(300, 30, 50, 90);
    g.fillRect(330, 10, 30, 20);
    }

}
public void PaintComponent(Graphics g) {

should be 应该

@Override
protected void paintComponent(Graphics g) {

Why not just use a drag-and-drop editor such as the ones provided with Netbeans rather than hard code? 为什么不只使用Netbeans随附的拖放编辑器而不是硬代码?

I find it to be much more efficient and effective. 我发现它更加有效。

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

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