2011-04-14 14 views
6

Sto scrivendo un programma per un gioco black jack. È un compito che non dobbiamo usare per gui, ma lo sto facendo per un credito extra. Ho creato due frame e stanno lavorando. Sul secondo frame voglio essere in grado di tornare al primo quando viene premuto un pulsante. Come faccio a fare questo?GUI multiple frames switch

prima finestra .............

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


public class BlackJackWindow1 extends JFrame implements ActionListener 
{ 
    private JButton play = new JButton("Play"); 
    private JButton exit = new JButton("Exit"); 
    private JPanel pane=new JPanel(); 
    private JLabel lbl ; 

    public BlackJackWindow1() 
    { 
    super(); 
    JPanel pane=new JPanel(); 
    setTitle ("Black Jack!!!!!") ; 
    JFrame frame = new JFrame(""); 

    setVisible(true); 
    setSize (380, 260) ; 
    setLocation (450, 200) ; 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; 

    setLayout(new FlowLayout()); 
    play = new JButton("Start"); 
    exit = new JButton("exit"); 
    lbl = new JLabel ("Welcome to Theodores Black Jack!!!!!"); 

    add (lbl) ; 
    add(play, BorderLayout.CENTER); 
    play.addActionListener (this); 
    add(exit,BorderLayout.CENTER); 
    exit.addActionListener (this); 
    } 
    @Override 
    public void actionPerformed(ActionEvent event) 
    { 
    // TODO Auto-generated method stub 
    BlackJackWindow2 bl = new BlackJackWindow2(); 
    if (event.getSource() == play) 
    { 
     bl.BlackJackWindow2(); 
    } 
    else if(event.getSource() == exit){ 
     System.exit(0); 
    } 
    } 

seconda finestra ....

import javax.swing.* ; 

import java.awt.event.* ; 
import java.awt.* ; 
import java.util.* ; 

public class BlackJackWindow2 extends JFrame implements ActionListener 
{ 
    private JButton hit ; 
    private JButton stay ; 
    private JButton back; 
    //private JLabel lbl; 

    public void BlackJackWindow2() 
    { 
    // TODO Auto-generated method stub 
    JPanel pane=new JPanel(); 
    setTitle ("Black Jack!!!!!") ; 
    JFrame frame = new JFrame(""); 

    setVisible(true); 
    setSize (380, 260) ; 
    setLocation (450, 200) ; 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ; 

    setLayout(new FlowLayout()); 
    hit = new JButton("Hit"); 
    stay = new JButton("stay"); 
    back = new JButton("return to main menu"); 

    // add (lbl) ; 
    add(hit, BorderLayout.CENTER); 
    hit.addActionListener (this) ; 
    add(stay,BorderLayout.CENTER); 
    stay.addActionListener (this) ; 
    add(back,BorderLayout.CENTER); 
    back.addActionListener (this) ; 
    } 

    @Override 
    public void actionPerformed(ActionEvent event) 
    { 
    // TODO Auto-generated method stub 
    BlackJackWindow1 bl = new BlackJackWindow1(); 
    if (event.getSource() == hit) 
    { 
     //code for the game goes here i will complete later 
    } 
    else if(event.getSource() == stay){ 
     //code for game goes here i will comeplete later. 
    } 
    else 
    { 
     //this is where i want the frame to close and go back to the original. 
    } 
    } 
} 
+1

Si consiglia di prendere in considerazione l'utilizzo di un solo JFrame e l'utilizzo di un JTabbedPane con due riquadri secondari. – MeBigFatGuy

+0

@MeBigFatGuy ha ragione; c'è un esempio di 'JTabbedPane' [qui] (http://stackoverflow.com/questions/5617957). – trashgod

risposta

5

Il secondo telaio richiede un riferimento il primo fotogramma in modo che possa impostare la messa a fuoco sul primo fotogramma.

Anche le classi estendono JFrame ma stanno anche creando altri frame nei loro costruttori.

+0

ho cercato e ho scoperto che potevo fare questo. setVisible (false); dispose(); sulla dichiarazione else e chiude il frame e ritorna al frame originale e nel frame originale faccio lo stesso dopo la chiamata per la seconda finestra, sembra funzionare. cosa ne pensi? – namdizy

+0

Cosa succede se il tuo utente riduce al minimo il primo fotogramma? Vuoi davvero chiudere il secondo fotogramma? Sembra che dovresti usare una finestra di dialogo modale se è davvero quello che cerchi. – jzd

+0

controllerò la finestra di dialogo modale. Se il mio utente è sul primo frame e ha premuto il pulsante "start", passa al secondo frame e chiude il primo, quindi quando viene premuto il pulsante "return to menu" chiude il secondo frame e torna al primo. quindi entrambi i frame non sono aperti allo stesso tempo. – namdizy

3

Un paio di suggerimenti:

Stai salvando componenti a un JPanel che utilizza FlowLayout ma sta usando le costanti BorderLayout quando si fa questo, che non si deve fare in quanto non ha senso:

add(play, BorderLayout.CENTER); 

Piuttosto, se si utilizza FlowLayout, è sufficiente aggiungere i componenti senza tali costanti.

Inoltre, piuttosto che scambiare JFrames, si potrebbe prendere in considerazione l'utilizzo di un CardLayout e lo scambio di dati in un singolo JFrame. Per esempio:

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

public class FooBarBazDriver { 
    private static final String INTRO = "intro"; 
    private static final String GAME = "game"; 
    private CardLayout cardlayout = new CardLayout(); 
    private JPanel mainPanel = new JPanel(cardlayout); 
    private IntroPanel introPanel = new IntroPanel(); 
    private GamePanel gamePanel = new GamePanel(); 

