如何参考JFrame [英] How to reference JFrame

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

问题描述

我有一个很可能是简单"的问题,但是我无法弄清楚.我试图引用我当前的JFrame,以便我可以处置它,并创建一个新的JFrame,从而重置"程序,但是我在弄清楚如何引用JFrame时遇到了麻烦,我尝试了,超级,这个, getParent(),但似乎都不起作用.感谢您提供的所有帮助. ^^

I have a problem which is most likely "simple" however I can't figure it out. I am trying to reference my current JFrame so that I can dispose of it, and create a new one, thus "resetting" the program, however I and having trouble figuring out how to reference the JFrame, I have tried, super, this and getParent(), but none of the seem to work. Thanks for any / all help. ^^

这是我的代码:

主类,只需设置Jframe并调用创建所有内容的类:

Main Class, just sets up the Jframe and calls the class that creates everything:

    public static void main(String args[]) {
    JFrame window = new JFrame();
    Director director = new Director(window, args);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLocationRelativeTo(null);
    window.pack();
    window.setVisible(true);
}

}

创建所有东西的类:

    import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class Director extends JFrame implements CollisionListener {

    private BrickWall wall;
    private JLabel gameTitle, gameScore, gameLives;
    private JPanel controlPanel;
    private JButton reset, quit;
    private JRadioButton hard, normal, easy;
    private int score = 6, lives = 5;
    private ButtonGroup difficulty;


    public Director(JFrame window, String[] args) {
        window.getContentPane().add(makeGamePanel(), BorderLayout.CENTER);
        window.getContentPane().add(gameControlPanel(), BorderLayout.NORTH);

    }


    public void collisionDetected(CollisionEvent e) {
        wall.setBrick(e.getRow(), e.getColumn(), null);
    }

    private JComponent makeGamePanel() {
        wall = new BrickWall();
        wall.addCollisionListener(this);
        wall.buildWall(3, 6, 1, wall.getColumns(), Color.GRAY);
        return wall;
    }

    // Reset method I'm trying to dispose of the JFrame in.
    private void reset() {
        JFrame frame = new JFrame();

        frame.getContentPane().add(makeGamePanel(), BorderLayout.CENTER);
        frame.getContentPane().add(gameControlPanel(), BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    private JComponent gameControlPanel() {

        // CONTROL PANEL PANEL!
        controlPanel = new JPanel();

        gameTitle = new JLabel("Brickles");
        gameScore = new JLabel("Score:" + "   " + score);
        gameLives = new JLabel("Lives:" + "   " + lives);
        reset = new JButton("Reset");
        quit = new JButton("Quit");
        hard = new JRadioButton("Hard", false);
        normal = new JRadioButton("Normal", true);
        easy = new JRadioButton("Easy", false);
        difficulty = new ButtonGroup();
        difficulty.add(hard);
        difficulty.add(normal);
        difficulty.add(easy);
        controlPanel.setLayout(new GridLayout(4, 2));
        controlPanel.add(gameTitle);
        controlPanel.add(gameScore);
        controlPanel.add(hard);
        controlPanel.add(gameLives);
        controlPanel.add(normal);
        controlPanel.add(reset);
        controlPanel.add(easy);
        controlPanel.add(quit);

        // Action Listener, where I'm caling the reset method.

        reset.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                reset();
            }
        });
        return controlPanel;
    }

}

推荐答案

您可以使用以下语法从嵌套类中引用外部this":

You can refer to the "outer this" from a nested class with the following syntax:

reset.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         Director.this.reset();
    }
});

这篇关于如何参考JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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