背景设置分布在两个地方,效果不一样:
public Cont()
{
background = new ImageIcon("g.jpg");
l1 = new JLabel(background);
p2.add(l1, new Integer(Integer.MIN_VALUE));
background.setImage(background.getImage().getScaledInstance(x,y,Image.SCALE_DEFAULT));
Contframe.validate();//刷新
}public void actionPerformed(ActionEvent f) //监控按钮操作
{ if (f.getActionCommand()=="森林界面")
{ p2.removeAll();
background = new ImageIcon("s.jpg");
l1 = new JLabel(background);
p2.add(l1, new Integer(Integer.MIN_VALUE));
background.setImage(background.getImage().getScaledInstance(x,y,Image.SCALE_DEFAULT));
Contframe.validate();//刷新
}
几乎相同的代码,在不同的地方的功能却不一样。好像在Cont()里就没法让图片适应p2的大小。怎么办呢?
public Cont()
{
background = new ImageIcon("g.jpg");
l1 = new JLabel(background);
p2.add(l1, new Integer(Integer.MIN_VALUE));
background.setImage(background.getImage().getScaledInstance(x,y,Image.SCALE_DEFAULT));
Contframe.validate();//刷新
}public void actionPerformed(ActionEvent f) //监控按钮操作
{ if (f.getActionCommand()=="森林界面")
{ p2.removeAll();
background = new ImageIcon("s.jpg");
l1 = new JLabel(background);
p2.add(l1, new Integer(Integer.MIN_VALUE));
background.setImage(background.getImage().getScaledInstance(x,y,Image.SCALE_DEFAULT));
Contframe.validate();//刷新
}
几乎相同的代码,在不同的地方的功能却不一样。好像在Cont()里就没法让图片适应p2的大小。怎么办呢?
控件还没展现出来,size还不确定,图片自然无法缩放到你期望的大小而你在按钮中触发的时候:
所有的控件已经初始化完毕,已经展现了出来,size已经确定,你自然可以将图片缩放到控件的大小
控件还没展现出来,size还不确定,图片自然无法缩放到你期望的大小
第二:这个x,y是什么?是通过什么方法获得?