thank you very much! 我是先把程序实现了,然后又把界面作了,单独运行都没问题,合一块就出上面的问题 我确实重写了paint()方法,如下: public class aa { Image image; void DrawImage(JApplet app,Graphics g) { image=app.getImage(app.getCodeBase(),"res/aa.jpg"); g.drawImage(image,m_pos.x,m_pos.y,app); } //m_pos是image的当前位置} aa pCom; public void paint(Graphics g) { paintComponents(g); pCom.DrawImage(this,g); } 如果不写paintComponents(g);菜单显示不出来 如果不重写paint(),我定义的类就没法画图.
public class Lab extends JApplet
{
public void init() {
setJMenuBar(mb);
JToolBar toolBar1 = new JToolBar();
JToolBar toolBar2=new JToolBar();
Panel tbPanel=new Panel();
tbPanel.setLayout(new GridLayout(2,1));
tbPanel.add(toolBar1);
tbPanel.add(toolBar2);
getContentPane().add(tbPanel,BorderLayout.NORTH );}}
gRootPane.setMenuBar(mb);
this.getContentPane().setLayout(new BorderLayout());
jMenu2.setText("gfhf");
jMenuItem1.setText("sh");
jMenu3.setText("fghh");
jCheckBox1.setText("jCheckBox1");
jPasswordField1.setText("jPasswordField1");
this.setJMenuBar(jMenuBar1);
jCheckBox2.setText("jCheckBox2");
jPasswordField2.setText("jPasswordField2");
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu2.add(jMenuItem1);
JPanel tbPanel=new JPanel();
tbPanel.setLayout(new GridLayout(2,1));
tbPanel.add(jToolBar1);
tbPanel.add(jToolBar2);
jToolBar2.add(jCheckBox2, null);
jToolBar2.add(jPasswordField2, null);
this.getContentPane().add(tbPanel, BorderLayout.NORTH);
jToolBar1.add(jPasswordField1, null);
jToolBar1.add(jCheckBox1, null);
我是先把程序实现了,然后又把界面作了,单独运行都没问题,合一块就出上面的问题
我确实重写了paint()方法,如下:
public class aa {
Image image;
void DrawImage(JApplet app,Graphics g)
{
image=app.getImage(app.getCodeBase(),"res/aa.jpg");
g.drawImage(image,m_pos.x,m_pos.y,app); } //m_pos是image的当前位置}
aa pCom;
public void paint(Graphics g)
{
paintComponents(g);
pCom.DrawImage(this,g);
}
如果不写paintComponents(g);菜单显示不出来
如果不重写paint(),我定义的类就没法画图.
public class BackMapPanel extends JPanel {
Image image = null;
BorderLayout borderLayout1 = new BorderLayout(); public BackMapPanel(Image image) {
super();
this.image = image;
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
//SwingUtilities.updateComponentTreeUI(this);
this.setLayout(borderLayout1);
} public void paintComponent(Graphics g)
{
//System.out.println(image.toString());
if(this.image!=null)
g.drawImage(image,0,0,image.getWidth(this),image.getHeight(this),null,this);
else
g.drawString("找不到图片",50,50);
// this.updateUI();
g.dispose();
}
}