如何完全摆脱JPanel及其中的所有内容?

时间:2015-07-22 23:51:09

标签: java swing jpanel

如何在跑步时完全摆脱JPanel及其中的所有内容?

package textgame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class EscapeFromPrison extends JFrame implements ActionListener{
    JButton startGameButton;
    JButton creditsButton;

    JButton choice1;
    Button choice2;

    JLabel mainTitleLabel;
    JLabel question;
    JLabel space;
    JLabel credits;

    JPanel titleScreen;

    public EscapeFromPrison(){
        super("Escape From Prison");
        setLookAndFeel();
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainTitleLabel = new JLabel("<html>Escape From Prison</html>");

        startGameButton = new JButton("<html>START<html>");
        startGameButton.setActionCommand("StartGameButton");

        creditsButton = new JButton("<html>CREDITS</html>");
        creditsButton.addActionListener(this);
        creditsButton.setActionCommand("CreditsButton");

        question = new JLabel("");
        space = new JLabel("");
        credits = new JLabel("");

        choice1 = new JButton("");
        choice2 = new JButton("");

        JPanel titleScreen = new JPanel();
        BoxLayout titleScreenLayout = new BoxLayout(titleScreen,      BoxLayout.Y_AXIS);
        titleScreen.setLayout(titleScreenLayout);
        titleScreen.add(mainTitleLabel);
        titleScreen.add(startGameButton);
        titleScreen.add(creditsButton);
        add(titleScreen);

        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent buttonClick){
        String event = buttonClick.getActionCommand();

在这个if语句中,我想摆脱mainTitleLabelstartGameButtoncreditsButton。所以问题可能在左上角位置,因为现在它们目前是不可见的,问题是在右上角位置。我正在使用网格布局。

            if(event.equals("StartGameButton")){
            GridLayout grid = new GridLayout(2,2);
            setLayout(grid);

            question.setText("<html>text</html>");
            choice1.setActionCommand("");
            choice1.setText("");
            choice2.setActionCommand("");
            choice2.setText("");

            mainTitleLabel.setVisible(false);
            startGameButton.setVisible(false);
            creditsButton.setVisible(false);

            add(question);
            add(choice1);
            add(choice2);

            setVisible(true);

        }
        if(event.equals("CreditsButton")){
            FlowLayout flo = new FlowLayout();
            setLayout(flo);

            credits.setText("<html></html>");

            mainTitleLabel.setVisible(false);
            startGameButton.setVisible(false);
            creditsButton.setVisible(false);

            add(credits);

            setVisible(true);
        }
    }

    private void setLookAndFeel(){
        try{
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        }catch(Exception exc){
            //ignore error
        }
    }

    public static void main(String[] arguments){
        EscapeFromPrison frame = new EscapeFromPrison();
    }
}

1 个答案:

答案 0 :(得分:3)

当前代码的基本问题是startGameButton从未注册到ActionListener,因此它永远不会执行任何操作。

最大的问题是,当您添加更多内容时,您的代码将变得非常复杂。一个更好的解决方案是将每个视图中的每个视图分开,然后您可以根据需要独立管理和切换。

这是Model-View-Controller概念的开始,您可以将自己的功能分成自包含的域,这些域可以通过观察者模式(作为一个想法)进行通信。

通过此功能,您可以使用CardLayout更轻松地在视图之间切换。

基本示例......

这只是实现此目的的许多可行方法之一

首先,我们需要定义某种&#34;控制器&#34;它可用于控制可见的内容。我们使用接口作为主要合同,因为它们限制了实现的暴露并定义了我们期望在类之间维护的确切合同,这通常被称为&#34;程序到接口&#34;,请参阅{{3} }和Smarter Java development了解更多详情

public interface CardGameController {
    public void showMainMenu();
    public void showQuestion();
    public void showCredits();
}

现在,因为我想使用CardLayout作为控制视图的基础机制,我需要CardGameController的实现,它可以做到这一点......

