日期:2014-05-20  浏览次数:20816 次

java界面的背景图片怎么加
求帮这个图形界面加一个背景图片,最好能随着界面大小的变化而变化
Java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.net.*;

/**
 * 人事管理系统主界面
 */
public class HrMain extends JFrame implements ActionListener,TreeSelectionListener{
    //框架的大小
    Dimension faceSize = new Dimension(650, 450);
    //程序图标
    Image buaa;

    //建立Jtree菜单
    JTree tree;
    DefaultMutableTreeNode root;  //人事管理系统
    DefaultMutableTreeNode node1; //人员基本信息维护
    DefaultMutableTreeNode node2; //部门信息管理
    DefaultMutableTreeNode node3; //人员调动管理
    DefaultMutableTreeNode node4; //人员考核管理
    DefaultMutableTreeNode node5; //劳资管理
    DefaultMutableTreeNode leafnode;
    TreePath treePath;

    //主界面面板
    public static JSplitPane splitPane;
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    JLabel welcome = new JLabel();
    JScrollPane scrollPane;

    /**
     * 程序初始化函数
     */
     public HrMain() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);

        //添加框架的关闭事件处理
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        //设置框架的大小
        this.setSize(faceSize);
        //设置标题
        this.setTitle("人事管理系统");
        //程序图标
        buaa = getImage("buaa.jpg");
        this.setIconImage(buaa); //设置程序图标
        //设置自定义大小
        this.setResizable(false);

        try {    
            Init();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 程序初始化函数
     */
    private void Init() throws Exception {
        //Container contentPane = this.getContentPane();
        //contentPane.setLayout(new BorderLayout());

        //添加Jtree菜单
        root = new DefaultMutableTreeNode("人事管理系统");
        node1 = new DefaultMutableTreeNode("基本信息管理");
        node2 = new DefaultMutableTreeNode("人员调动管理");
        node3 = new DefaultMutableTreeNode("人员考核管理");
        node4 = new DefaultMutableTreeNode("劳资管理");
        //人员基本信息
        root.add(node1);
        leafnode = new DefaultMutableTreeNode("添加人员信息");
        node1.add(leafnode);
        leafnode = new DefaultMutableTreeNode("修改人员信息");
        node1.add(leafnode);
        leafnode = new DefaultMutableTreeNode("删除人员信息");
        node1.add(leafnode);
        leafnode = new DefaultMutableTreeNode("查询人员信息");
        node1.add(leafnode);
        leafnode = new DefaultMutableTreeNode("部门管理");
        node1.add(leafnode);
        //人员调动管理
        root.add(node2);
        leafnode = new DefaultMutableTreeNode("人员调动");
        node2.add(leafnode);
        leafnode = new DefaultMutableTreeNode("调动历史查询");
        node2.add(leafnode);
        //人员考核管理
        root.add(node3);
        leafnode = new DefaultMutableTreeNode("人员考核");
        node3.add(leafnode);
        leafnode = new DefaultMutableTreeNode("考核历史查询");
        node3.add(leafnode);
        //劳资管理
        root.add(node4);
        leafnode = new DefaultMutableTreeNode("劳资分配管理");
        node4.add(leafnode);
        leafnode = new DefaultMutableTreeNode("劳资历史查询");
        node4.add(leafnode);
        //生成左侧的JTree
        tree = new JTree(root);
        scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(150,400));
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //生成JPanel
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel1.add(scrollPane);
        welcome.setText("欢迎使用人事管理系统");
        welcome.setFont(new Font("Dialog",0,16));
        panel3.add(welcome);
        
        //生成JSplitPane并设置参数
        splitPane = new JSplitPane();
        splitPane.setOneTouchExpandable(false);
        splitPane.setContinuousLayout(true);
        splitPane.setPreferredSize(new Dimension(150, 400));
        splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(panel1);
        splitPane.setRightComponent(panel3);
        splitPane.setDividerSize(2);
        splitPane.setDividerLocation(161);
        //生成主界面
        this.setContentPane(splitPane);
        this.setVisible(true);

        //添加事件侦听
        tree.addTreeSelectionListener(this);

        //关闭程序时的操作
        this.addWindowListener(
            new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            }
        );
    }

    /**
     * 事件处理
     */
    public void actionPerformed(ActionEvent e) {

    }

    /**
     * JTree事件处理
     */
    public void valueChanged(TreeSelectionEvent tse) {
        DefaultMutableTreeNode dnode = 
            (DefaultMutableTreeNode)tse.getPath().getLastPathComponent();
        System.out.println("dnode="+dnode);
        String node_str = dnode.toString();
        if (node_str == "人事管理系统") { 
            splitPane.setRightComponent(panel3);
        }
        //人员基本信息管理树
        else if (node_str == "基本信息管理") { 
            //当选中后展开或关闭叶子节点
            treePath = new TreePath(node1.getPath());
            if(tree.isExpanded(treePath))
                tree.collapsePath(treePath);
            else
                tree.expandPath(treePath);
        }
        else if (node_str == "添加人员信息") { 
            Node11Panel node11Panel = new Node11Panel();
            splitPane.setRightComponent(node11Panel);
        }
        else if (node_str == "修改人员信息") { 
            Node12Panel node12Panel = new Node12Panel();
            splitPane.setRightComponent(node12Panel);
        }
        else if (node_str == "删除人员信息") { 
            Node13Panel node13Panel = new Node13Panel();
            splitPane.setRightComponent(node13Panel);
        }
        else if (node_str == "查询人员信息") { 
            Node14Panel node14Panel = new Node14Panel();
            splitPane.setRightComponent(node14Panel);
        }
        else if (node_str == "部门管理") { 
            Node15Panel node15Panel = new Node15Panel();
            splitPane.setRightComponent(node15Panel);
        }
        //人员调动管理树
        else if (node_str == "人员调动管理") { 
            //当选中后展开或关闭叶子节点
            treePath = new TreePath(node2.getPath());
            if(tree.isExpanded(treePath))
                tree.collapsePath(treePath);
            else
                tree.expandPath(treePath);
        }
        else if (node_str == "人员调动") { 
            Node21Panel node21Panel = new Node21Panel();
            splitPane.setRightComponent(node21Panel);
        }
        else if (node_str == "调动历史查询") { 
            Node22Panel node22Panel = new Node22Panel();
            splitPane.setRightComponent(node22Panel);
        }
        //人员考核管理树
        else if (node_str == "人员考核管理") {
            //当选中后展开或关闭叶子节点
            treePath = new TreePath(node3.getPath());
            if(tree.isExpanded(treePath))
                tree.collapsePath(treePath);
            else
                tree.expandPath(treePath);
        }
        else if (node_str == "人员考核") { 
            Node31Panel node31Panel = new Node31Panel();
            splitPane.setRightComponent(node31Panel);
        }
        else if (node_str == "考核历史查询") { 
            Node32Panel node32Panel = new Node32Panel();
            splitPane.setRightComponent(node32Panel);
        }
        //劳资管理树
        else if (node_str == "劳资管理") {
            //当选中后展开或关闭叶子节点
            treePath = new TreePath(node4.getPath());
            if(tree.isExpanded(treePath))
                tree.collapsePath(treePath);
            else
                tree.expandPath(treePath);
        }
        else if (node_str == "劳资分配管理") { 
            Node41Panel node41Panel = new Node41Panel();
            splitPane.setRightComponent(node41Panel);
        }
        else if (node_str == "劳资历史查询") { 
            Node42Panel nod42Panel = new Node42Panel();
            splitPane.setRightComponent(nod42Panel);
        }
    }

    /**
     * 通过给定的文件名获得图像
     */
    Image getImage(String filename) {
        URLClassLoader urlLoader = (URLClassLoader)this.getClass().
            getClassLoader();
        URL url = null;
        Image image = null;
        url = urlLoader.findResource(filename);
        image = Toolkit.getDefaultToolkit().getImage(url);
        MediaTracker mediatracker = new MediaTracker(this);
        try {
            mediatracker.addImage(image, 0);
            mediatracker.waitForID(0);
        }
        catch (InterruptedException _ex) {
            image = null;
        }
        if (mediatracker.isErrorID(0)) {
            image = null;
        }

        return image;
    }
}