Help with a JFrame to open another

0

I have a JFrame for a project, I have been asked to pass the window that had to be a secondary window, that is, open from another window that would be the main one.

Here is the code of the secondary window (Which should be able to open from the main window). It is in the same project and the same package.

package neuronaia;

import javax.swing.*;

public class Aprendizaje extends JFrame {

JFrame Ap = new JFrame();
Ap.setLayout(null);
Ap.setSize(350,350);            
Ap.setTitle("Aprendizaje");
Ap.setDefaultCloseOperation(Ap.EXIT_ON_CLOSE);
Ap.setVisible(true);
Ap.setLocationRelativeTo(null);

//Instanciando Cuadros de Texto         
JLabel lx1 = new JLabel("X1");
Ap.add(lx1);
lx1.setBounds(25, 15, 25, 20);

JTextField textx1 = new JTextField();
Ap.add(textx1);
textx1.setBounds(25, 35, 35, 30);

JLabel lx2 = new JLabel("X2");
Ap.add(lx2);
lx2.setBounds(125, 15, 25, 20);

JTextField textx2 = new JTextField();
Ap.add(textx2);
textx2.setBounds(125, 35, 35, 30);

JLabel ly1 = new JLabel("Y1");
Ap.add(ly1);
ly1.setBounds(250, 10, 25, 30);

JTextField texty1 = new JTextField();
Ap.add(texty1);
texty1.setBounds(250, 35, 35, 30);

JTextField textx1a = new JTextField();
Ap.add(textx1a);
textx1a.setBounds(25, 90, 35, 30);

JTextField textx2a = new JTextField();
Ap.add(textx2a);
textx2a.setBounds(125, 90, 35, 30);

JTextField texty1a = new JTextField();
Ap.add(texty1a);
texty1a.setBounds(250, 90, 35, 30);

JTextField textx1b = new JTextField();
Ap.add(textx1b);
textx1b.setBounds(25, 145, 35, 30);

JTextField textx2b = new JTextField();
Ap.add(textx2b);
textx2b.setBounds(125, 145, 35, 30);

JTextField texty1b = new JTextField();
Ap.add(texty1b);
texty1b.setBounds(250, 145, 35, 30);

JTextField textx1c = new JTextField();
Ap.add(textx1c);
textx1c.setBounds(25, 195, 35, 30);

JTextField textx2c = new JTextField();
Ap.add(textx2c);
textx2c.setBounds(125, 195, 35, 30);

JTextField texty1c = new JTextField();
Ap.add(texty1c);
texty1c.setBounds(250, 195, 35, 30);
//Fin cuadros de texto

//Botones
JButton button1 = new JButton("Aceptar");
Ap.add(button1);
button1.setBounds(75, 300, 100, 20);

This is the code of my main window. I want a button that opens the window of the Learning Class.

package neuronaia;

import javax.swing.*;

public class MainWindow extends JFrame {

public static void main(String[] args) {
    // TODO Auto-generated method stub      

    JFrame Mw = new JFrame();
    Mw.setLayout(null);
    Mw.setSize(350,350);            
    Mw.setDefaultCloseOperation(Mw.EXIT_ON_CLOSE);
    Mw.setVisible(true);
    Mw.setLocationRelativeTo(null);

    JButton Apr = new JButton("Aprendizaje");
    Mw.add(Apr);
    Apr.setBounds(50, 50, 150, 30);

  }

}
    
asked by Juan Pimentel 08.09.2017 в 04:29
source

1 answer

0

To be able to open a JFrame the code that you have in the learning class, you should go in the constructor of that class, in addition if the class extends from JFrame , it is not necessary to create a new instance it would only be necessary to replace Ap by this .

Also declare the variables of the controls at the class level for use in some other process within JFrame later

import javax.swing.*;

public class Aprendizaje  extends JFrame {
    private JLabel lx1,lx2,ly1;
    private JTextField  textx1,textx2,texty1,
                        textx1a,textx2a,texty1a,
                        textx1b,textx2b,texty1b,
                        textx1c,textx2c,texty1c;
    private JButton button1;
    public Aprendizaje(){
    this.setLayout(null);
    this.setSize(350,350);            
    this.setTitle("thisrendizaje");
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setLocationRelativeTo(null);
    //Instanciando Cuadros de Texto         
    lx1 = new JLabel("X1");
    this.add(lx1);
    lx1.setBounds(25, 15, 25, 20);
    textx1 = new JTextField();
    this.add(textx1);
    textx1.setBounds(25, 35, 35, 30);
    lx2 = new JLabel("X2");
    this.add(lx2);
    lx2.setBounds(125, 15, 25, 20);
    textx2 = new JTextField();
    this.add(textx2);
    textx2.setBounds(125, 35, 35, 30);
    ly1 = new JLabel("Y1");
    this.add(ly1);
    ly1.setBounds(250, 10, 25, 30);
    texty1 = new JTextField();
    this.add(texty1);
    texty1.setBounds(250, 35, 35, 30);
    textx1a = new JTextField();
    this.add(textx1a);
    textx1a.setBounds(25, 90, 35, 30);
    textx2a = new JTextField();
    this.add(textx2a);
    textx2a.setBounds(125, 90, 35, 30);
    texty1a = new JTextField();
    this.add(texty1a);
    texty1a.setBounds(250, 90, 35, 30);
    textx1b = new JTextField();
    this.add(textx1b);
    textx1b.setBounds(25, 145, 35, 30);
    textx2b = new JTextField();
    this.add(textx2b);
    textx2b.setBounds(125, 145, 35, 30);
    texty1b = new JTextField();
    this.add(texty1b);
    texty1b.setBounds(250, 145, 35, 30);
    textx1c = new JTextField();
    this.add(textx1c);
    textx1c.setBounds(25, 195, 35, 30);
    textx2c = new JTextField();
    this.add(textx2c);
    textx2c.setBounds(125, 195, 35, 30);
    texty1c = new JTextField();
    this.add(texty1c);
    texty1c.setBounds(250, 195, 35, 30);
    //Fin cuadros de texto
    //Botones
     button1 = new JButton("Aceptar");
    this.add(button1);
    button1.setBounds(75, 300, 100, 20);
  }
}

In your class MainWindow you would only need the listener to the button and call the Learning

JButton Apr = new JButton("Aprendizaje");
Apr.addActionListener( new ActionListener()
 {
   @Override
   public void actionPerformed(ActionEvent ae) {
      Aprendizaje ap = new Aprendizaje();
   }
});
    
answered by 08.09.2017 / 05:19
source