我写了一个panel,用于显示图片,代码如下:
..
public PicPanel(String picPath) {
try {
jbInit(picPath);
} catch (Exception e) {
e.printStackTrace();
}
} JLabel jl = null;
ImageIcon img = null; private void jbInit(String picPath) throws Exception {
this.setLayout(null);
setVisible(true);
img = new ImageIcon(picPath);
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.setSize(new Dimension(400, 359));
this.add(jl, BorderLayout.CENTER); }
..
我在Frame中调用panel并把图片地址以参数发给panel:
PicPanel pp=new PicPanel("c:\\t.jpg");
panelCenter.add(pp,BorderLayout.NORTH);
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
可以正常显示图片,
但我希望在FRAME中加入一个按钮方法,点击该按钮来切换panel中的图片,我采用的是如下方法:
private void button1_actionPerformed(ActionEvent e) {
pp=new PicPanel("c:\\t2.jpg");
panelCenter.add(pp,BorderLayout.WEST);
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
}
不起作用。
请高手帮个忙,怎么样解决这个问题
..
public PicPanel(String picPath) {
try {
jbInit(picPath);
} catch (Exception e) {
e.printStackTrace();
}
} JLabel jl = null;
ImageIcon img = null; private void jbInit(String picPath) throws Exception {
this.setLayout(null);
setVisible(true);
img = new ImageIcon(picPath);
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.setSize(new Dimension(400, 359));
this.add(jl, BorderLayout.CENTER); }
..
我在Frame中调用panel并把图片地址以参数发给panel:
PicPanel pp=new PicPanel("c:\\t.jpg");
panelCenter.add(pp,BorderLayout.NORTH);
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
可以正常显示图片,
但我希望在FRAME中加入一个按钮方法,点击该按钮来切换panel中的图片,我采用的是如下方法:
private void button1_actionPerformed(ActionEvent e) {
pp=new PicPanel("c:\\t2.jpg");
panelCenter.add(pp,BorderLayout.WEST);
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
}
不起作用。
请高手帮个忙,怎么样解决这个问题
你加validate方法看行不,它可以重新布置组件
panelCenter.add(pp,BorderLayout.WEST); pp应该放同一位置
panelCenter.updateUI();
private void button1_actionPerformed(ActionEvent e) {
pp=new PicPanel("c:\\t2.jpg");
panelCenter.validate();
panelCenter.add(pp,BorderLayout.NORTH);
panelCenter.updateUI();
//this.getContentPane().add( panelCenter, BorderLayout.CENTER );
}
2. repaint();
private void button1_actionPerformed(ActionEvent e) {
pp=new PicPanel("c:\\t2.jpg");
panelCenter.validate();
panelCenter.add(pp,BorderLayout.NORTH);
panelCenter.updateUI();
panelCenter.repaint();
//this.getContentPane().add( panelCenter, BorderLayout.CENTER );
}
不行的
请再帮忙
不管怎么样,还是请SWING老大轻抬贵手,给解决解决,或者您有例子什么的
给我一个