按了按钮后,画面切换,总是要鼠标经过,这个图像才显示。。。
Class Game extends JPanel
{
//主类
public static launch_Satellite toppanel;
public void repaint_manual()
{
this.add(toppanel);
toppanel.setVisible(true);
// this.validate();
}
}
//别的类里的触发
public void actionPerformed(ActionEvent e)
{
Game.toppanel=new launch_Satellite(...);
Game.repaint_manual();
}
public Class launch_Satellite extends JPanel
{
this.setLayout(null);
cardbtn1=new Cardbtn(n);
cardbtn1.setBounds(x,y,width,height);
cardbtn1.setVisible(true);
this.add(cardbtn1);
this.setBounds(x,y,width,height);
this.setVisible(true);
Game.toppanel.add(this);
}
public Class Cardbtn extends JPanel
{
this.setLayout(null);
CardImg cardimg;
cardimg.setBounds(0, 0, width, height);
this.setSize(width, height);
this.add(cardimg);
cardimg.setVisible(true);
}
public Cardimg extends JButton
{
}
------解决方案--------------------
我之前也遇到过这个问题。。。。
------解决方案--------------------
加上repaint,不然界面不会更新,你鼠标经过时触发了repaint才会更新界面,另外加上validate是可以的,因为界面布局可能发生变化
public void repaint_manual() {
this.add(toppanel);
toppanel.setVisible(true);
this.validate();
this.repaint();
}