[英]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.