import javax.swing.*
import java.awt.image.*
import java.awt.event.*
public class JButtonImage extends JFrame {
JLabel lbl1, lbl2
public JButtonImage(){
super("JFrameSimpleImage Demo")
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setSize(400, 300)
this.setLayout(new GridLayout(2, 2) )
img1 = Toolkit.getDefaultToolkit().getImage("image1.jpg")
img2 = Toolkit.getDefaultToolkit().getImage("image2.jpg")
lbl1=new JLabel()
JButton btn1 = new JButton("Image-1")
lbl2=new JLabel()
JButton btn2 = new JButton("Image-2")
this.add(lbl1)
this.add(lbl2)
this.add(btn1)
this.add(btn2)
this.setVisible(true)
this.validate()
this.repaint()
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lbl1.setIcon(new ImageIcon(img1))
}
})
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lbl2.setIcon(new ImageIcon(img2))
}
})
}
Image img1, img2
public static void main(String args[]){
javax.swing.SwingUtilities.invokeLater(new Thread(){
public void run(){
new JButtonImage()
}
})
}
}
请参考
很简单:JLabel background = new JLabel(new ImageIcon(url))//url代表你图片的路径
this.add(background)即可//将你的jlabel背景加到jframe上
当然jframe也有专门的设置背景的复方法,忘掉了,你查一下
最好下一个JDK -API手册,一查便知
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)