• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding JButton

 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a basic battleship game and I'm at the point of trying to add a JButton to a welcome screen, as far as I know, I'm doing it okay and it's compiling, but see no reason why the button isn't added to the frame?

Here is the code, help would be appreciated =]


Hope I'm not doing anything too stupid =]

Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to add a component into a program via the paint method override which you shouldn't be doing. The paint and paintComponent methods are for painting and nothing but painting, and shouldn't be used for component initialization, for program logic, or for anything else.

Please go through the introductory Sun Swing tutorials for examples of how to create simple Swing programs. The tutorials vary from very good to phenomenal and will teach you a lot.

Probably a better way to do what you want is to create and show a modal JDialog that has your splash screen graphics and text on it.
[ April 22, 2008: Message edited by: pete stein ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally I don't like posting full solutions because it encourages the OP to not work out their problems and learn the concepts, and because I'm still new at this, and my solutions are often wrong, but I had too much dang fun with this one, especially the graphics.

 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Pete Stein

So your saying that I shouldn't be adding a button in the section of drawWelcomeScreen() method? or I shouldn't be doing any of what I do in that method. I've taken your advice and I'm currently going through the tutorials on the sun site.

Thanks for the help.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your saying that I shouldn't be adding a button in the section of drawWelcomeScreen() method? or I shouldn't be doing any of what I do in that method.

The former. But going forward, you also need to consider that -even though at the beginning the window shows the welcome screen- later on it will presumably show the game board. Pete's code sidesteps this nicely by making the welcome screen its own dialog, so there's no need to differentiate between showing one vs. showing the other in the same window.

As an aside, don't use the paint method. That's what AWT used, but for Swing applications you generally use the paintComponent method.
 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Ulf

Well, now I kind of have a simplified version of Bill's code which when, like Bills', the user clicks the 'New Game' button, a new window appears.

Now, I've managed to come into another problem from changing my code, before, when the user clicked the screen, a method called drawGrid() was called, and the grid drawn to the screen (like you explained I would do =P) and now I have a different window opening when I click enter, I don't really know how to call this method so that it runs on this new window that appears.

Hope I've explained what I need help with, and, as a side not, the tutorial that I'm learning from is a bit out of date, had to change a lot of things already lol.

Thanks,
[ April 23, 2008: Message edited by: Dan Brownlow ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Brownlow:
Hope I've explained what I need help with, and, as a side not, the tutorial that I'm learning from is a bit out of date, had to change a lot of things already
If you still need help, then you may benefit by posting code.
 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just realised, in my previous post, I wrote 'Bills code' when I actually meant I'd been trying out how to fix my program using code like yours.

What I was asking, because you showed me a new way of doing it, is how I would run my drawGrid() method on the JFrame that appears after the 'Enter' button has been pressed. I tried doing:


I thought this would be the section of the code where I would call drawGrid() method and I tried this but it had no result. Just need a little help on how I could go about calling this method.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Brownlow:
I've just realised, in my previous post, I wrote 'Bills code' when I actually meant I'd been trying out how to fix my program using code like yours.

No matter. I've been called a lot worse.

What I was asking, because you showed me a new way of doing it, is how I would run my drawGrid() method on the JFrame that appears after the 'Enter' button has been pressed. I tried doing:...

I'd call the drawGrid() method any place where you initialize the JFrame. This doesn't have to be done before the JDialog is created and displayed, and in fact could be easily done before.

For example, in my code above, you could call drawGrid in your JFrame initialization code like so:


One of the reasons to have splash screens is to give the user something to look at while your main program is being set up. For instance, if you have any processor-intense calculations that must be done, a smart way to do this is to start the process in a background thread, then show the splash screen on the EDT while the background thread churns away.
[ April 25, 2008: Message edited by: pete stein ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!