gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:02:35 26 4
gpt4 key购买 nike

如何在运行时完全摆脱 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();
}
}

最佳答案

您当前代码的基本问题是 startGameButton 从未注册到 ActionListener,因此它永远不会做任何事情。

最大的问题是,随着您添加更多内容,您的代码将变得难以管理。更好的解决方案是将每个 View 分离到它自己的类中,然后您可以独立管理并根据需要切换进出。

这是Model-View-Controller概念的开始,在这里你将你的功能分成独立的域,这些域可以通过观察者模式(作为一个想法)之类的东西进行通信。

有了它,您可以使用 CardLayout 来更轻松地在 View 之间切换。

基本示例...

这只是实现这一目标的众多可能方法之一

首先,我们需要定义某种“ Controller ”,它可以用来控制什么是可见的。我们使用接口(interface)作为主要契约,因为它们限制了实现的公开,并定义了我们希望在类之间维护的确切契约,这通常被称为“接口(interface)编程”,参见 Smarter Java developmentCode to Interface了解更多详情

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

现在,因为我想使用 CardLayout 作为控制 View 的底层机制,所以我需要一个可以执行此操作的 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);
}

}

看看How to Use CardLayout了解更多详情

现在,我们需要要管理的 View ...

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);
}

}

最后,将所有内容组合在一起的“主” View ...

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);
}
});
}
}

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

因为你会问不止一个问题,你需要一些方法来更有效地管理问题(和答案),最好通过某种包含所有问题、可能的答案和用户回答。这样,您将只需要一个问题 View ,但它可以使用问题模型中的信息重新配置自身以显示每个新问题

对于 example

关于java - 如何完全摆脱 JPanel 及其中的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com