Results 1 to 5 of 5
  1. #1
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Problem with loading multiple images in jframes

    Hello i am working on a project for my school and i run in to quite a few problems. I am making an interface using a JFrame for every menu.
    Now if i load an image in my main class i set Opague(false) and Layout(null) my image shows. But when i make a new class with almost same the code but with a different image
    it shows a grayscreen even when the opague is false.

    This is my first class:
    Java Code:
    package magicboxinterface;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author Lars
     */
    
    public class MagicBoxInterface extends JPanel   {
       
        
        public MagicBoxInterface() {
            setOpaque(false);
            //setLayout(new FlowLayout());
            setLayout(null);
        }
    
        public static void main(String[] args) {
            
            ImageIcon geluidenknop = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\geluiden.png");
            ImageIcon memoryknop = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\memory.png");
            
            JButton button = new JButton(geluidenknop);
            JButton button2 = new JButton(memoryknop);
            
            button.setBounds(270,250,451,87);
            button2.setBounds(270,400,451,87);
            
            
            JFrame myFrame = new JFrame("Magic Box");
            
            MagicBoxInterface frame = new MagicBoxInterface();
           
            frame.add(button);
            frame.add(button2);
            
            myFrame.add(frame);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setSize(1024,768);
            myFrame.setVisible(true);
            myFrame.setLocationRelativeTo(null);
            
                
            button.addActionListener(new Action());
            button2.addActionListener(new Action1());
            
        
        }
            
            static class Action implements ActionListener {
                
                @Override
                public void actionPerformed (ActionEvent e){
                    Menu2 menu2  = new Menu2();                          
                }  
        }   
         
            
           static class Action1 implements ActionListener {
                
                @Override
                public void actionPerformed (ActionEvent e){
                    
                    Menu4 menu4  = new Menu4();                          
                }  
        }       
            
        @Override
        public void paint(Graphics g) {
            
            Image a = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\menu.jpg");
            g.drawImage(a,0,0,getSize().width,getSize().height,this);
            super.paint(g);
        }   
    }
    And this is the second menu class:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package magicboxinterface;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     *
     * @author Lars
     */
    public class Menu2 extends JPanel  {
        
    public Menu2() {
       
        ImageIcon drums = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\drums.png");
        ImageIcon dieren = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\dierenknop.png");
        ImageIcon piano = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\piano.png");
        ImageIcon terug = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\terug.png");
        
        JButton drumsknop = new JButton(drums);
        JButton dierenknop = new JButton(dieren);
        JButton pianoknop = new JButton(piano);
        JButton terugknop = new JButton(terug);
               
        drumsknop.setBounds(270,250,451,87);
        dierenknop.setBounds(270,350,451,87);
        pianoknop.setBounds(270,450,451,87);
        terugknop.setBounds(370,560,270,50);
        
        MagicBoxInterface frame = new MagicBoxInterface();
           
        frame.add(drumsknop);
        frame.add(dierenknop);
        frame.add(pianoknop);
                    
        JFrame myFrame2 = new JFrame("Geluiden");     
       
        setOpaque(false);
        myFrame2.add(frame);
        myFrame2.setLayout(null); 
        myFrame2.add(drumsknop);
        myFrame2.add(dierenknop);
        myFrame2.add(pianoknop);
        myFrame2.add(terugknop);
        myFrame2.setVisible(true);
        myFrame2.setSize(1024,768);
        myFrame2.setLocationRelativeTo(null);
        
        terugknop.addActionListener(new Action());
        
    }
    
    @Override
    public void paint(Graphics g) {
            
            Image a = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Lars\\Dropbox\\Thuis\\MagicBoxInterface\\src\\magicboxinterface\\resources\\geluidenmenu.png");
            g.drawImage(a,0,0,getSize().width,getSize().height,this);
            super.paint(g);
        }
    
     static class Action implements ActionListener {
                
                @Override
                public void actionPerformed (ActionEvent e){
                    
                    Menu3 menu3  = new Menu3();  
                }  
        }      
    
    }
    Last edited by Larss; 05-10-2013 at 05:08 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Problem with loading multiple images in jframes

    It's [code] tags [/code], not [quote] tags [/quote]
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Problem with loading multiple images in jframes

    Ok sorry first time posting here il fix it

  4. #4
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Problem with loading multiple images in jframes

    Bump

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Problem with loading multiple images in jframes

    Building the JFrame inside the constructor for a JPanel that forms part of the JFrame strikes me as a Bad Idea.
    The JPanel has not finished being constructed at that point so all sorts of wierdness could be happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Using multiple gui/jframes
    By happiocholloh in forum New To Java
    Replies: 0
    Last Post: 02-18-2012, 02:49 AM
  2. multiple jframes question
    By dhazwa in forum New To Java
    Replies: 1
    Last Post: 09-08-2011, 10:28 AM
  3. Multiple JFrames
    By frguy in forum Advanced Java
    Replies: 11
    Last Post: 01-08-2011, 11:36 AM
  4. Help: passing data from multiple JFrames
    By newicons in forum New To Java
    Replies: 5
    Last Post: 06-26-2010, 03:55 PM
  5. Replies: 4
    Last Post: 11-13-2009, 01:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •