Buttons

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.

thanks for any help,
scov

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.

that sounds like exactly what i need. could you explain how that is done? sorry im a newb.

thanks,
scov

Maybe i was unclear i just want to understand how to make a button open a new panel.

maybe its not called a panel but you know what i mean. and im not opposed to using swing if that makes it easier.

You probably mean you want to open a new Frame. You mean you want it to be a separate window, right?

Frame f = new Frame();
f.setSize(frameWidth, frameHeight);
f.setVisible(true);

Obviously, you need to add some stuff to the frame if you want it to have anything on it, but that’s the basic idea.

I’m assuming you know how to create the ActionListener for the button.

All of this is covered in the Java Tutorial at http://java.sun.com/docs/books/tutorial/.

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.

thanks,
scov

i also do not want to buttons to stay visible after being pushed. Once the button is pushed the existing card should disappear completely giving way to a completely new screen.

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.

ok so how do i go about editing each panel and implementing card layout?

also i found a game that is like what im trying to do.

http://www.javaonthebrain.com/java/3dblox/

you can push each button but then when play is pushed the buttons disappear and the game begins.

thanks,
scov

if its easier than explaining you could just post the source for a simple applet that has a button and the button opens a new panel.

simple google search yielded these results:

http://72.5.124.55/docs/books/tutorial/uiswing/layout/card.html

http://journals.ecs.soton.ac.uk/java/tutorial/ui/layout/card.html

you should be able to figure it out from there…

so i have this code so far…

import java.applet.;
import java.awt.
;
import java.awt.event.*;

public class MegaShot extends Applet implements ActionListener
{

Button playButton;
Image backGround;

Panel cardPanel;
Panel firstP, secondP;
CardLayout ourLayout;


public void init()
{
    /*backGround = getImage(getCodeBase(), "background.gif");*/
    cardPanel = new Panel();
    ourLayout = new CardLayout();
    cardPanel.setLayout(ourLayout);
    
    playButton = new Button("Play!");
    playButton.addActionListener(this);
    
    firstP = new Panel();
    firstP.setBackground(Color.blue);
    firstP.add(playButton);
    
    secondP = new Panel();
    secondP.setBackground(Color.green);
    
    
    this.setLayout(new BorderLayout());
    this.add(cardPanel, BorderLayout.CENTER);
    
    cardPanel.add(firstP, "First");
    cardPanel.add(secondP, "Second");
      
}
    
    
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == playButton)
    {   
        ourLayout.show(cardPanel,"Second");
        
    }
        
}

public void start()
{
}

public void stop()
{
}

/*public void paint (Graphics g)
{
    g.drawImage(backGround, 0, 0, 1000,600, this);
    
    
    
}*/

}

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.

thanks,
scov

This is a tutorial on layout managers
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

This is The Java Tutorial
http://java.sun.com/docs/books/tutorial/

You can learn all you need to know from these 2 links. I would recommend doing them both from start to finish.

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=neoskunk]however if i leave out the this.setLayout part nothing shows up. why is this?
[/quote]
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.

thanks,
scov

Here is a sample of using CarLayout. Now you should read those tutorials to understand how it works.


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestApplet extends JApplet implements ActionListener {
	JButton button1;
	JButton button2;
	CardLayout card;
	Container contentPane;

	public void init() {
		card = new CardLayout();
		setLayout(card);
		contentPane = getContentPane();

		JPanel panel1 = new JPanel();
		add(panel1, "card1");
		button1 = new JButton("Go to panel 2");
		panel1.add(button1);
		button1.addActionListener(this);

		JPanel panel2 = new JPanel();
		add(panel2, "card2");
		button2 = new JButton("Go to panel 1");
		panel2.add(button2);
		button2.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e){
		Object source = e.getSource();

		if(source.equals(button1)) {
			card.show(contentPane, "card2");
		}
		else if(source.equals(button2)) {
			card.show(contentPane, "card1");
		}
	}
}

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.

alright ill go read up.

thanks for the help.