Results 1 to 2 of 2
  1. #1
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default JFrame not "updating"

    I'm tryng to start the UI for a java game i'm making for my friends and I.

    right now it's putting the new JButtons up but they won't show unless i edit the screen in any way.
    remade it and it still have the problem. wondering where i missed a simple , but. page 1
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    class Main {
    	
    	public static void main(String[]args){
    	boolean main = true;
    	
    
    	JFrame inter = new Inter(main);
    	inter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	inter.setSize(300,300);
    	inter.setLocation(500, 250);
    	inter.setVisible(main);
    		
    		System.out.println("test");
    	
    		
    	}
    	
    
    }
    page 2
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Inter extends JFrame{
    	//was trying to get it to work :-P
    
    	JButton create,play,pc,mobs,skills,items;
    
    
    public Inter(boolean main){
    	super("RPAGE");
    	setLayout(new FlowLayout());
    	play = new JButton("Play");
    	create = new JButton("Create");
    	add(play);
    	add(create);
    	main=true;
    	
    	cre crea =new cre();
    	create.addActionListener(crea);
    	play.addActionListener(crea);
    }
    
    private class cre implements ActionListener{
    public void actionPerformed(ActionEvent create1){
    	
    	
    	
    	pc=new JButton("Create New Pc's");
    	skills =new JButton("Create New Skill's");
    	items = new JButton("Create New Items");
    	mobs = new JButton ("Create New Mobs");
    	add(pc);
    	add(skills);
    	add(items);
    	add(mobs);
    
    
    	
    
    	
    }
    public void test(){
    
    }
    }
    }
    ty in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you add components to a container, you need to call revalidate on the container and sometimes repaint for the layout managers to layout the components and the latter for the container to repaint the newly laid out components and to paint over any components that have been removed. Since you're adding the components to the JFrame's contentPane, you'll need to make these calls on the contentPane, but after casting it to a JPanel:

    Java Code:
    	pc=new JButton("Create New Pc's");
    	skills =new JButton("Create New Skill's");
    	items = new JButton("Create New Items");
    	mobs = new JButton ("Create New Mobs");
    	add(pc);
    	add(skills);
    	add(items);
    	add(mobs)
    	((JPanel)getContentPane()).revalidate();
    	getContentPane().repaint();

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. Replies: 0
    Last Post: 04-20-2009, 07:26 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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