gpt4 book ai didi

Java - 如何使图像消失

转载 作者:行者123 更新时间:2023-12-02 00:13:37 25 4
gpt4 key购买 nike

嗨,我想让我的 JPanel 消失,所以我写了这些代码

removeAll();
updateUI();
revalidate();

这只会让 JComponent 和 JButton 消失。我想让用paint方法显示的图像也消失。如果我执行 setVisible(false),那么我无法在其后面添加另一个 JPanel。

这是我的课:

package screens;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class menuScreen extends JPanel implements MouseListener{
private static final long serialVersionUID = 1L;

//-------------VARIABLES---------------//
Image wallpaper = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/wallpaper.jpg"));
Image title_text = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/title-text.png"));
ImageIcon startGameimg = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/startGame.png")));
ImageIcon optionsimg = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/options.png")));
//JButton start = new JButton(basketball);
JLabel options = new JLabel(optionsimg);
JLabel startGame = new JLabel(startGameimg);
gameScreen gS = new gameScreen();
CardLayout scenechange = new CardLayout();
JPanel scenechange1 = new JPanel (scenechange);

//-------------PAINT FUNCTION----------//
public void paintComponent(Graphics g){
g.drawImage(wallpaper,0,0,this);
g.drawImage(title_text,0,0,this);
//g.drawImage(basketball1,110,180,this);

}

//-------------CONSTRUCTOR-------------//
public menuScreen(){

scenechange.addLayoutComponent(this,"menuScreen");
scenechange.addLayoutComponent(gS,"gameScreen");
//scenechange.show(this,"menuScreen");

this.setLayout(null);
this.add(options);
this.add(startGame);
startGame.setBounds(110,180,110,110);
options.setBounds(110,300,110,110);
startGame.addMouseListener(this);
options.addMouseListener(this);


}


public void mouseClicked(MouseEvent e) {
if(e.getSource() == (startGame)){

removeAll();

revalidate();
add(gS);
}

if(e.getSource() == (options)){
setVisible(false);
}


}


public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}


public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}


public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}


public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}


}//END OF CLASS startingScreen

提前致谢。

最佳答案

首先,不要调用 updateUI,它与外观相关,而不是(直接)更新组件。

如果您在面板中提供了自定义绘制例程,那么您需要阻止它绘制图像(而不阻止它绘制自己的内容)。 removeXxx 将删除您之前添加到容器中的子组件。

多一点代码会很有用

更新

首先,您绘制的图像不是容器的组件,它们已被“标记”,您需要某种方式告诉组件不要绘制图像

public void paintComponent(Graphics g){
super.paintComponent(g); // this is super important
if (paintImages){ // you need to define and set this flag
g.drawImage(wallpaper,0,0,this);
g.drawImage(title_text,0,0,this);
}
}

现在,这将停止绘制图像。

但是,如果您不再想要使用该组件(即,想要将其从屏幕上删除,以便可以在屏幕上将新组件放置在其位置),则需要从其父组件中删除该组件,这是 Code-Guru 所建议的(所以我不会窃取他的答案;))

更新

好吧,您有一个想法的核心,但要么不太知道如何实现它,要么决定放弃它。

基本上,从代码的外观来看,您要么试图实现,要么已经实现了 CardLayout,不幸的是,您的想法有点错误。

使用CardLayout,您需要“ Controller ”,一个负责切换屏幕的组件...

public class ScreenController extends JPanel {

private static final long serialVersionUID = 1L;
//-------------VARIABLES---------------//
MenuScreen ms = new MenuScreen();
GameScreen gs = new GameScreen();
CardLayout sceneChange;

//-------------CONSTRUCTOR-------------//
public ScreenController() {

sceneChange = new CardLayout();

this.setLayout(sceneChange);
add(ms, "menuScreen");
add(gs, "gameScreen");

sceneChange.show(this, "menuScreen");

ms.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("startgame")) {
sceneChange.show(ScreenController.this, "gameScreen");
}
}
});
}

}//END OF CLASS startingScreen

然后你就有了菜单和游戏屏幕......

public class MenuScreen extends JPanel implements MouseListener {

private static final long serialVersionUID = 1L;
//-------------VARIABLES---------------//
//JButton start = new JButton(basketball);
JLabel options = new JLabel("Options");
JLabel startGame = new JLabel(" >> Start << ");
// gameScreen gS = new gameScreen();
BufferedImage wallpaper;

//-------------PAINT FUNCTION----------//
@Override
public void paintComponent(Graphics g) {
System.out.println("paint");
super.paintComponent(g);
if (wallpaper != null) {
g.drawImage(wallpaper, 0, 0, this);
}
}

//-------------CONSTRUCTOR-------------//
public MenuScreen() {

// Please handle your exceptions better
try {
wallpaper = ImageIO.read(getClass().getResource("/Menu.png"));
setPreferredSize(new Dimension(wallpaper.getWidth(), wallpaper.getHeight()));
} catch (IOException ex) {
ex.printStackTrace();
}

setLayout(new GridBagLayout());

Cursor cusor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
options.setCursor(cusor);
startGame.setCursor(cusor);

Font font = UIManager.getFont("Label.font").deriveFont(Font.BOLD, 48);
options.setFont(font);
startGame.setFont(font);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;

this.add(options, gbc);
gbc.gridy++;
this.add(startGame, gbc);
startGame.addMouseListener(this);
options.addMouseListener(this);
}

public void mouseClicked(MouseEvent e) {
if (e.getSource() == (startGame)) {
fireActionPerformed("startGame");
}
if (e.getSource() == (options)) {
fireActionPerformed("gameOptions");
}
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}

public void removeActionListener(ActionListener listener) {
listenerList.remove(ActionListener.class, listener);
}

protected void fireActionPerformed(String cmd) {
ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
if (listeners != null && listeners.length > 0) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, cmd);
for (ActionListener listener : listeners) {
listener.actionPerformed(evt);
}
}
}
}

菜单屏幕...

Menu Screen

当你点击开始...游戏屏幕...

enter image description here

这是一个示例。在继续实现之前,请尝试花些时间了解代码中发生的情况。我使用自己的图像,您需要获取自己的图像..

关于Java - 如何使图像消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12291237/

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