I want to call a class from another. I have 2 classes which both are extending JPanels and i want one to disappear and the other to appear.
我想从另一个人那里打电话。我有两个类,它们都在扩展JPanels,我希望一个消失,另一个消失。
I have looked at other questions and none of them really apply to my situation.
我看过其他问题,但没有一个真正适用于我的情况。
My menuScreen class:
我的menuScreen类:
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();
//-------------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(){
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)){
setVisible(false);
}
if(e.getSource() == (options)){
setVisible(false);
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
} //END OF CLASS startingScreen
My gameScreen class which I want to appear:
我想要出现的gameScreen类:
package screens;
import javax.swing.*;
public class gameScreen extends JPanel{
private static final long serialVersionUID = 1L;
}
In the gameScreen class, i havent put anything yet because I don't know how to call it.
在gameScreen类中,我还没有放任何东西,因为我不知道如何调用它。
1 个解决方案
#1
0
Basically the behaviour you wish is redundant and of no help since inheritantly they are of same type and fulfill the same purpose. Well to be simple no. you cannot replace a class like that, what you can do is change the reference/instantiation from the calling class to a new gamescreen class instance otherwise do a repaint from within menuscreen class to hold the state values which you wish your gameclass should hold.
基本上你想要的行为是多余的,没有任何帮助,因为继承它们是相同的类型并且实现相同的目的。好吧,简单不行。你无法替换这样的类,你可以做的是将引用/实例化从调用类更改为新的gamescreen类实例,否则从menuscreen类中重新绘制以保存你希望你的游戏类应该保存的状态值。
#1
0
Basically the behaviour you wish is redundant and of no help since inheritantly they are of same type and fulfill the same purpose. Well to be simple no. you cannot replace a class like that, what you can do is change the reference/instantiation from the calling class to a new gamescreen class instance otherwise do a repaint from within menuscreen class to hold the state values which you wish your gameclass should hold.
基本上你想要的行为是多余的,没有任何帮助,因为继承它们是相同的类型并且实现相同的目的。好吧,简单不行。你无法替换这样的类,你可以做的是将引用/实例化从调用类更改为新的gamescreen类实例,否则从menuscreen类中重新绘制以保存你希望你的游戏类应该保存的状态值。