JFrame.dispose();什么也没做

时间:2019-08-04 20:01:55

标签: java swing dispose

我已经尝试了在这里和论坛上都能找到的所有解决方案,但是无论我做什么,我都无法关闭JFrame,我也不知道为什么。我需要处理JFrame并通过创建WarioWareGUI对象来启动另一个JFrame。

现在,我似乎能做的最好的事情就是阻止球移动,只将旧的JFrame留在后台,但这还不够好。我需要风JFrame关闭

我尝试了无限处理,我尝试将JFrame设置为公共静态并将其初始化为BallGame的对象,以便可以在任何地方进行处理。我尝试过在创建后立即将其丢弃。似乎没有什么可以摆脱它的。

我什至尝试将球速设置为0,并使帧不可见,但为wind.setVisible(false);。也不做任何事情。

package GameProject;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public final class BallGame extends JComponent implements ActionListener, MouseMotionListener, KeyListener {

    private int ballx = 150;
    private int bally = 30;
    private int paddlex = 0;
    private static int ballySpeed = 7;
    private static int ballxSpeed = 5;
    private int score = 0;
    private int round = 1;
    private int bounces = 0;
    private static int delay = 20;
    public static boolean gameOver = false;
    public static boolean gameOverFlag = false;
    public boolean started;

    public static void main(int round) {

        //only true the first time the game is run
        if ((gameOver == false) && (gameOverFlag == false)) {
            gameMake(round);
        }

        //only true after the game has resulted in a loss
        if ((gameOver == true) && (gameOverFlag==true)) {
            gameOver = false;
            //gameMake(round);
            WarioWareGUI gui = new WarioWareGUI();
        }

        //only true after the game has resulted in a loss and
        //returned to main menu
        if ((gameOver == false) && (gameOverFlag==true)) {

        }

    }

    public static void gameMake(int round) {

        JFrame wind = new JFrame("RedBall/GamePinfo");
        BallGame g = new BallGame();
        wind.add(g);
        wind.pack();
        wind.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        wind.setLocationRelativeTo(null);
        wind.setVisible(true);
        wind.addMouseMotionListener(g);

        wind.dispose();

        //speed up the timer each round
        Timer tt = new Timer(delay, g);
        tt.start();

    }

    public void newball(int ballx, int bally, int ballxspeed, int ballyspeed) {

        ballx = 150;
        bally = 30;
        ballxspeed = 5;
        ballyspeed = 7;

        JOptionPane.showMessageDialog(null, "new ball !");

        return;
    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(800, 600);
    }

    @Override
    protected void paintComponent(Graphics g) {

        //draw the sky
        g.setColor(Color.cyan);
        g.fillRect(0, 0, 800, 600);

        g.setColor(Color.GREEN);
        g.fillRect(0, 550, 800, 100);

        //draw the paddel
        g.setColor(Color.black);
        g.fillRect(paddlex, 500, 100, 20);

        //draw the ball
        g.setColor(Color.RED);
        g.fillOval(ballx, bally, 30, 30);

        g.setColor(Color.white);
        g.setFont(new Font("Arial", 8, 50));
        g.drawString(String.valueOf(score), 30, 80);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        //Manages ball movement
        ballx = ballx + ballxSpeed;
        bally = bally + ballySpeed;

        if (gameOverFlag) {
            round = 1;
            BallGame.main(round);
        }

        else if (gameOver) {

            gameOverFlag = true;

            //displays the dialog box indicating victory
            JOptionPane.showMessageDialog(this, "You lost on round"
                    + round + "!");

            ballx = 150;
            bally = 30;


        }

        else if (bounces==10) {

            //reset score
            bounces = 0;

            round++;

            JOptionPane.showMessageDialog(this,"You Win! You move on to"
                    + " round " + round + "!");

        }


        // Sets ball speed upward if it hits the paddle
        else if (ballx >= paddlex && ballx <= paddlex + 100 && bally >= 475) {

            ballySpeed = -7 - (round*2);

            //monitors the number of times the ball is successfully hit
            score++;
            bounces++;

        }

        // Handles loss condition
        else if (bally >= 500 ) {

            gameOver = true;

        }

        // Sets ball movement down if it hits the ceiling
        else if (bally <= 0) {

            ballySpeed = 7 + (round*2);

        }

        // Sets ball
        else if (ballx >= 775) {

            ballxSpeed = -5 - (round*2);

        }

        // Window left
        else if (ballx <= 0) {

            ballxSpeed = 5 + (round*2);

        }

        //**********************************************************************

        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {

        //places paddle in middle of mouse
        paddlex = e.getX() - 50;
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

我希望调用wind.dispose来关闭GUI,但是无论我放在哪里或如何声明JFrame,它都不会关闭。我也不能使它不可见。

现在的代码就是我放弃时的代码,我当然不打算立即处置它,但是一旦我能成功处置它,就可以将它付诸实践。在正确的时间做到这一点。

0 个答案:

没有答案
相关问题