GUI多帧切换 [英] GUI multiple frames switch

查看:145
本文介绍了GUI多帧切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为黑杰克游戏编写程序。这是一项我们不会使用gui的任务,但我正在为了额外的功劳而创建它我已经创建了两个框架,它们正在工作。在第二帧我希望能够在按下按钮时切换回第一帧。我该怎么做?

I am writing a program for a black jack game. It is an assignment we are not to use gui's but I am doing it for extra credit I have created two frames ant they are working. On the second frame I want to be able to switch back to the first when a button is pressed. How do I do this?

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

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


推荐答案

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

The second frame needs a reference to the first frame so that it can set the focus back to the first frame.

此外,您的类扩展了JFrame,但它们是还在其构造函数中创建其他框架。

Also your classes extend JFrame but they are also creating other frames in their constructors.

这篇关于GUI多帧切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