java 窗口背景图片无法加载
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class Index extends JFrame
{
JButton Login=new JButton("Login");
JButton Enter=new JButton("Enter");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JLabel lb1=new JLabel("UserID");
JLabel lb2=new JLabel("PassWord");
private ImageIcon myimage = new ImageIcon("/Images/向日葵.PNG");
JLabel imgLabel = new JLabel(myimage);//将背景图放在标签里
Index(String s)
{
super(s);
setSize(400,330);
setLocation(450,250);
Container c;
c= getContentPane();
setLayout(null);
c.add(lb1);
c.add(lb2);
c.add(tf1);
c.add(tf2);
c.add(Enter);
c.add(Login);
lb1.setBounds(75,85,60,20);
lb2.setBounds(75,125,60,20);
tf1.setBounds(165,85,150,20);
tf2.setBounds(165,125,150,20);
Login.setBounds(100,185,65,20);
Enter.setBounds(230,185,65,20);
getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0,0,myimage.getIconWidth(), myimage.getIconHeight());
((JPanel)c).setOpaque(false);
setVisible(true);
}
public static void main(String [] args)
{
Index inf=new Index("Index");
}
}
编译没有问题,但显示的时候只能显示窗口的其他控件,背景图片不能显示,图片路径应该是正确的,不知道为什么。 展开
你好,你这种情况我曾经遇过。
不能显示背景图片的原因是它被其他组件挡住了,主要原因是布局的问题,你设置为空,JFrame就不会帮你布局,你要特别注意添加组件的顺序,我特意为你写了一个符合你要求的JFrame,跟你那个完全一样,只要你的图片跟我的图片一样就行了。
解决办法(参照如下代码):
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* MyIndex.java
*
* Created on 2011-5-8, 1:00:26
*/
package testjframebgcolor;
/**
*
* @author Study
*/
public class MyIndex extends javax.swing.JFrame {
/** Creates new form MyIndex */
public MyIndex() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lb1 = new javax.swing.JLabel();
Enter = new javax.swing.JButton();
Login = new javax.swing.JButton();
tf2 = new javax.swing.JTextField();
tf1 = new javax.swing.JTextField();
lb2 = new javax.swing.JLabel();
imgLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
getContentPane().setLayout(null);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testjframebgcolor.TestJFrameBGColorApp.class).getContext().getResourceMap(MyIndex.class);
lb1.setText(resourceMap.getString("lb1.text")); // NOI18N
lb1.setName("lb1"); // NOI18N
getContentPane().add(lb1);
lb1.setBounds(75, 85, 60, 20);
Enter.setText(resourceMap.getString("Enter.text")); // NOI18N
Enter.setName("Enter"); // NOI18N
getContentPane().add(Enter);
Enter.setBounds(230, 185, 65, 20);
Login.setText(resourceMap.getString("Login.text")); // NOI18N
Login.setName("Login"); // NOI18N
getContentPane().add(Login);
Login.setBounds(100, 185, 65, 20);
tf2.setName("tf2"); // NOI18N
getContentPane().add(tf2);
tf2.setBounds(165, 125, 120, 20);
tf1.setText(resourceMap.getString("tf1.text")); // NOI18N
tf1.setName("tf1"); // NOI18N
getContentPane().add(tf1);
tf1.setBounds(165, 85, 120, 20);
lb2.setText(resourceMap.getString("lb2.text")); // NOI18N
lb2.setName("lb2"); // NOI18N
getContentPane().add(lb2);
lb2.setBounds(75, 125, 60, 20);
imgLabel.setIcon(resourceMap.getIcon("imgLabel.icon")); // NOI18N
imgLabel.setText(resourceMap.getString("imgLabel.text")); // NOI18N
imgLabel.setName("imgLabel"); // NOI18N
imgLabel.setOpaque(true);
getContentPane().add(imgLabel);
imgLabel.setBounds(0, 0, 400, 330);
setBounds(450, 250, 400, 330);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyIndex().setVisible(true);
}
});
}
// Variables declaration - do not modify
protected javax.swing.JButton Enter;
protected javax.swing.JButton Login;
protected javax.swing.JLabel imgLabel;
protected javax.swing.JLabel lb1;
protected javax.swing.JLabel lb2;
protected javax.swing.JTextField tf1;
protected javax.swing.JTextField tf2;
// End of variables declaration
}
我找到了原因,是图片的路径问题,我用相对路径就加载不了,为什么啊?