New to gui

Hi,

I'm kinda new to java swing and stuff like that and i have some questions.

I'm programming a game so i need multiple screens:

- one basic screen with: new game, load game, about,...

- second screen to choose your character

- third to show a fight

How could i do that. My StartUp class makes a domaincontroller object and passes this through to my startFrame.

Then in my startFrame ( the basic screen) I have made some buttons. For example when you click new game, this code is being executed

//action performed

secondscreenJFrame second =new secondscreenJFrame();

second.setVisible(true);

this.dispose;

Is this the right way to switch to screens or should i use another technique. Anyway it works but i can see the screens switching (sort of flash) and I don't like that.

Final question: how can I program that my new screen is displayed at the position of the previous screen? So I don't have to drag it at the previous position all the time.

Greetz Daan.

[1154 byte] By [D-a-a-na] at [2007-11-27 2:27:09]
# 1
and you need screens for:-> new game-> load game-> aboeut, help etc. ppi would try it with the CardLayout. best regards Marcel
marcel_heckera at 2007-7-12 2:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

>

> i would try it with the CardLayout.

>

With the CardLayout, I'm not familiar with all these layouts but I found something in the java docs. But when I use this layout, I don't have to make JFrames but JPanels, is that correct? Maybe somebody could explain me the difference between them, that would be nice :)

Greetz Daan.

D-a-a-na at 2007-7-12 2:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 3
Or maybe I should ask an other question.I want 3 screens as described before.Which layout suits most and how do I manage the different screens. Greetz Daan
D-a-a-na at 2007-7-12 2:37:31 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I want 3 screens as described before.

> Which layout suits most

generally, no single layout suits most - depends on what you're trying to do.

often this means nested layouts - each with a different feature.

> and how do I manage the different screens.

if you want your game to return to the main screen for 'new game',

then 'select a character', a cardlayout would be a good solution, where you just

cardlayout.show([the appropriate screen]);

if the new/load and character screens are not to be reused, something along

these lines is one of the ways (code is rough)

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

JFrame f = new JFrame();

public void buildGUI()

{

final StartPanel startPanel = new StartPanel();

f.setTitle("Start New Game or Load Game?");

f.setLayout(new GridBagLayout());

f.getContentPane().add(startPanel,new GridBagConstraints());

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

startPanel.rbNew.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

final CharacterPanel characterPanel = new CharacterPanel();

f.remove(startPanel);

f.getContentPane().add(characterPanel,new GridBagConstraints());

f.validate();

f.repaint();

characterPanel.rbChar1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

setGamePanel(characterPanel,characterPanel.rbChar1.getText());

}

});

characterPanel.rbChar2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

setGamePanel(characterPanel,characterPanel.rbChar2.getText());

}

});

}

});

startPanel.rbLoad.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

setGamePanel(startPanel,"Arnie Schwarzenegger");

}

});

}

class StartPanel extends JPanel

{

JRadioButton rbNew = new JRadioButton("New Game");

JRadioButton rbLoad = new JRadioButton("Load Game");

public StartPanel()

{

setLayout(new GridLayout(2,1));

ButtonGroup group = new ButtonGroup();

group.add(rbNew); group.add(rbLoad);

add(rbNew);

add(rbLoad);

}

}

class CharacterPanel extends JPanel

{

JRadioButton rbChar1 = new JRadioButton("The Hulk");

JRadioButton rbChar2 = new JRadioButton("The Rock");

public CharacterPanel()

{

f.setTitle("Select a Character");

setLayout(new GridLayout(2,1));

ButtonGroup group = new ButtonGroup();

group.add(rbChar1); group.add(rbChar2);

add(rbChar1);

add(rbChar2);

}

}

class GamePanel extends JPanel

{

public GamePanel(String character)

{

f.setTitle("Start Fighting");

setLayout(new GridLayout(2,1));

JLabel label = new JLabel("Fight in Progress",JLabel.CENTER);

label.setFont(label.getFont().deriveFont(24f));

add(label);

add(new JLabel("Your character is: "+character));

}

}

public void setGamePanel(JPanel oldPanel,String character)

{

GamePanel gamePanel = new GamePanel(character);

f.remove(oldPanel);

f.getContentPane().add(gamePanel,new GridBagConstraints());

f.validate();

f.repaint();

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 2:37:31 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thank you very much Michael, that really did the trick.Instead of making innerclasses I made new classes (JPanels) so I could use Jigloo Gui builder to design them easily.Greetz Daan.
D-a-a-na at 2007-7-12 2:37:31 > top of Java-index,Desktop,Core GUI APIs...