本文主要是介绍Java学习之在JPanel上绘制图形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
案例代码:
import javax.swing.*;
import java.awt.*;
public class DrawOnJPanel extends JPanel{public static void main(String[] args) {JFrame f=new JFrame("绘制图形");DrawOnJPanel d=new DrawOnJPanel();f.getContentPane().add(d);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setSize(300,200);f.setVisible(true);}protected void paintComponent(Graphics g) {super.paintComponent(g);g.setColor(Color.red);g.drawString("字符串",20,50);g.drawLine( 120,10,200,50);g.drawRect(60,80,getWidth()/2-30,getHeight()/2-30);g.drawOval(60,80,getWidth()/2-30,getHeight()/2-30); }
}
案例运行图:
这篇关于Java学习之在JPanel上绘制图形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!