关于java GUI 我用paint方法绘制一些几何图形,但是显示不出来
packagech9;importjava.awt.*;importjavax.swing.*;publicclassEx9_2extendsJFrame{publicE...
package ch9;
import java.awt.*;
import javax.swing.*;
public class Ex9_2 extends JFrame
{
public Ex9_2()
{
super("演示字体、颜色、绘画");
setSize(480,250);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);//call superclass's paint method
g.setFont(new Font("SansSerif",Font.BOLD,12));//设置字体
g.setColor(Color.blue); //设置颜色
g.drawString("字体 ScanSerif,粗体,12号,蓝色",20,50);//绘制字符串
g.setFont(new Font("Serif",Font.ITALIC,14));
g.setColor(new Color(255,0,0));
g.drawString("字体 Serif,斜体,14号,红色",250,50);
g.drawLine(20,60,460,60);
g.setColor(Color.green);
g.drawRect(20,70,100,50);
g.fillRect(130,70, 100, 50);
g.setColor(Color.yellow);
g.drawRoundRect(240, 70, 100, 50, 50, 50);//绘制空心圆角矩形
g.fillRoundRect(350, 70, 100, 50, 50, 50);//绘制实心圆角矩形
g.setColor(Color.cyan);
g.draw3DRect(20, 130, 100, 50, true);//绘制有三维凸起效果的空心矩形
g.fill3DRect(130, 130, 100, 50, false);//绘制有三维凸起效果的实心矩形
g.setColor(Color.pink);
g.drawOval(240,130,100,50);//绘制空心椭圆
g.fillOval(350, 130, 100, 50);//绘制实心椭圆
g.setColor(new Color(0,120,20));
g.drawArc(20, 190, 100, 50, 0, 90);//绘制一段圆弧
g.fillArc(130, 190, 100, 50, 0, 90);//绘制扇形,扇形由圆弧及两半径圈定
g.setColor(Color.black);
int xValues[]={250,280,290,300,330,310,320,290,260,270};
int yValues[]={210,210,190,210,210,220,230,220,230,220};
g.drawPolygon(xValues,yValues,10);//绘制空心多边形
int xValues2[]={360,290,400,410,440,420,430,400,370,380};
g.fillPolygon(xValues2,yValues,10);//绘制实心多边形
}//end method paint
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);//设置窗口的外观感觉为Java默认
Ex9_2 application=new Ex9_2();
//Graphics g=new Graphics();
//application.paint(g);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我找到答案了,JFrame.setDefaultLookAndFeelDecorated(true);把这个去了就对了,我现在也不知道为什么,呵呵,还是谢谢你的回答。 展开
import java.awt.*;
import javax.swing.*;
public class Ex9_2 extends JFrame
{
public Ex9_2()
{
super("演示字体、颜色、绘画");
setSize(480,250);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);//call superclass's paint method
g.setFont(new Font("SansSerif",Font.BOLD,12));//设置字体
g.setColor(Color.blue); //设置颜色
g.drawString("字体 ScanSerif,粗体,12号,蓝色",20,50);//绘制字符串
g.setFont(new Font("Serif",Font.ITALIC,14));
g.setColor(new Color(255,0,0));
g.drawString("字体 Serif,斜体,14号,红色",250,50);
g.drawLine(20,60,460,60);
g.setColor(Color.green);
g.drawRect(20,70,100,50);
g.fillRect(130,70, 100, 50);
g.setColor(Color.yellow);
g.drawRoundRect(240, 70, 100, 50, 50, 50);//绘制空心圆角矩形
g.fillRoundRect(350, 70, 100, 50, 50, 50);//绘制实心圆角矩形
g.setColor(Color.cyan);
g.draw3DRect(20, 130, 100, 50, true);//绘制有三维凸起效果的空心矩形
g.fill3DRect(130, 130, 100, 50, false);//绘制有三维凸起效果的实心矩形
g.setColor(Color.pink);
g.drawOval(240,130,100,50);//绘制空心椭圆
g.fillOval(350, 130, 100, 50);//绘制实心椭圆
g.setColor(new Color(0,120,20));
g.drawArc(20, 190, 100, 50, 0, 90);//绘制一段圆弧
g.fillArc(130, 190, 100, 50, 0, 90);//绘制扇形,扇形由圆弧及两半径圈定
g.setColor(Color.black);
int xValues[]={250,280,290,300,330,310,320,290,260,270};
int yValues[]={210,210,190,210,210,220,230,220,230,220};
g.drawPolygon(xValues,yValues,10);//绘制空心多边形
int xValues2[]={360,290,400,410,440,420,430,400,370,380};
g.fillPolygon(xValues2,yValues,10);//绘制实心多边形
}//end method paint
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);//设置窗口的外观感觉为Java默认
Ex9_2 application=new Ex9_2();
//Graphics g=new Graphics();
//application.paint(g);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我找到答案了,JFrame.setDefaultLookAndFeelDecorated(true);把这个去了就对了,我现在也不知道为什么,呵呵,还是谢谢你的回答。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询