简体   繁体   中英

How to call a class from another

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.

I have looked at other questions and none of them really apply to my situation.

My menuScreen class:

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM