0

I probably have a really simple problem. I want to create two frames - mainfr (in the class called Pag) and dscrpt (in a class called Apras). I managed to create the first frame (mainfr) and the second frame (dscrpt) using button skaiciav from the frame mainfr. When question is how to come back to the first frame using the button griz located in frame dscrp?

package grap;

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

public class Pag implements ActionListener {
JFrame mainfr;
JFrame apras;
JLabel psl_apras;
JLabel galim_veiksm;
JLabel paspaud;

public Pag() {
    //Create JFrame container
    mainfr = new JFrame("Turinys");

    //Choosing layout type
    mainfr.setLayout(new FlowLayout());

    //window resolution
    mainfr.setSize(500, 300);

    //Program terminates on close
    mainfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      

    //Label's
    psl_apras = new JLabel("Mokomoji programa"); 
    galim_veiksm = new JLabel("Galimi veiksmai");

    //adding them to main frame
    mainfr.add(psl_apras);
    mainfr.add(galim_veiksm);

    //Button
    JButton skaiciav = new JButton("Description");

    //action listener
    skaiciav.addActionListener(this);

    //adding button to frame
    mainfr.add(skaiciav);
    //another label
    paspaud = new JLabel("Press button");       

    //adding label to frame
    mainfr.add(paspaud);

    //making frame visible
    mainfr.setVisible(true);
}

//action listener method
public void actionPerformed(ActionEvent a) {
    if(a.getActionCommand().equals("Description"))
    {           
        Apras.suk();
        mainfr.setVisible(false);
    }

    //Started to get confused here
    /*if(a.getActionCommand().equals("back")){

        mainfr.setVisible(true);
        apras.setVisible(false);
        dscrp.dispose();            
    */
}


public static void main (String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Pag();
        }
    });
}

}   

Second class

    package grap;

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

    public class Apras {
       static JFrame dscrp;

       static void suk() {
       dscrp = new JFrame("Description");

       dscrp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
       //frame2.setLocationByPlatform(true);

       dscrp.setBackground(Color.WHITE);
       JButton griz = new JButton("back");
       //griz.addActionListener();
       dscrp.add(griz);

       dscrp.setSize(500, 300);
       dscrp.setVisible(true);       
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mindaugas
  • 1
  • 1
  • The design is a little weird here, but you could pass an `ActionListener` into the `suck()` method and then set the `griz` buttons listener to the parameter. i.e. `suk(ActionListener listener)`{ ... griz.setActionListener(listener); }` and then call `Apras.suk(this);` – chancea Feb 06 '15 at 17:55
  • Make the second frame a modal dialog. This way, when you open it, the code execution will block at that point until it is closed, you can get the values from the dialog using getters – MadProgrammer Feb 06 '15 at 20:21
  • See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Feb 06 '15 at 23:20

0 Answers0