Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2012, 10:22 PM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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);
		}
	}
}
Scriptr is offline   Reply With Quote
Old 06-13-2012, 10:44 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Scriptr (06-13-2012)
Old 06-13-2012, 11:05 PM   PM User | #3
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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.
Scriptr is offline   Reply With Quote
Old 06-14-2012, 12:09 AM   PM User | #4
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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..
Scriptr is offline   Reply With Quote
Old 06-14-2012, 03:52 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:14 PM.


Advertisement
Log in to turn off these ads.