0

i couldn't set a image as a background image on my jframe .. here is my code anyone can help me with that???

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);
                    }
                });  
    }       
}
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Bond007
  • 9
  • 4
  • 2
    Do it like every body who's asked this question. Either use a JLabel set with the image or create a custom component that paints the image under the paintComponent method and set either as the content pane of the frame – MadProgrammer Jan 11 '14 at 08:31
  • 1
    Nice code formatting. – PurkkaKoodari Jan 11 '14 at 08:35
  • -1 for Question format – ravibagul91 Jan 11 '14 at 08:42
  • 1
    For [example](http://stackoverflow.com/questions/13401109/java-add-background-image-to-frame/13405835#13405835) and [example](http://stackoverflow.com/questions/19699623/add-background-image-into-applet-frame/19699883#19699883) and [example](http://stackoverflow.com/questions/18777893/jframe-background-image/18777929#18777929) and [example](http://stackoverflow.com/questions/13943810/background-picture-problems-with-other-panels/13945212#13945212) – MadProgrammer Jan 11 '14 at 08:46
  • Question already asked, see this [http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe] – blackbishop Jan 11 '14 at 09:10
  • You will want to exercise and improve your Google skills. If you already searched and found something, and it's not working, then you will want to do debugging -- test to see what isn't working, and then ask a question with information gained from your debugging, or specific question about information that you searched on, but don't understand. – Hovercraft Full Of Eels Jan 11 '14 at 09:13
  • Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), especially the last link. Hope it might help :-) – nIcE cOw Jan 11 '14 at 11:55

0 Answers0