java drawingpanel,即使我将Eclipse上的Java添加到JFrame中,也不会显示JPanel

These are the files. I have set JFrame to be visible, and have added JPanel to it, but still, the code only shows the window without anything in it.

import java.util.List;

import java.util.ArrayList;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.util.Collections;

public static void main(String[] args)

{

JFrame frame = new JFrame();

frame.setSize(350, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("My Empty Window");

frame.setVisible(true);

DrawingPanel panel = new DrawingPanel();

frame.add(panel);

frame.setVisible(true);

}

-------------DRAWINGPANEL FILE-------------------

import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawingPanel extends JPanel {

public void painting(Graphics pen) {

pen.drawRect(50, 50, 20, 20);

pen.drawRect(100, 50, 40, 20);

pen.drawOval(200,50,20,20);

pen.drawOval(250, 50, 40, 20);

pen.drawString("Square", 50, 90);

pen.drawString("Rectangle", 100, 90);

pen.drawString("Cirlce", 200, 90);

pen.drawString("Oval", 250, 90);

pen.fillRect(50, 100, 20, 20);

pen.fillRect(100, 100, 40, 20);

pen.fillOval(250, 100, 20, 20);

pen.fillOval(250, 100, 40, 20);

pen.drawLine(50, 150, 300, 150);

pen.drawArc(50, 150, 200, 100, 0, 180);

pen.fillArc(100, 175, 200, 75, 90, 45);

}

}

解决方案

Try changing the method in DrawingPanel from painting to paint, which will get called when run. paint is a method inherited from JPanel.

Edit: As mentioned by NomadMaker, use paintComponent() not paint() here. Read this for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值