//给你看看我写的吧,点击按钮显示图片
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class TestDisplayPic extends javax.swing.JFrame {
private JButton jButton1;
private JLabel jLabel1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestDisplayPic inst = new TestDisplayPic();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TestDisplayPic() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("点击显示图片");
jButton1.setBounds(49, 192, 291, 22);
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
}
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setBounds(17, 12, 355, 168);
}
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jButton1MouseClicked(MouseEvent evt) {
ImageIcon image=new ImageIcon("img/6.png");
jLabel1.setIcon(image);
}
}
需要说明的是图片存放的位置,我这个是在工程名称上选择新建文件夹,再放一张图片进去
首先,你的代码简直让人不敢直视,无格式、无规范!其次,你对swing非常的不了解!废话不多说,还是解决问题:
//LayeredPane是层面板,add()方法中的第二个参数是Z轴,你设为Integer的最小值是不正确的
//this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
this.getLayeredPane().add(background, 1);
木有看明白