Kiriaevi
Kiriaevi

Reputation: 5

Link two classes

I am trying to combine two classes. I want to create a JFrame with some JLabel with images in the DON class and some buttons. When I press one of the buttons the program calls another class called inizio that sets the visibility of my jLabel to false.

When I call the class inizio with the button I added, the program also creates a new Jframe, leaving me with 2 or more (based on how many times I press the button).

This is because I put inside the the instruction to create a JFrame in the DON class. How can I fix this?

This is my program:

DON CLASS

import javax.swing.*;

public class DON  {
  public JFrame finestra;
  public JPanel imagePanel;
  public JLabel imageLabel;

  public  void rbeg() {

    }

   public DON() {
        //Impostazioni finestra
        finestra = new JFrame("JFRAME EXAMPLE");
        Container con = finestra.getContentPane();
        finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout layout = new GridBagLayout();
        finestra.setResizable(false);
        finestra.setSize(1280, 720);

        ImageIcon ICONA = new ImageIcon(getClass().getClassLoader().getResource("logo.png"));
        finestra.setIconImage(ICONA.getImage());
        con.setBackground(Color.black);






        //Jpanel for images
        GridBagConstraints c = new GridBagConstraints();
        imagePanel = new JPanel();
        imagePanel.setLayout(layout);

        //Impostazioni label e ImageIcon


        ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("menu.png")); 



         imageLabel= new JLabel(icon);
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        layout.setConstraints(imageLabel, c);
        imageLabel.setOpaque(true);

        imagePanel.add(imageLabel);
        //JBUTTONS 
        Insets paddingBottone = new Insets(10,25,10,25);
        //Button Jpanel
        JPanel pannelloBottoni = new JPanel();
        pannelloBottoni.setLayout(layout);

        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(3,3,60,3);

        layout.setConstraints(pannelloBottoni, c);
       //BUTTON 1 ( Start )
        JButton start = new JButton("Start!");
        c.gridx = 0;
        c.gridy = 0;
        layout.setConstraints(start, c);
        start.setMargin(paddingBottone);
        pannelloBottoni.add(start);
        start.addActionListener(e -> new inizio());
        imagePanel.add(pannelloBottoni);
        con.add(imagePanel);
        finestra.setVisible(true);
        }

   public static void main(String args[]) {
      new DON();
   }
}

inizio CLASS

public class inizio {
    public inizio() {
        DON a = new DON();
        a.rbeg();
        a.imageLabel.setVisible(false);

        }
}

Upvotes: 0

Views: 37

Answers (1)

user13571010
user13571010

Reputation:

Instead of crating a new DON object every time you click the button, just pass inizio a handle to the already existing JFrame. Something like this maybe:

public class inizio {
    public inizio(JFrame frame) {
        frame.imageLabel.setVisible(false);
        }
}

And change this line:

start.addActionListener(e -> new inizio(imagePanel));

Upvotes: 1

Related Questions