Results 1 to 4 of 4
  1. #1
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Can't add another component

    I cannot add the component from this class:

    Java Code:
    package TestVersion;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class matWood {
    	
    	private int woodX = 250;
    	private int woodY = 100;
    	private int woodWidth = 10;
    	private int woodHeight = 50;
    	
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.DARK_GRAY);
    		g.fillRect(woodX, woodY, woodWidth, woodHeight);
    	}
    
    	public void setFocusable(boolean b) {
    		
    		
    	}
    
    }
    to the frame what this class creates:

    Java Code:
    package TestVersion;
    
    import java.awt.Color;
    import java.awt.Component;
    
    import javax.swing.JFrame;
    
    import TestVersion.CKeyListener;
    import TestVersion.GameWorld;
    import TestVersion.matWood;
    
    public class MYCoreWorld {
    	
    	
        public static void main(String[] args) {   	
        	
        	JFrame frame = new JFrame("My Core 0.0.1a");
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	
        	GameWorld gameWorld = new GameWorld();
        	//make sure that the panel can detect keyevents
        	gameWorld.setFocusable(true);
        	gameWorld.setBackground(Color.GREEN);
        	
        	matWood matWood = new matWood();
        	
        	CKeyListener listener = new CKeyListener(gameWorld);
        	gameWorld.addKeyListener(listener);
        	frame.add(matWood);
        	
        	frame.add(gameWorld);
        	
        	frame.setBackground(Color.GREEN);
        	frame.setSize(500, 500);
        	frame.setVisible(true);
        	
        	//make sure the JPanel has the focus
        	gameWorld.requestFocusInWindow();
        	
        }
    }
    The error is at
    Java Code:
    frame.add(matWood);
    and this is what it says:
    The method add(Component) in the type Container is not applicable for the arguments (matWood)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Can't add another component

    That's because matWood is not a Component. Since you are working with Swing, best to extend JComponent to make it a component. Also, by convention, class names should start with upper case letters.

    Also, if you at put the @Override annotation before the paintComponent() method, it may have given you a hint that something was missing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Can't add another component

    Okay thanks jim but it isn't adding the MatWood in the frame itself like it isn't showing ingame

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Can't add another component

    You are using classes with which I am unfamiliar. I know if you add a JComponent to a JFrame, it will be shown. Your GameWorld instance may be crowding out your component. I suggest you just focus on adding MatWood to your JFrame to convince yourself it is working. Then you can solve the bigger problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Calling methods of a component from a different component
    By Mate de Vita in forum AWT / Swing
    Replies: 0
    Last Post: 10-26-2012, 08:15 PM
  2. help with new Swing component
    By Proshitness in forum AWT / Swing
    Replies: 5
    Last Post: 06-09-2012, 01:39 PM
  3. What's the name of the component that...?
    By balla in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2011, 03:03 AM
  4. Component to use ?
    By pbaudru in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2010, 09:55 AM
  5. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 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
  •