GUI多帧切换



我在写一个21点游戏的程序。这是一个作业,我们不使用gui的,但我做额外的学分,我已经创建了两个框架,他们正在工作。在第二帧中,我希望能够在按下按钮时切换回第一帧。我该怎么做呢?

的第一个窗口 .............

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);
    }
  }

第二窗口…

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.
    }
  }
}

第二帧需要引用第一帧,以便它可以将焦点设置回第一帧。

你的类也扩展了JFrame,但它们也在它们的构造函数中创建了其他框架。

几点建议:

你正在添加组件到一个JPanel,使用FlowLayout,但正在使用BorderLayout常量时这样做,你不应该这样做,因为它没有意义:

  add(play, BorderLayout.CENTER);

相反,如果使用FlowLayout,只需添加没有这些常量的组件。

另外,与其交换JFrame,不如考虑使用CardLayout并在单个JFrame中交换视图。例如:

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);
   }
}

因为我必须自己测试它是否真的很容易实现,所以我构建了这个简单的示例。它展示了解决问题的方法。受@jzd的回答启发(+1)。

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);
    }
}

享受,米堡。

相关内容

  • 没有找到相关文章

最新更新