简体   繁体   English

如何将变量从一个JFrame传递到另一个

[英]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. 我有两个JFrames newAccessLevels.java,其中有两个按钮“层次1”“层次2”和newAccessPanel.java我需要获得用户选择电平“1或2”到accessPanel所以我可以在标题显示它accessPanel.java的示例,例如访问级别1,访问级别2。如何完成此操作。 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: 下面是示例代码,因此,如果单击级别1,则将打开标题为* ACCESS LEVEL 1的newAccessPanel JFrame,反之亦然:

newAccessLevels.java 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 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. 嗨,这是一种实现方法,您只需要用构造函数构造一个新的JFrame即可获取所需的参数。

First JFrame, where there are the buttons 第一个JFrame,有按钮

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. 第二个JFrame,在其中接收消息。

import javax.swing.JFrame;

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

I hope this help you. 希望对您有所帮助。 Greetings! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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