Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Remove button on click

    I am making a vertical scroller game, and first I have the launcher.
    There is a button for "load game" and "new game." those are the only two items on the screen until one is clicked. I would like to remove them on click, but when I put remove(lGame);remove(nGame); in the actionPerformed method of the action listener, it freezes the program on click. What do I do to fix this?

    Code:
    public class Launcher extends JFrame{
    	
    	private JButton nGame;
    	private JButton lGame;
    	private String dir = "";
    	
    	public Launcher(){
    		super("Space Invaders");
    		setLayout(new FlowLayout(FlowLayout.CENTER));
    		
    		if(isWindows()){
    			System.out.println("Windows");
    			dir = "%APPDATA%\\.SInvaders\\save.sav";
    		}
    		if(isUnix() || isMac()){
    			System.out.println("Linux/Unix/Mac");
    			dir = "~/.SInvaders/save.sav";
    		}
    		
    		nGame = new JButton("New Game");
    		lGame = new JButton("Load Save");
    		File sav = new File(dir);
    		
    		add(nGame);
    		add(lGame);
    		
    		nGame.addActionListener(new HC(false));
    		lGame.addActionListener(new HC(true));
    		
    		if(!sav.exists()){
    			lGame.setEnabled(false);
    		}
            }
    //methods for OS detection
    
            private class HC implements ActionListener{
    		
    		boolean load;
    		
    		public HC(boolean load){
    			this.load = load;
    		}
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			remove(nGame);
    			remove(lGame);
    		}
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You'll need to repaint the parent frame. Most reliable way would be to take a component into the constructor of the HC to handle the repaint from there. Most generic way would be to pull the parent from the nGame and lGame onclick.

    Edit:
    Wait, this is an inner class. You can simply call repaint() inside the actionPerformed.

  • Users who have thanked Fou-Lu for this post:

    Scriptr (06-13-2012)

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You'll need to repaint the parent frame. Most reliable way would be to take a component into the constructor of the HC to handle the repaint from there. Most generic way would be to pull the parent from the nGame and lGame onclick.

    Edit:
    Wait, this is an inner class. You can simply call repaint() inside the actionPerformed.
    I called repaint from actionPerformed, and it did nothing.

    I then realized that you intended for me to add it, not replace the current content.

    Thank you for your help; just thought I'd clear that up for someone else who comes across this thread later.

  • #4
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    JPanel

    Could I (if so should I) put a JPanel inside my JFrame? Or, could/should I use a JPanel INSTEAD OF a JFrame?

    My current code is to make the JPanel in the Game class and then add a Game object to my JFrame. Please correct me if that is wrong, but it works for basic things.
    Last edited by Scriptr; 06-14-2012 at 01:50 AM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You should put your components on a panel, then put the panel on the frame. Lets you segment the layout since panels will take layout managers as well.


  •  

    Posting Permissions

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