Swing JButton设置背景图片

import org.*;
import org.v1.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import org.AbstractFrame;
import org.config.FrameConfig;
import org.config.FrameManager;
import org.dao.BaseDaoFactory;
import org.dao.UserDao;
import org.event.ResizeEvent;
import org.util.FrameUtil;
import org.util.PropertiesUtil;

/**
 *
 * 主登录窗口类
 *
 * @author weijian.zhongwj
 */
public class Login extends AbstractFrame {

    public Login() {
        InitBackGround();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(FrameManager.currentWidth, FrameManager.currentHeight);
        this.setResizable(true);
        this.setMinimumSize(new Dimension(800,600));
//        this.setLocationRelativeTo(null);
        this.setLocation(FrameManager.currentX, FrameManager.currentY);
        this.setVisible(true);
        this.setTitle("");
        addButtonEnter();
    }
    private JPanel panel;

    public void InitBackGround() {
        final ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(FrameConfig.WELCOME_IMAGE));

        panel = new JPanel() {

            protected void paintComponent(Graphics g) {
                g.drawImage(icon.getImage(), 0, 0, FrameConfig.WIDTH, FrameConfig.HEIGH, null);
                super.paintComponent(g);
            }
        };
        panel.setLayout(null);
        panel.setOpaque(false);
        panel.setPreferredSize(new Dimension(750, 550));
        JScrollPane scrollPane = new JScrollPane(panel);
        getContentPane().add(scrollPane);
    }

    private void addButtonEnter() {
        ImageIcon icon = new ImageIcon(FrameConfig.WELCOME_BUTTON);
        JButton enter = new JButton(icon);
        enter.setBounds(900, 380, 380, 270);
        enter.setOpaque(false);
        enter.setContentAreaFilled(false);
        enter.setMargin(new Insets(0, 0, 0, 0));
        enter.setFocusPainted(false);
        enter.setBorderPainted(false);
        enter.setBorder(null);
        panel.add(enter);
        enter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

    }

    public static void main(String[] args) {
        try {
            JFrame.setDefaultLookAndFeelDecorated(true);
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            Login frame = new Login();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


posted @ 2012-06-30 22:39  MXi4oyu  阅读(958)  评论(0编辑  收藏  举报