Skip to main content
gamedev.net
🔒 Locked

[java] Java Internal Frames

Started by zeroshot Nov 25, 2001 at 12:13 PM 2 replies 550+ views
Original Post
zeroshot
zeroshot
Can u plz have a look @ this code and tell me where the problem is can it aint loading the internal frame pricture up atm

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

public class DesktopTest extends JFrame {
    
    public DesktopTest()
    {
        super( "Using a Desktop" );
        
        JMenuBar bar = new JMenuBar( );
        
        JMenu addMenu = new JMenu( "Add" );
        
        JMenuItem newFrame = new JMenuItem( "Internal Frame");
        addMenu.add( newFrame );
        bar.add( addMenu );
        setJMenuBar( bar );
        
        
        final JDesktopPane theDesktop = new JDesktopPane( );
        getContentPane( ).add( theDesktop );
        
        newFrame.addActionListener(
            new ActionListener( ){
                public void actionPerformed( ActionEvent e )
                {
                    JInternalFrame frame = 
                        new JInternalFrame( "Internal Frame" , true, true, true, true );
                    
                    Container c = frame.getContentPane( );
                    MyJPanel panel = new MyJPanel( );
                    
                    c.add( panel , BorderLayout.CENTER );
                    frame.setSize(
                        panel.getImageWidthHeight().width,
                        panel.getImageWidthHeight( ).height );
                    frame.setOpaque( true );
                    theDesktop.add( frame );
                }
        }
        );
        
        setSize( 640, 480 );
        show( );
    }
    
    public static void main( String args[ ] )
    {
        DesktopTest app = new DesktopTest();
        app.addWindowListener(
            new WindowAdapter( ){
                public void windowClosing( WindowEvent e )
                {
                    System.exit( 0);
                }
        }
        );
    }
    
    public class MyJPanel extends JPanel{ 
        
        private ImageIcon imgIcon;
        
        public MyJPanel( )
        {
            imgIcon = new ImageIcon( "E:\\Java\\bug.gif" );
        }
        
        public void paintComponent( Graphics g )
        {
            imgIcon.paintIcon( this , g , 0  , 0 );
        }
        
        public Dimension getImageWidthHeight( )
        {
            return new Dimension( imgIcon.getIconWidth(), imgIcon.getIconHeight() );
        }
    }
}

 
thxs
bslayerw
bslayerw
The problem is in your MyJPanel object. You need to add the imgIcon to the JPanel. Also, there is no need to override the paintComponent method if you add the imgIcon to the JPanel.

What you will probably need to do is something like this in your MyJPanel constructor:

public MyJPanel( )
{
imgIcon = new ImageIcon( "E:\\Java\\bug.gif" );
JLabel l = new JLabel(imgIcon);
add(l);
}
harleyrana
harleyrana
Hey, it was just missing a few methods...
This tested fine.

/*
* FrameTest.java
*
* Created on 30 November 2001, 16:39
*/

import javax.swing.*;
import java.awt.*;
/**
*
* @author Harley Rana
* @version
*/
public class FrameTest extends JFrame {

/** Creates new FrameTest */
public FrameTest() {

super( "Using a Desktop" );
JMenuBar bar = new JMenuBar( );
JMenu addMenu = new JMenu( "Add" );
JMenuItem newFrame = new JMenuItem( "Internal Frame");
addMenu.add( newFrame );
bar.add( addMenu );
setJMenuBar( bar );
JDesktopPane theDesktop = new JDesktopPane( );

/** This section is what was missing */
this.setContentPane(theDesktop);

JInternalFrame iFrame = new JInternalFrame("Internal Icon Frame");
iFrame.reshape(5,5,300,300);
iFrame.getContentPane().add(new MyPanel());
iFrame.setVisible(true);
theDesktop.add(iFrame);

/** .... */

this.setSize(600,600);
this.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new FrameTest();
}


class MyPanel extends JPanel{

ImageIcon icon;

public MyPanel(){
icon = new ImageIcon("i:/crusier_enemy.gif");
}


public void paintComponent(Graphics g){
icon.paintIcon(this,g,0,0);
}

}

}
zeroshot
zeroshot
yeh, i read the api doc and found out i needed to set the frame to visible pity the book is suppose to do this aint it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.