Hi, im very new to java programming but i was wondering how do you make a button that will go to a new screen?

for example i want to make a title screen for my game with a "Play Now" button. How would i make said button go to a new screen? Say the choose character screen or something of that nature. I already now how to make the button just not how to make the button do anything. This is all in an applet and im trying to only use awt not swing.

One way is to use a CardLayout. Put your title panel (or whatever) in one card. Then put your game panel in another card. CardLayout allows you to switch between different cards. This allows you to switch between any number of screens.

thanks for the help but actually i do not want to open a new frame. i would like to keep it all in the applet. basically i want to be able to create several different screens individually and then be able to link them together with buttons. is this possible? and yes i do know how to use the actionlistener.

CardLayout is what you want to use. You create a number of "cards" that are just Panels that contain your screens. You then tell CardLayout which one you want to display. You can only display one at a time. You add an ActionListener to the buttons you want to use to switch screens with. When the button is clicked you make the switch in the actionPerformed() method.

it yeilds a panel with a button that leads to another panel.....however if i leave out the this.setLayout part nothing shows up. why is this? also the button will only appear in the top center. i knew how to move buttons around with setLayout(null); but how would i go about moving buttons around on separate panels? also how do i edit individual panels? such as painting and animations etc.

First of all, your code would be more modular if you separated the code for each panel into separate methods or even separate classes. Create the first panel with a createFirstPanel method, and create the second panel with a createSecondPanel method. Put all the code for each panel in its method.

That shouldn't affect how your code works, but it would make it easier to read.

Quote from: neoskunk

however if i leave out the this.setLayout part nothing shows up. why is this?

Without the call to setLayout, your applet most likely has a FlowLayout. cardPanel's minimum, preferred, and maximum sizes are probably set to strange values. And FlowLayout probably uses those sizes.

I think the best thing you could do is look up layouts in the Java Tutorial I linked to before. You can mix and match layouts to get the effect you want.

Mixing null layouts with other layouts can cause problems. However, you can create a class that acts like a null layout but can work well with other layouts. You can learn about this from http://www.swingwiki.org/workaround:fixed_position_layout. I used to use similar code until I coded my own special set of layouts for my own special widgets.

i have attempted to put each card in a separate method and idealy yes i would love to make them each a seperate class so they could be tested individually. however i guess i dont quite understand the language yet. I'm going to go over those links but once i create a panel in say a method or class how do i call it or get it to show? it can't be the same as just cardLayout.show(....);. seems like a method call or something is necessary. anyways thanks for the help. i'm going to go over those links hopefully i wont be back with such newb questions.

thank you very much for the example. i have a pretty good understanding of the layouts now. could you adapt that example to create the 2 cards in seperate methods or even classes? also what are the advantages and disadvantages of swing vs regular awt? i know swing is better for GUI but in terms of games would you suggest using it?

Look, if you're still having problems with the syntax for classes and methods, you really need to read the Java Tutorial or find some other source of information. All the information is there, and there's got to be plenty of examples already posted on the Internet.

Swing is far more customizable than AWT. There may be a speed difference between the two, but both are pretty slow.

Try to avoid them both for the actual game in your program. If you're making a Super Mario clone, there should be one panel (or whatever) containing the entire game area (i.e. where Mario can move around). You should store all the objects in the game yourself and draw them to this panel. Mario should not be a Swing component!!!

You can use components for things like the buttons on the title screen. They work great for that. There's no way that your title screen is going to be too slow. You can even have buttons like that on your main screen, so long as you don't do something silly like make your game characters components.

If you're going to actually use AWT/Swing buttons, tables, etc., I would go with Swing. If you just need a frame to draw in, I would go with AWT because you shouldn't need any of Swing's features.

ok im making progress now. i understand how to call methods and classes now and i understand the layout. however once i put the different cards into their own methods the buttons won't work. buttons have to stay separate or am i doing something wrong?

publicvoidpaint(Graphicsg) {//do all the stuff that paint is suposed to dosuper.paint(g);

//do all your custom graphics here }}

This class just puts all the code for defining the title panel in its constructor. TitlePanel extends Panel, so it is a Panel, just with additional behavior. You will need to look up subclassing in the Java Tutorial.

Do all the graphics within the paint method. This method already exists within Panel. It gets called whenever the applet is drawn. By overriding this method in the TitlePanel class, you're just changing its behavior.

I changed the ActionListener from being one listener for all the buttons (of which there was only one) to be one listener for each button. This makes it easier because you don't have to store playButton or check which button was pressed any more.

When you define a class like I did there, that's called an anonymous class. That's something else you should look up.

Look, you should just read the whole Java Tutorial, and then you won't need to ask all these questions.

ok all goes well. now im creating a character select screen. the characters i draw here i would like to implement in game play and other panels. what would be the best way to create these? put them in their own classs? method? maybe put each character on a transparent panel?

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org