当前位置: 技术问答>java相关
菜鸟问题,关于paint方法的内部机制:
来源: 互联网 发布时间:2015-05-23
本文导语: 在以下代码中,我去掉注释,JButton就显示不出来,难道paint不能把加入的控件画出来,还是什么别的问题?可是我必须用Graphics在JPanel上画(重画,响应鼠标拖动)东西,请问该怎么解决?谢谢! import java.awt.*; import...
在以下代码中,我去掉注释,JButton就显示不出来,难道paint不能把加入的控件画出来,还是什么别的问题?可是我必须用Graphics在JPanel上画(重画,响应鼠标拖动)东西,请问该怎么解决?谢谢!
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class LinkTest extends JApplet {
JP jpObj=new JP();
public void init()
{
getContentPane().add(jpObj);
}
class JP extends JPanel
{
JButton jb=new JButton();
JP()
{
setBackground(Color.black);
setSize(new Dimension(500,600));
add(jb);
jb.setLocation(40,60);
}
/*
public void (Graphics g)
{
g.drawString("have to draw sth",20,60);
}*/
}
}
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class LinkTest extends JApplet {
JP jpObj=new JP();
public void init()
{
getContentPane().add(jpObj);
}
class JP extends JPanel
{
JButton jb=new JButton();
JP()
{
setBackground(Color.black);
setSize(new Dimension(500,600));
add(jb);
jb.setLocation(40,60);
}
/*
public void (Graphics g)
{
g.drawString("have to draw sth",20,60);
}*/
}
}
|
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class LinkTest extends JApplet {
JP jpObj=new JP();
public void init()
{
getContentPane().add(jpObj);
}
class JP extends JPanel
{
JButton jb=new JButton();
JP()
{
setBackground(Color.black);
setSize(new Dimension(500,600));
add(jb);
jb.setLocation(40,60);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("have to draw sth",20,60);
}
}
}
import java.applet.*;
import javax.swing.*;
public class LinkTest extends JApplet {
JP jpObj=new JP();
public void init()
{
getContentPane().add(jpObj);
}
class JP extends JPanel
{
JButton jb=new JButton();
JP()
{
setBackground(Color.black);
setSize(new Dimension(500,600));
add(jb);
jb.setLocation(40,60);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("have to draw sth",20,60);
}
}
}
|
参看http://www.csdn.net/expert/topic/682/682782.xml?temp=.0885126