Call another class (Java)

Asked

Viewed 1,018 times

-1

How do I call a "class" of type jFrame for example from my main class? My jFrame has its own main that gives the true setVisible for it to appear, but I wanted to access this main of it from my main class , to be able to give setVisible by the main of it instead of I create an instance of such and give the setVisible by my class, because it loses frame design quality.

3 answers

1

You have to instantiate the class JFrame and call the method setVisible(boolean b) with the true parameter.

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        JFrame frame= new JFrame("PT Stack Overflow");
        frame.setVisible(true);
    }
}

If your goal is to inherit JFrame the procedure is just that instead of instantiating JFameyou instance your heiress class of JFrame:

import java.awt.*; 
import javax.swing.*;

class MeuFrame extends JFrame
{
   JLabel label;

  // constructor
  MeuFrame ( String titulo )
  {
    super( titulo );                      // invoca o constructor de JFrame
    setSize( 150, 100 );                 

    // Ajusta o comportamento de fechamento do JFrame
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    setLayout( new FlowLayout() );       
    label = new JLabel("PT Stack Overflow");  
    add( label );                        // adiciona o JLabel ao JFrame
  }

} 

public class TestFrame2
{
  public static void main ( String[] args )
  {        
    MeuFrame frame = new MeuFrame("Esse é o meu frame!");
    frame.setVisible( true );            

  }
}

0

Already got, to call the main of another class through its main class is :

Nomedaclasse.main(args);

  • That’s not how you call a window, it’s instantiating it and giving a setVisible.

  • setVisible is built in the main of the window itself

  • It depends on your application. If you’re using more than one Jframe, it’s a bad idea. The most common is Have only one Jframe, and other windows use Jdialog, and when opening Ubwindows, give a setvisible(false) in the frame to hide. That way, you don’t need to instantiate again. Don’t get me wrong, but this seems like a scam.

-1

Well, if instantiating the class can access it, main class:

public static void main(String args[])
     {
        Aluno estudante = new Aluno();//Aluno é outra classe.

Was that your doubt?

  • 1

    No, like, jframe has its own main,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.