gpt4 book ai didi

java - 如何多次创建新的和处置jcomponents?

转载 作者:行者123 更新时间:2023-12-01 09:05:30 25 4
gpt4 key购买 nike

(抱歉,如果这个问题没有正确完成,我是新人。但至少在问自己的问题之前我做了很多研究)

你好。我正在用 Java 编写一个二十一点游戏,它变得相当庞大。 我的问题是如何处理swing组件的多个实例,我想你可以这样调用它。我不知道是否要在类级别或特定方法中创建组件(例如 jpanels 和 jbuttons)。

如果我在相应的方法中创建它们,那么我的操作监听器将看不到它们,但如果我将它们创建为类级别,那么当我调用 dispose() 时它们会被删除。

 class BlackjackGame extends JFrame implements ActionListener{

public void mainMenu(){

JPanel menuPane = new JPanel(new GridBagLayout()); //Init of main menu
GridBagConstraints c = new GridBagConstraints();
menuPane.setBackground(new Color(125,0,0));
menuPane.setBounds(620,220,175,250);

JLabel menuTitle = new JLabel("Welcome to Blackjack!");//Main menu-content
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,20,0);
menuPane.add(menuTitle, c);

JButton playButton = new JButton("Play!");
playButton.addActionListener(this);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
c.ipadx = 25;
c.ipady = 25;
c.insets = new Insets(0,0,0,0);
menuPane.add(playButton, c);

JButton exitButton = new JButton("Exit!");
exitButton.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
menuPane.add(exitButton, c);

JButton rulesButton = new JButton("Set rules.");
rulesButton.addActionListener(this);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
menuPane.add(rulesButton, c);

this.add(menuPane,0);
}

//This is where I get problems
public void actionPerformed (ActionEvent event){
if(event.getSource() == playButton){
//I want the menuPane to disappear, and a transition into the game.
menuPane.dispose();
//Call method for the rest of the game.

}else if(event .getSource() etcetera etcetera){
etcetera etcetera
}
}
}

以这种方式完成后, Action 监听器无法找到我的组件,例如 playButton 或 menuPane。但如果我将它们作为类级别对象引入:

class BlackjackGame extends JFrame implements ActionListener{

JPanel menuPane = new JPanel(new GridBagLayout());
JLabel menuTitle = new JLabel("Welcome to Blackjack!");
JButton playButton = new JButton("Play!");
JButton exitButton = new JButton("Exit!");
JButton rulesButton = new JButton("Set rules.");

public void mainMenu(){
//Rest of code
}

public void actionPerformed(ActionEvent event){
menuPane.dispose();
//Rest of code
}
}

...然后当我调用 menuPane.dispose() 时,当我想再次调用 mainMenu() 时如何取回它?如果我想返回主菜单,那么我需要创建一个新的 menuPane 实例以及所有按钮,但由于它们是类级别并且已经被处理,所以我不能。

请帮助我,谢谢!

PS。如果有帮助的话,我可以发布我的完整代码,因为它是 atm。

编辑:丹的答案已被接受,因为它确实是正确的答案,并且它非常适合我的特定程序。谢谢你,圣诞快乐!

最佳答案

首先,除非我误解了你的代码,menuPane.dispose()不应该像 JPanel 一样工作没有名为 dispose() 的函数

如果您想使用相同的 menuPane 做您想做的事情的最佳方式对于菜单。而不是menuPane.dispose();使用remove(menuPane);然后add(yourOtherPanel);

工作示例

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class BlackjackGame extends JFrame implements ActionListener {
private JPanel menuPane;
private JLabel menuTitle;
private JButton playButton;
private JButton exitButton;
private JButton rulesButton;

private JPanel otherPane;
private JLabel otherTitle;
private JButton otherButton;

public BlackjackGame() {
mainMenu();
otherPanel();
setSize(400, 400);
setVisible(true);
}

private void mainMenu() {

menuPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
menuPane.setBackground(new Color(125,0,0));
menuPane.setBounds(620,220,175,250);

menuTitle = new JLabel("Welcome to Blackjack!");
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,20,0);
menuPane.add(menuTitle, c);

playButton = new JButton("Play!");
playButton.addActionListener(this);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
c.ipadx = 25;
c.ipady = 25;
c.insets = new Insets(0,0,0,0);
menuPane.add(playButton, c);

exitButton = new JButton("Exit!");
exitButton.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
menuPane.add(exitButton, c);

rulesButton = new JButton("Set rules.");
rulesButton.addActionListener(this);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
menuPane.add(rulesButton, c);

add(menuPane);
}

private void otherPanel() {
otherPane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
otherPane.setBackground(new Color(125,0,0));
otherPane.setBounds(620,220,175,250);

otherTitle = new JLabel("Welcome to Second Pane!");
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,20,0);
otherPane.add(otherTitle, c);

otherButton = new JButton("Go Back!");
otherButton.addActionListener(this);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
c.ipadx = 25;
c.ipady = 25;
c.insets = new Insets(0,0,0,0);
otherPane.add(otherButton, c);
}

public void actionPerformed (ActionEvent event) {
if(event.getSource() == playButton) {
remove(menuPane);
add(otherPane);
validate();
repaint();

} else if(event.getSource() == otherButton) {
remove(otherPane);
add(menuPane);
validate();
repaint();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(() -> new BlackjackGame());
}
}

编辑评论

所以java方法 remove() 仅从容器中删除对象,在本例中为 JFrame 。此方法不会影响其移动的对象,因此可以稍后重用该对象。因此,为什么在上面的代码中我只能使用 remove()add()无需重新声明或重新制作的方法menuPaneotherPane .

至于为什么我这样声明对象

`private JPanel menuPane;`

然后像这样初始化

menuPane = new JPanel(new GridBagLayout());

这是因为我想要ActionListener能够在不立即初始化对象的情况下查看它们。专线private JPanel menuPane;使对象成为全局变量,并且行 menuPane = new JPanel(new GridBagLayout());使它成为我将要使用的东西。这样做而不是 JPanel menuPane = new JPanel(new GridBagLayout());也意味着我可以在多个方法中重用同一个变量。例如

private JPanel panel;

private void createPanelOne() {
panel = new JPanel(new FlowLayout());
...
add(panel);
}

private void createPanelTwo() {
panel = new JPanel(new GridBagLayout());
...
add(panel);
}

完成此操作后,您将有两个 JPanel在你的 JFrame 中它们会有所不同,但您只需要使用一个 JPanel

这是声明它的另一种方式,它创建一个局部变量。局部变量对于您正在使用的方法之外的其他方法不可见,除非您传递它们。

JPanel panel = new JPanel();

我不会说这是一个骗局。我认为有时最好在一个方法中使用其他方法不可见的局部变量。

最后,要将局部变量传递给其他方法,您可以在您创建的方法中使用参数。例如

public void setSomeValue(int val) {
someValue = val;
}

关于java - 如何多次创建新的和处置jcomponents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41318798/

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