如何使用JButton关闭JFrame并无限地打开同一帧的新帧

时间:2012-09-30 03:06:48

标签: jframe jpanel jbutton

我正在用Java制作我的课程游戏,我想要它,这样当游戏结束时,这就是#34;开始新游戏"按钮关闭当前窗口并创建一个新窗口,这样我就可以无限制地玩游戏。问题是,它打开了一个新窗口,但它并没有关闭旧窗口。我无法访问内部类中的静态变量,因此无法使用.setVisible(false);.有任何想法吗?我对这些东西很陌生,我真的很想学。谢谢!

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class GameGUI extends JFrame
{
    private Container c, d;
    private JTextArea terminal;
    private JScrollPane scroll;
    private JTextField name11, str11, name22, str22;
    private JButton start, atk, swp, hl, www, reset;
    private Panel top, ava1, ava2, move;
    private GameMaster gm;
    private JLabel qqq;
    private int s1, s2;
    private String n1, n2;

    public GameGUI()
    {
        c = this.getContentPane();
        c.setLayout( new BorderLayout() );

        ava1 = new Panel();
        ava1.setLayout( new GridLayout ( 1, 4, 5, 0 ) );
        JLabel name1 = new JLabel( "Avatar 1 Name: ", JLabel.CENTER );
        name11 = new JTextField( 20 );
        JLabel str1 = new JLabel( "Avatar 1 Strength: ", JLabel.CENTER );
        str11 = new JTextField();
        ava1.add( name1 );
        ava1.add( name11 );
        ava1.add( str1 );
        ava1.add( str11 );

        ava2 = new Panel();
        ava2.setLayout( new GridLayout ( 1, 4, 5, 0 ) );
        JLabel name2 = new JLabel( "Avatar 2 Name: ", JLabel.CENTER );
        name22 = new JTextField( 20 );
        JLabel str2 = new JLabel( "Avatar 2 Strength: ", JLabel.CENTER );
        str22 = new JTextField();
        ava2.add( name2 );
        ava2.add( name22 );
        ava2.add( str2 );
        ava2.add( str22 );

        top = new Panel();
        top.setLayout( new GridLayout( 3, 1 ) );
        top.add( ava1 );
        top.add( ava2 );

        class reset implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                JFrame wwe = new GameGUI();
                wwe.setLocation( 400, 230 );
                wwe.setSize( 640, 120 );
                wwe.setResizable( true );
                wwe.setVisible( true );
                wwe.setDefaultCloseOperation( EXIT_ON_CLOSE );
                c.remove( reset );
            }
        }

        class action implements ActionListener
        {
            String input;
            public action( String s )
            {
                input = s;
            }

            public void actionPerformed( ActionEvent ae )
            {
                boolean win = false;
                if ( !gm.hasWinner() )
                {
                    terminal.append( gm.play( input ) );
                    terminal.setCaretPosition( terminal.getDocument().getLength() );
                }
                else
                {
                    terminal.append( gm.printGameReport() );
                    c.remove( move );
                    reset = new JButton( "Start a new battle" );
                    reset.addActionListener( new reset() );
                    setSize( 650, 480 );
                    reset.setVisible( true );
                    c.add( reset, "North" );
                }
            }
        }

        class game implements ActionListener
        {
            public void actionPerformed( ActionEvent ae )
            {
                boolean badInput = false;
                n1 = name11.getText();
                n2 = name22.getText();
                if ( n1.equals( "" ) || n2.equals( "" ))
                {
                    JOptionPane.showMessageDialog( null, "Fields lack input!" );
                    badInput = true;
                }
                try
                {
                    s1 = Integer.parseInt( str11.getText());
                    s2 = Integer.parseInt( str22.getText());
                }
                catch( NumberFormatException e )
                {
                    JOptionPane.showMessageDialog( null, "Strength should be an     integer!" );
                    badInput = true;
                }

                if(!badInput)
                {
                    Avatar x = new Avatar ( n1, s1 );
                    Avatar y = new Avatar ( n2, s2 );
                    gm = new GameMaster( x, y );

                    terminal = new JTextArea();
                    terminal.append("....-----======LET THE BATTLE BEGIN! ======----    -....\n\n" );
                    terminal.setEditable( false );
                    c.setVisible( false );
                    c.remove(top);
                    c.setVisible( true );

                    atk = new JButton( "Attack" );
                    atk.addActionListener( new action( "attack" ) );
                    swp = new JButton( "Swap" );
                    swp.addActionListener( new action( "swap" ) );
                    hl = new JButton( "Heal" );
                    hl.addActionListener( new action( "heal" ) );

                    move = new Panel();
                    move.setLayout( new GridLayout( 3, 1 ) );
                    move.add( atk );
                    move.add( swp );
                    move.add( hl );

                    c.add( move, "West" );

                    scroll = new JScrollPane( terminal );
                    c.add( scroll, "Center" );
                    setLocation( 400, 120 );
                    setSize( 640, 480 );
                    do
                    {
                        terminal.append ( gm.equip() );
                    }while ( !x.beltIsFull() || !y.beltIsFull() );
                }
            }
        }

        start = new JButton( "Commence Battle!" );
        start.addActionListener( new game() );
        top.add( start );
        c.add( top, "North" );
    }
}

和我的主要方法:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class GameApp
{
    public static void main ( String args[] )
    {
        JFrame f = new GameGUI();
        f.setLocation( 400, 230 );
        f.setSize( 640, 120 );
        f.setResizable( true );
        f.setVisible( true );`enter code here`
    }
}

0 个答案:

没有答案