关于Label 上图片更换的问题~~~~~
请各位高手帮帮忙:我的界面是空布局,在界面是添加了一个Label, 在Label 上已经添加了一张图片,我想点击一下按钮就显示另外一张图片,请问怎么实现,我是JAVA新手,麻烦你帮帮我,非常感谢!!!
我写的代码如下:
final JLabel label = new JLabel(new ImageIcon("风景.jpg")); // 创建Label,并在Label上添加第一张图片
label.setBounds(27, 22, 200, 150);
getContentPane().add(label);
label.setVisible(true);
final JButton button = new JButton(); // 创建一个按钮,我想点击 按钮 就能显示另外一张图片
button .addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
*********** 我想在这里写显示另外一张图片的代码 ***********
}
});
button .setText("按钮");
button .setBounds(79, 180, 99, 23);
getContentPane().add( button );
搜索更多相关的解决方案:
Label
----------------解决方案--------------------------------------------------------
程序代码:
package com.freish;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LabelTest extends JFrame {
private Container container;
private JLabel label;
private JButton button;
private ImageIcon[] icon = new ImageIcon[]{new ImageIcon("image/1.jpg"),
new ImageIcon("image/2.jpg"),new ImageIcon("image/3.jpg"),
new ImageIcon("image/4.jpg"),new ImageIcon("image/5.jpg"),
new ImageIcon("image/6.jpg"),new ImageIcon("image/7.jpg")};
public LabelTest() {
super("LabelTest");
label = new JLabel(new ImageIcon("image/1.jpg")); // 创建Label,并在Label上添加第一张图片
//label.setBounds(27, 22, 200, 150);
container = getContentPane();
container.add(label);
button = new JButton("更改图片");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random rnd = new Random();
int num = rnd.nextInt(icon.length);
label.setIcon(icon[num]);
}
});
container.add(button,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Dimension frameSize = getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
setVisible(true);
}
public static void main(String[] args) {
new LabelTest();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LabelTest extends JFrame {
private Container container;
private JLabel label;
private JButton button;
private ImageIcon[] icon = new ImageIcon[]{new ImageIcon("image/1.jpg"),
new ImageIcon("image/2.jpg"),new ImageIcon("image/3.jpg"),
new ImageIcon("image/4.jpg"),new ImageIcon("image/5.jpg"),
new ImageIcon("image/6.jpg"),new ImageIcon("image/7.jpg")};
public LabelTest() {
super("LabelTest");
label = new JLabel(new ImageIcon("image/1.jpg")); // 创建Label,并在Label上添加第一张图片
//label.setBounds(27, 22, 200, 150);
container = getContentPane();
container.add(label);
button = new JButton("更改图片");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random rnd = new Random();
int num = rnd.nextInt(icon.length);
label.setIcon(icon[num]);
}
});
container.add(button,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Dimension frameSize = getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
setVisible(true);
}
public static void main(String[] args) {
new LabelTest();
}
}
本来想传一个MyEclipse的工程的附件,但是今天发的附件超过论坛限制了,发不了了
[[it] 本帖最后由 freish 于 2008-9-29 17:33 编辑 [/it]]
----------------解决方案--------------------------------------------------------
非常感谢freish给我的答案,问题我已经解决啦,谢谢~~~~~~~~~~~
----------------解决方案--------------------------------------------------------