import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class WindowFrame extends JFrame
{
Image myIM;
public void setImage(Image im)
{
this.myIM=im;
System.out.println("SetP");
}
public WindowFrame()
{
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.drawImage(myIM, 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
JButton button = new JButton( "Hello" );
panel.add( button );
System.out.println("Panel");
//initGUI();
}
public static void initGUI()
{
WindowFrame frame = new WindowFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
System.out.println("GUI");
}
public static void main(String [] args)
{
initGUI();
System.out.println("Main");
}
}我在别的类先实体化:WindowFrame mywf=new WindowFrame()里面调用这个类的myfm.setImage(im)来传入一个图片.按说调用mywf.repaint();就应该画图的啊...可是就是不画,连框都不显示!!救命啊..后天就要交作业了...5555
import javax.swing.*;
import java.awt.*;
public class WindowFrame extends JFrame
{
Image myIM;
public void setImage(Image im)
{
this.myIM=im;
System.out.println("SetP");
}
public WindowFrame()
{
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.drawImage(myIM, 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque( false );
JButton button = new JButton( "Hello" );
panel.add( button );
System.out.println("Panel");
//initGUI();
}
public static void initGUI()
{
WindowFrame frame = new WindowFrame();
frame.setSize(300, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
System.out.println("GUI");
}
public static void main(String [] args)
{
initGUI();
System.out.println("Main");
}
}我在别的类先实体化:WindowFrame mywf=new WindowFrame()里面调用这个类的myfm.setImage(im)来传入一个图片.按说调用mywf.repaint();就应该画图的啊...可是就是不画,连框都不显示!!救命啊..后天就要交作业了...5555
add(panel);就好了
2.概念问题 你把initGUI搞成静态?WindowFrame构造函数里面,事例一个对象才执行一次.
实在是没法讲~~应该多看看书
3.JFRAME里面是装不了JFRAME的~~哎 LZ应该多看点书
===>帮你代码改好了 单执行或直接加在别的JFRAME里都行 快点结了 偶还茶50分 两星了 嘿嘿
package csdn;import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.*;public class WindowFrame extends JPanel {
Image myIM;
JPanel panel;
public void setImage(Image im) {
this.myIM = im;
System.out.println("SetP");
} public WindowFrame() {
}
public void initGUI() {
panel = new JPanel() {
protected void paintComponent(Graphics g) {
g.drawImage(myIM, 0, 0, null);
super.paintComponent(g);
}
};
panel.setOpaque(false);
JButton button = new JButton("Hello");
panel.add(button);
System.out.println("Panel");
this.add(panel);
//initGUI();
System.out.println("GUI");
} public static void main(String[] args) {
JFrame jframeComtainer=new JFrame();
WindowFrame frame = new WindowFrame();
Image image = null;
try {
image = ImageIO.read(new File("c:/text.png"));
} catch (IOException ex) {
System.out.println("ex=" + ex);
}
frame.setImage(image);
frame.initGUI();
jframeComtainer.add(frame);
jframeComtainer.setLocationRelativeTo(null);
jframeComtainer.setVisible(true);
jframeComtainer.setSize(300, 300);
// initGUI();
System.out.println("Main");
}
}