下面是部分的程序:
public class Mywork1 extends JFrame{
/**
* @param args
*/
//重写 piant
Image image1=null;
public void paint (Graphics g){
super.paint(g);
g.drawImage(image1,10,50,600,500,this);
}
public EnjoyMusic(){
//设置窗体属性
setSize(600,590);
setLocation(330,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
//this.setResizable(false);
//建立切换按钮 ,可是运行之后显示不出来,会不会和paint()有关??
JButton button1=new JButton("切换");
//button1.setSize(50,100);
this.add(button1);
button1.setLocation(250, 500);
// 建立菜单栏
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);//为窗体设置menubar
那要怎么办?难道要对button 来个setvisible?怎么我看书上的都不用啊都会显示的 。会不会和加的那个菜单栏有关?
追答public class Test extends JFrame{
private ImageIcon icon = new ImageIcon(getClass().getResource("image.png"));
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(icon.getImage(), 100, 100, this);
}
public void Fun() {
setSize(500, 400);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("button");
add(button);
setVisible(true);
}
public static void main(String[] args){
new Test().Fun();
}
}
把你的图片替换上,你看看吧
你的程序用的是icon。我原来的也可以啊。
你的程序怎么连frame都出不来。= =还是感谢了。
1. 程序跟icon无关,最后还是用的icon.getImage()
2. 我这边运行正常,附图
没用诶。 你这只是调换了个顺序啊,你这样改是绝对错的,连图片都paint不出来了。
追答o . 忘了,
你去掉那个drawImage 试试
大哥,去掉了也没有见着啊
追答不如你把代码贴全了看看
追问太长了,贴不完。
本回答被网友采纳这么说,和这个有关?但是你看看我的写的坐标什么的,根本就不会把那个给覆盖。窗口最大化了也没有button的影子。而且感觉 g.drawImage(image1,10,50,600,500,this);的绘制很奇怪 好像会自己调整尺寸 而不是完全按照你写的坐标来画
追答调试下,看看事件先后顺序。。。