如何在JFrame上设置背景图像

时间:2014-01-11 08:27:47

标签: java swing jframe jpanel

我无法在我的jframe上将图像设置为背景图像..这是我的代码,任何人都可以帮助我吗???

import javax.swing.*; 
import javax.swing.WindowConstants.*;  
import java.awt.*; 
import java.awt.event.*;


public class project2 extends JFrame{   
    private JFrame newFrame = new JFrame();  
    private JButton exitButton;
    public project2() {     
        mainWindow();   
    }

    private void mainWindow() {     
        JPanel mainPanel = new JPanel();        
        JButton distance = new JButton("Distance"); 
        JButton time = new JButton("Time");     
        JButton temp = new JButton("Temperature");  
        JButton weight = new JButton("Weight");          
        JButton exit = new JButton("Exit");
        JLabel unitMeasure = new JLabel("What unit of measure would you like to convert?");      unitMeasure.setFont(new Font("Serif", Font.BOLD,
                18));           
        setDefaultCloseOperation(EXIT_ON_CLOSE);    
        setSize(600,480);   
        setTitle("Universal Converter");    
        setResizable(false); 
        setLayout(new FlowLayout()); 

        distance.addActionListener( 
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        actionWindow("Distance", 200, 200);  
                    }
                });      
        time.addActionListener( 
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        actionWindow("Time", 300, 300);  
                    }
                });     
        temp.addActionListener( 
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        actionWindow("Temp", 400, 400);  
                    }
                });     
        weight.addActionListener( 
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        actionWindow("Weight", 500, 500);    
                    }
                });     
        exit.addActionListener(     
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        System.exit(0);  
                    }
                });
        add(unitMeasure, BorderLayout.CENTER);  
        mainPanel.add(distance, BorderLayout.CENTER);   
        mainPanel.add(time, BorderLayout.CENTER);   
        mainPanel.add(temp, BorderLayout.CENTER); 
        mainPanel.add(weight, BorderLayout.CENTER);
        add(mainPanel);     
        add(exit);  
        setLocationRelativeTo(null);  
    }
    public void actionWindow(String title, int xValue, int yValue) {    
        newFrame = new JFrame(title);   
        newFrame.setSize(xValue,    yValue);            
        newFrame.setUndecorated(false);     
        exitButton = new JButton();     
        newFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);   
        newFrame.setLayout(new FlowLayout());   
        exitButton.setText("Close");    
        exitButton.addActionListener(       
                new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {             
                        newFrame.setVisible(false);      
                        setVisible(true);
                    }
                });      newFrame.add(exitButton);   
                newFrame.setLocationRelativeTo(null);   
                newFrame.setVisible(true);       
                setVisible(false); 
    }       

    public static void main(String args[]) {    
        EventQueue.invokeLater(     
                new Runnable() {
                    public void run() {
                        new project2().setVisible(true);
                    }
                });  
    }       
}

0 个答案:

没有答案