问题描述
importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjavax.swing.JFrame;publicclasspage151{publicstaticvoidmain(String[]agrs){JFramewin;ContainercontentPane;Graphicsg;win=newJFrame("myfirstrectangle");win.setSize(600,500);win.setLocation(100,100);win.setVisible(true);contentPane=win.getContentPane();contentPane.setBackground(Color.green);g=contentPane.getGraphics();g.drawString("china",50,100);g.setColor(Color.BLUE);g.drawRect(50,50,100,50);g.setColor(Color.red);g.fillRect(175,50,100,30);g.drawOval(60,60,100,100);g.drawLine(20,30,50,60);}}这个程序绘制出来的东西有时显示不了,窗口一点最大化和缩放就消失,该怎么解决?才能显示一个稳定并且能缩放窗口?请各位高手指点,谢谢
解决方案
解决方案二:
当你改变窗口大小时,会重新调用JFrame的paint()方法,你放paint()里面试试
解决方案三:
能不能具体点?貌似我都没有用JFrame
解决方案四:
需要重写jframe的paint方法窗口大小改变的时候会自动调用这个方法
解决方案五:
publicclasspage151intendsJFrame{publicpage151(){super("myfirstrectangle");setSize(600,500);setLocation(100,100);setBackground(Color.green);setVisible(true);}publicvoidpaint(Graphicsg){g.drawString("china",50,100);g.setColor(Color.BLUE);g.drawRect(50,50,100,50);g.setColor(Color.red);g.fillRect(175,50,100,30);g.drawOval(60,60,100,100);g.drawLine(20,30,50,60);}publicstaticvoidmain(String[]agrs){page151pag=newpage151()}}记事本下改了下,可能有错误大概就是这样
解决方案六:
importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjavax.swing.JFrame;publicclasspage151{publicstaticvoidmain(String[]agrs){JFramewin;ContainercontentPane;win=newJFrame("myfirstrectangle"){publicvoidpaint(Graphicsg){g.drawString("china",50,100);g.setColor(Color.BLUE);g.drawRect(50,50,100,50);g.setColor(Color.red);g.fillRect(175,50,100,30);g.drawOval(60,60,100,100);g.drawLine(20,30,50,60);}};win.setSize(600,500);win.setLocation(100,100);win.setVisible(true);contentPane=win.getContentPane();contentPane.setBackground(Color.green);}}