两个帧在不同的类中,如何使用actionListener

时间:2015-02-06 17:48:23

标签: java swing jframe actionlistener multiple-instances

我可能有一个非常简单的问题。我想创建两个框架--mainfr(在类中称为Pag)和dscrpt(在一个名为Apras的类中)。我设法使用框架mainfr中的按钮skaiciav创建第一帧(mainfr)和第二帧(dscrpt)。当问题是如何使用位于帧dscrp的按钮griz回到第一帧时?

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();
        }
    });
}

}   

第二课

    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);       
    }
}

0 个答案:

没有答案