public class Kyodai extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
/**
 * @param args
 */
ImageIcon image;
JMenuBar mb;
JMenu menu;
JMenuItem junior,intermediate,senior,rank;
Container con;

Kyodai(){
con=this.getContentPane();
image=new ImageIcon("F:\\workbench\\lianliankan\\img\\z0.jpg");
JLabel imagelabel=new JLabel(image);
mb=new JMenuBar();
menu=new JMenu("游戏");
junior=new JMenuItem("初级");
intermediate=new JMenuItem("中级");
senior=new JMenuItem("高级");
rank=new JMenuItem("排行榜");
this.setJMenuBar(mb); mb.add(menu);
menu.add(junior);
menu.add(intermediate);
menu.add(senior);
menu.add(rank);
con.add(imagelabel); this.setSize(700,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); junior.addActionListener(this);
intermediate.addActionListener(this);
senior.addActionListener(this);
rank.addActionListener(this);
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource()==junior)
juniorGame();
else if(ae.getSource()==intermediate)
intermediateGame();
else if(ae.getSource()==senior)
seniorGame();
else
rankGame();

}

void juniorGame(){
con=this.getContentPane();
JPanel toppanel=new JPanel();
JLabel label=new JLabel("难度1");
JLabel gradelabel=new JLabel("得分:");
JLabel grade=new JLabel();
JPanel cenpanel=new JPanel();
ImageIcon image=new ImageIcon("F:\\workbench\\lianliankan\\img\\303.jpg");
JLabel imagelabel=new JLabel(image); toppanel.add(label);
toppanel.add(gradelabel);
toppanel.add(grade);
cenpanel.add(imagelabel);
con.add(toppanel,BorderLayout.NORTH);
con.add(cenpanel,BorderLayout.CENTER);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Kyodai();
}
}
为什么我点击“初级”后并不会显示新的图片?

解决方案 »

  1.   

    已经完整了,就是我想在juniorGame()里更改图片,但不行,还是显示以前的那张图片?
      

  2.   

    你在juniorGame()最后加句validate();试试
      

  3.   

    试了black_night所说的方法,但还是不行。
      

  4.   

    回copine,在juniorGame()中只用repaint()不行。不知你说的repaint是怎么用的?
      

  5.   

    wunan320提示用卡片布局,这是一种思路,但就不能用我所说的布局吗?