如何在一个JFrame上面设置背景图片
public class Dome extends javax.swing.JFrame {
JLabel topLabel;
JLabel huiyixinxi,yudingxinxi,yanshi,tiqianjiesu;
JButton exitButton,loginButton;
/** Creates new form Dome */
public Dome() {
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.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
this.setBounds(0, 0, 1024, 768);
this.setResizable(false);//
topLabel=new JLabel();
topLabel.setIcon(new ImageIcon("images/mitbg.bmp") );
add(topLabel);
topLabel.setLayout(null);
yudingxinxi = new JLabel("预定信息");
yudingxinxi.setForeground(Color.WHITE);
huiyixinxi = new JLabel("会议信息");
yanshi = new JLabel("会议延时");
tiqianjiesu = new JLabel("提前结束");
exitButton = new JButton("退 出");
loginButton = new JButton("登 录");
ButtonListener bListener = new ButtonListener();
// 按钮注册事件监听器
exitButton.addActionListener(bListener);
loginButton.addActionListener(bListener);
MyKeyListener keylis=new MyKeyListener();
yudingxinxi.setSize(45,20);
yudingxinxi.setLocation(40,280);
yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
topLabel.add(yudingxinxi);
yanshi.setSize(50,20);
yanshi.setLocation(85,280);
topLabel.add(yanshi);
huiyixinxi.setSize(40,20);
huiyixinxi.setLocation(140,280);
topLabel.add(huiyixinxi);
tiqianjiesu.setSize(50,20);
tiqianjiesu.setLocation(195,280);
topLabel.add(tiqianjiesu);
exitButton.setSize(80,20);
exitButton.setLocation(260,280);
topLabel.add(exitButton);
loginButton.setSize(80,20);
loginButton.setLocation(350,280);
topLabel.add(loginButton);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//setSize(500,350);
setVisible(true);
}// </editor-fold>
//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Dome().setVisible(true);
}
});
}
private void checkNamePwd(){
}
private void exitWindow(){
System.exit(0);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// 获取事件源
Object source = e.getSource();
if(source == exitButton)
{
exitWindow();
}
else if(source == loginButton)
{
checkNamePwd();
}
}
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent ke){
int code=ke.getKeyCode();
if(code==10){
checkNamePwd();
}
else if(code==27){
exitWindow();
}
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
// End of variables declaration//GEN-END:variables
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Dome extends javax.swing.JFrame {
JLabel topLabel;
JLabel huiyixinxi, yudingxinxi, yanshi, tiqianjiesu;
JButton exitButton, loginButton;
public void paint(Graphics g) {
super.paint(g);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, (int) screenSize.getWidth(),
(int) screenSize.getHeight());
g.drawImage(new ImageIcon(
"C:/Users/eacfgjl/Desktop/clock.png").getImage(), 0, 0, getWidth(), getHeight(), this);// 这里换成你图片的路径
}
/** Creates new form Dome */
public Dome() {
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.
*/
// GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
this.setBounds(0, 0, 1024, 768);
this.setResizable(false);//
topLabel = new JLabel();
topLabel.setIcon(new ImageIcon("images/mitbg.bmp"));
add(topLabel);
topLabel.setLayout(null);
yudingxinxi = new JLabel("预定信息");
yudingxinxi.setForeground(Color.WHITE);
huiyixinxi = new JLabel("会议信息");
yanshi = new JLabel("会议延时");
tiqianjiesu = new JLabel("提前结束");
exitButton = new JButton("退 出");
loginButton = new JButton("登 录");
ButtonListener bListener = new ButtonListener();
// 按钮注册事件监听器
exitButton.addActionListener(bListener);
loginButton.addActionListener(bListener);
MyKeyListener keylis = new MyKeyListener();
yudingxinxi.setSize(45, 20);
yudingxinxi.setLocation(40, 280);
yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
topLabel.add(yudingxinxi);
yanshi.setSize(50, 20);
yanshi.setLocation(85, 280);
topLabel.add(yanshi);
huiyixinxi.setSize(40, 20);
huiyixinxi.setLocation(140, 280);
topLabel.add(huiyixinxi);
tiqianjiesu.setSize(50, 20);
tiqianjiesu.setLocation(195, 280);
topLabel.add(tiqianjiesu);
exitButton.setSize(80, 20);
exitButton.setLocation(260, 280);
topLabel.add(exitButton);
loginButton.setSize(80, 20);
loginButton.setLocation(350, 280);
topLabel.add(loginButton);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// setSize(500,350);
setVisible(true);
}// </editor-fold>
// GEN-END:initComponents
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Dome().setVisible(true);
}
});
}
private void checkNamePwd() {
}
private void exitWindow() {
System.exit(0);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
if (source == exitButton) {
exitWindow();
} else if (source == loginButton) {
checkNamePwd();
}
}
}
class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();
if (code == 10) {
checkNamePwd();
} else if (code == 27) {
exitWindow();
}
}
// GEN-BEGIN:variables
// Variables declaration - do not modify
// End of variables declaration//GEN-END:variables
}
}
Image image = Toolkit.getDefaultToolkit().getImage("c:\\abc.jpg");
Icon icon = new ImageIcon(image);
jlabel.setIcon(icon);