简体   繁体   中英

how to pass a variable from one JFrame to another

I have two JFrames newAccessLevels.java , which has two buttons "Level 1" "Level 2" and newAccessPanel.java I need to get the level that the user selects "1 or 2" onto the accessPanel so I can display it in the title of the accessPanel.java eg Access Level 1, Access Level 2. How can this be done. below is example code, so if level 1 is clicked the newAccessPanel JFrame will open with the title * ACCESS LEVEL 1 and vice versa for level 2:

newAccessLevels.java

package securitySystem;

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

public class newAccessLevels extends JFrame{

public static void main (String args[]){
    newAccessLevels gui= new newAccessLevels ();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setTitle("Access Levels");
    gui.setSize(400,400);
    gui.setLocationRelativeTo(null);
    gui.setVisible(true);       
}

JButton btnLevel1= new JButton("Levels 1");
JButton btnLevel2= new JButton("Level 2");


public newAccessLevels (){
    setLayout (null);

    btnLevel1.setBounds(120,70, 150, 30);
    add(btnLevel1);

    btnLevel2.setBounds(120,130, 150, 30);
    add(btnLevel2); 
}

public void calcButtons()
{
    btnLevel1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e) 
        {
            newAccessPanel gui =new newAccessPanel();
            gui.setSize (360, 450);
            gui.setLocationRelativeTo(null);
            gui.setVisible(true);
            dispose();              
        }
    });

    btnLevel2.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e) 
        {
            newAccessPanel gui =new newAccessPanel();
            gui.setSize (360, 450);
            gui.setLocationRelativeTo(null);
            gui.setVisible(true);
            dispose();              
        }
    });
}

}

newAccessPanel.java

package securitySystem;

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

public class newAccessPanel extends JFrame{

public static void main (String args[]){
    newAccessPanel gui= new newAccessPanel ();
    gui.setSize (360, 450);
    gui.setLocationRelativeTo(null);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setTitle("ACCESS LEVEL '1/2'");     
    //gui.setLayout(new BorderLayout());
    //gui.setBackground(Color.BLACK);       
}

}

Hi this is an approach of how you can do it, simply you need to construct a new JFrame with a constructor that recives the parameter you need.

First JFrame, where there are the buttons

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;

public class Frame1 extends JFrame{

    private String mensaje;
    private JButton btnHola;
    private JButton btnAdios;

    public Frame1() {
        getContentPane().setLayout(null);

        btnHola = new JButton("Hello");
        btnHola.setBounds(63, 210, 89, 23);
        getContentPane().add(btnHola);
        btnHola.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                mensaje = Frame1.this.btnHola.getText();
                Frame2 frame2 = new Frame2(mensaje);
            }
        });

        btnAdios = new JButton("Bye");
        btnAdios.setBounds(245, 210, 89, 23);
        getContentPane().add(btnAdios);

        btnAdios.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            mensaje = Frame1.this.btnAdios.getText();
            Frame2 frame2 = new Frame2(mensaje);
        }
    });
    }

    public static void main(String[] args) {
        Frame1 frame = new Frame1();
        frame.setVisible(true);
    }
}

Second JFrame, where the message is received.

import javax.swing.JFrame;

public class Frame2 extends JFrame {
    public Frame2(String message) {
        super();
        setVisible(true);
        setTitle(message);
    }
}

I hope this help you. Greetings!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM