[英]JPanel not showing up on JFrame
我试图在JPanel上绘制一些填充的矩形并将该面板添加到JFrame,但是当我运行程序时,我看到的只是JFrame。 我使用frame.add(new RectanglePanel());
添加了JPanel frame.add(new RectanglePanel());
所以我不确定为什么面板没有显示。
车架类:
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();
}
}
小组课:
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) {
应该
@Override
protected void paintComponent(Graphics g) {
为什么不只使用Netbeans随附的拖放编辑器而不是硬代码?
我发现它更加有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.