Java窗体加入背景图片不可以什么情况?
importjava.awt.*;importjava.net.URL;importjavax.swing.*;publicclassMyImageIconextends...
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class MyImageIcon extends JFrame{
public MyImageIcon(){
Container container = getContentPane();
JLabel jl = new JLabel("zheshi yi ",JLabel.CENTER);
URL url = MyImageIcon.class.getResource("登陆背景.jpg");
Icon icon = new ImageIcon(url);
jl.setIcon(icon);
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setOpaque(true);
container.add(jl);
setSize(250,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyImageIcon();
}
}
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MyImageIcon.<init>(MyImageIcon.java:11)
at MyImageIcon.main(MyImageIcon.java:21) 展开
import java.net.URL;
import javax.swing.*;
public class MyImageIcon extends JFrame{
public MyImageIcon(){
Container container = getContentPane();
JLabel jl = new JLabel("zheshi yi ",JLabel.CENTER);
URL url = MyImageIcon.class.getResource("登陆背景.jpg");
Icon icon = new ImageIcon(url);
jl.setIcon(icon);
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setOpaque(true);
container.add(jl);
setSize(250,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyImageIcon();
}
}
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MyImageIcon.<init>(MyImageIcon.java:11)
at MyImageIcon.main(MyImageIcon.java:21) 展开
3个回答
展开全部
package free.images;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Login_frame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 7686507931273153138L;
private ImageIcon bj = new ImageIcon("images/bj14.jpg");
private ImageIcon loog = new ImageIcon("images/008.png");
private JPanel mainpane;
private JLabel bjbel;
public Login_frame(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(loog.getImage());
bjbel = new JLabel(bj);
bjbel.setBounds(0, 0, bj.getIconWidth(),bj.getIconHeight());
getLayeredPane().setLayout(null);
getLayeredPane().add(bjbel,new Integer(Integer.MIN_VALUE));
mainpane = new JPanel();
mainpane.setOpaque(false);
mainpane.setLayout(null);
setContentPane(mainpane);
setSize(800, 600);
setLocationRelativeTo(null);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Login_frame frame = new Login_frame();
frame.setVisible(true);
}
});
}
}
展开全部
图片多打了一个空格? 你看不到吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询