gpt4 book ai didi

java - 从 JFrame 中删除 JPanel

转载 作者:行者123 更新时间:2023-12-02 00:22:31 26 4
gpt4 key购买 nike

我正在尝试删除 JPanel,而不是隐藏它,但我找不到任何有效的东西。

这是面板中的代码,按下按钮时需要将其自身删除:

    play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
//need to remove this panel on this line
frame.ThreeD(); // adds a new panel
}
});

已更新

这是完整的代码:

package ThreeD;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;

import Run.Frame;

public class Launcher extends JPanel{
private JButton play, options, help, mainMenu;
private Rectangle rplay, roptions, rhelp, rmainMenu;

private int buttonWidthLocation, buttonWidth, buttonHeight;
private int width = 1280;

public Launcher() {
this.setLayout(null);

drawButtons();
}

private void drawButtons() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}

play = new JButton("Play");
options = new JButton("Options");
help = new JButton("Help");
mainMenu = new JButton("Main Menu");

buttonWidthLocation = (width / 2) - (buttonWidth / 2);
buttonWidth = 80;
buttonHeight = 40;

rplay = new Rectangle(buttonWidthLocation, 150, buttonWidth, buttonHeight);
roptions = new Rectangle(buttonWidthLocation, 300, buttonWidth, buttonHeight);
rhelp = new Rectangle(buttonWidthLocation, 450, buttonWidth, buttonHeight);
rmainMenu = new Rectangle(buttonWidthLocation, 600, buttonWidth, buttonHeight);

play.setBounds(rplay);
options.setBounds(roptions);
help.setBounds(rhelp);
mainMenu.setBounds(rmainMenu);

add(play);
add(options);
add(help);
add(mainMenu);

play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame frame = new Frame();
//need to remove this panel here
frame.ThreeD();
}
});
options.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("options");
}
});
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("help");
}
});
mainMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("mainMenu");
}
});
}
}

这是我的 Frame 类:

package Run;

import javax.swing.*;

import ThreeD.Display;
import ThreeD.Launcher;
import TowerDefence.Window;


import java.awt.*;
import java.awt.image.BufferedImage;

public class Frame extends JFrame{

public static String title = "Game";

/*public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}*/

/*public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}*/

//public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight());
public static Dimension size = new Dimension(1280, 774);


public static void main(String args[]) {
Frame frame = new Frame();

System.out.println("Width of the Frame Size is "+size.width+" pixels");
System.out.println("Height of the Frame Size is "+size.height+" pixels");
}

public Frame() {
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ThreeDLauncher();
}

public void ThreeDLauncher() {
Launcher launcher = new Launcher();
add(launcher);

setVisible(true);
}

public void TowerDefence() {
setLayout(new GridLayout(1, 1, 0, 0));

Window window = new Window(this);
add(window);

setVisible(true);
}

public void ThreeD() {
BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");

getContentPane().setCursor(blank);

Display display = new Display();
add(display);

setVisible(true);

display.start();
}

}

最佳答案

基本上 - 您正在创建 Frame 的新实例行中:

Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)

Frame 的新实例不可见,并且您尝试删除 Launcher来自不可见新 Frame 。但这是错误的 - 你应该删除 Launcher来自Frame您之前在 main 中创建的函数(即: Launcher 组件的父组件)。

这是一个例子:

public class TestFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestFrame frame = new TestFrame();
frame.getContentPane().add(new MyPanel(frame));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});

}
}

MyPanel类:

public class MyPanel extends JPanel {
public MyPanel(final TestFrame frame) {
JButton b = new JButton("Play");
add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Container pane = frame.getContentPane();
pane.remove(MyPanel.this);
JPanel otherPanel = new JPanel();
otherPanel.add(new JLabel("OtherPanel"));
pane.add(otherPanel);
pane.revalidate();
}
});
}
}

在您的示例中,您应该添加对 Frame 的引用在你的Launcher构造函数:

public Launcher(Frame frame) {
this.frame = frame;
...

初始化Launcher :

public void ThreeDLauncher() {     
Launcher launcher = new Launcher(this);

并使用:

    play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//need to remove this panel here
frame.getContentPane().remove(Launcher.this);
frame.ThreeD();
}
});

关于java - 从 JFrame 中删除 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672160/

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