    public FooBarBazDriver() { 
     mainPanel.add(introPanel.getMainComponent(), INTRO); 
     mainPanel.add(gamePanel.getMainComponent(), GAME); 

     introPanel.addBazBtnActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      cardlayout.show(mainPanel, GAME); 
     } 
     }); 

     gamePanel.addBackBtnActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      cardlayout.show(mainPanel, INTRO); 
     } 
     }); 
    } 

    private JComponent getMainComponent() { 
     return mainPanel; 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("Foo Bar Baz"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new FooBarBazDriver().getMainComponent()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

class IntroPanel { 
    private JPanel mainPanel = new JPanel(); 
    private JButton baz = new JButton("Baz"); 
    private JButton exit = new JButton("Exit"); 

    public IntroPanel() { 
     mainPanel.setLayout(new FlowLayout()); 
     baz = new JButton("Start"); 
     exit = new JButton("exit"); 

     mainPanel.add(new JLabel("Hello World")); 
     mainPanel.add(baz); 
     mainPanel.add(exit); 

     exit.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      Window win = SwingUtilities.getWindowAncestor(mainPanel); 
      win.dispose(); 
     } 
     }); 
    } 

    public void addBazBtnActionListener(ActionListener listener) { 
     baz.addActionListener(listener); 
    } 

    public JComponent getMainComponent() { 
     return mainPanel; 
    } 

} 

class GamePanel { 
    private static final Dimension MAIN_SIZE = new Dimension(400, 200); 
    private JPanel mainPanel = new JPanel(); 

    private JButton foo; 
    private JButton bar; 
    private JButton back; 

    public GamePanel() { 
     foo = new JButton("Foo"); 
     bar = new JButton("Bar"); 
     back = new JButton("return to main menu"); 

     mainPanel.add(foo); 
     mainPanel.add(bar); 
     mainPanel.add(back); 
     mainPanel.setPreferredSize(MAIN_SIZE); 
    } 

    public JComponent getMainComponent() { 
     return mainPanel; 
    } 

    public void addBackBtnActionListener(ActionListener listener) { 
     back.addActionListener(listener); 
    } 

} 
3

Dal momento che ho dovuto testarlo da solo se è davvero così facile da implementare, ho costruito questo semplice esempio. Dimostra una soluzione al tuo problema. Leggermente ispirato alla risposta di @jzd (+1 per quello).

import java.awt.Color; 
import java.awt.HeadlessException; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class FocusChangeTwoFrames 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createGUI(); 
      } 
     }); 
    } 

    private static void createGUI() throws HeadlessException 
    { 
     final JFrame f2 = new JFrame(); 
     f2.getContentPane().setBackground(Color.GREEN);  
     final JFrame f1 = new JFrame();  
     f1.getContentPane().setBackground(Color.RED); 
     f1.setSize(400, 300); 
     f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f1.setVisible(true); 
     MouseListener ml = new MouseAdapter() 
     { 
      @Override 
      public void mousePressed(MouseEvent e) 
      { 
       if(f1.hasFocus()) 
        f2.requestFocus(); 
       else 
        f1.requestFocus(); 
      } 
     }; 
     f1.addMouseListener(ml); 
     f2.setSize(400, 300); 
     f2.setLocation(200, 150); 
     f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f2.setVisible(true); 
     f2.addMouseListener(ml); 
    } 
} 

Divertiti, Boro.