public class DefaultCardGameController implements CardGameController {

    public static final String MAIN_MENU_PANE = "mainMenuPane";
    public static final String CREDITS_PANE = "creditsPane";
    public static final String QUESTION_PANE = "questionPane";

    private Container parent;
    private CardLayout cardLayout;

    public DefaultCardGameController(Container parent, CardLayout cardLayout) {
        this.parent = parent;
        this.cardLayout = cardLayout;
    }

    public Container getParent() {
        return parent;
    }

    public CardLayout getCardLayout() {
        return cardLayout;
    }

    protected void show(String name) {
        getCardLayout().show(getParent(), name);
    }

    @Override
    public void showMainMenu() {
        show(MAIN_MENU_PANE);
    }

    @Override
    public void showCredits() {
        show(CREDITS_PANE);
    }

    @Override
    public void showQuestion() {
        show(QUESTION_PANE);
    }

}

有关详细信息,请查看Code to Interface

现在,我们需要我们想要管理的视图......

public class MenuPane extends JPanel {

    private JButton startGameButton;
    private JButton creditsButton;
    private JLabel mainTitleLabel;

    private CardGameController controller;

    public MenuPane(CardGameController controller) {
        this.controller = controller;
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        mainTitleLabel = new JLabel("<html>Escape From Prison</html>");

        startGameButton = new JButton("<html>START<html>");
        startGameButton.setActionCommand("StartGameButton");
        startGameButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.showQuestion();
            }
        });

        creditsButton = new JButton("<html>CREDITS</html>");
        creditsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.showCredits();
            }
        });
        creditsButton.setActionCommand("CreditsButton");

        add(mainTitleLabel, gbc);
        add(startGameButton, gbc);
        add(creditsButton, gbc);
    }

}

public class QuestionPane extends JPanel {

    private JButton choice1;
    private JButton choice2;

    private JLabel question;

    private CardGameController controller;

    public QuestionPane(CardGameController controller) {
        this.controller = controller;
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;

        question = new JLabel("The meaning of life, the universe and everything!?");
        choice1 = new JButton("42");
        choice2 = new JButton("46");

        add(question, gbc);

        JPanel panel = new JPanel();
        panel.add(choice1);
        panel.add(choice2);

        gbc.gridy++;
        add(panel, gbc);

        // Have some mechanism to control the questions
    }

}

public class CreditsPane extends JPanel {

    private JLabel credits;

    private CardGameController controller;

    public CreditsPane(CardGameController controller) {
        this.controller = controller;
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        credits = new JLabel("Happy Bunnies");
        add(credits);
    }

}

最后,&#34;主人&#34;将所有这些结合在一起的观点......

public class EscapeFromPrison extends JPanel {

    private MenuPane menuPane;
    private QuestionPane questionPane;
    private CreditsPane creditsPane;

    public EscapeFromPrison() {

        CardLayout cardLayout = new CardLayout();
        setLayout(cardLayout);
        DefaultCardGameController controller = new DefaultCardGameController(this, cardLayout);

        menuPane = new MenuPane(controller);
        questionPane = new QuestionPane(controller);
        creditsPane = new CreditsPane(controller);

        add(menuPane, DefaultCardGameController.MAIN_MENU_PANE);
        add(questionPane, DefaultCardGameController.QUESTION_PANE);
        add(creditsPane, DefaultCardGameController.CREDITS_PANE);

        controller.showMainMenu();
    }

    private static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                            "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            //ignore error
        }
    }

    public static void main(String[] arguments) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                setLookAndFeel();
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new EscapeFromPrison());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

现在,这是一个非常有限的例子,它采用了你已经完成的工作并演示了一些基本原则。

因为你会问一个以上的问题,你需要一些方法来更有效地管理问题(和答案),这最好通过某种包含所有问题的模型来完成,可能的答案并且用户回答。这样,你只需要一个问题视图,但可以使用问题模型中的信息来重新配置自己以显示每个新问题

How to Use CardLayout