java为什么给窗体添加背景图片不是立即显示的?
packagecom.tarena.elts.ui;importjava.awt.BorderLayout;importjava.awt.Container;import...
package com.tarena.elts.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/** 登录界面 是一个具体窗口框 */
public class LoginFrame extends JFrame{
/**
*
*/
private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
private final int WEIGHT = Toolkit.getDefaultToolkit().getScreenSize().width;
private static final long serialVersionUID = -7993721800179709451L;
public LoginFrame() {
init();
}
/** 初始化界面组件和布局的 */
private void init(){
this.setTitle("登录系统");
this.setLocation((WEIGHT - 300) / 2, (HEIGHT - 200) / 2);
JPanel contentPane = createContentPane();
this.setContentPane(contentPane);
// setLocationRelativeTo(null);//居中
setSize(300,200);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
clientContext.exit(LoginFrame.this);
}
});
}
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon(this.getClass().getResource("login.png"));
JLabel jl1=new JLabel(icon);
jl1.setBounds(0, 0, 300, 200);
this.add(jl1);
}
private JPanel createContentPane(){
JPanel p = new JPanel(new BorderLayout());
p.setBorder(new EmptyBorder(8,8,8,8));
p.add(BorderLayout.NORTH,
new JLabel("登录考试系统", JLabel.CENTER));
return p;
}
} 展开
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/** 登录界面 是一个具体窗口框 */
public class LoginFrame extends JFrame{
/**
*
*/
private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
private final int WEIGHT = Toolkit.getDefaultToolkit().getScreenSize().width;
private static final long serialVersionUID = -7993721800179709451L;
public LoginFrame() {
init();
}
/** 初始化界面组件和布局的 */
private void init(){
this.setTitle("登录系统");
this.setLocation((WEIGHT - 300) / 2, (HEIGHT - 200) / 2);
JPanel contentPane = createContentPane();
this.setContentPane(contentPane);
// setLocationRelativeTo(null);//居中
setSize(300,200);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
clientContext.exit(LoginFrame.this);
}
});
}
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon(this.getClass().getResource("login.png"));
JLabel jl1=new JLabel(icon);
jl1.setBounds(0, 0, 300, 200);
this.add(jl1);
}
private JPanel createContentPane(){
JPanel p = new JPanel(new BorderLayout());
p.setBorder(new EmptyBorder(8,8,8,8));
p.add(BorderLayout.NORTH,
new JLabel("登录考试系统", JLabel.CENTER));
return p;
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询