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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20382999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:32:24  来源:igfitidea点击:

how to pass a variable from one JFrame to another

javaswing

提问by Adam

I have two JFramesnewAccessLevels.java, which has two buttons "Level 1" "Level 2" and newAccessPanel.javaI need to get the level that the user selects "1 or 2" onto the accessPanelso I can display it in the title of the accessPanel.javae.g. 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 1and vice versa for level 2:

我有两个JFramesnewAccessLevels.java,它有两个按钮“Level 1”“Level 2”和newAccessPanel.java我需要获得用户选择“1 或 2”的级别,accessPanel以便我可以在标题中显示它accessPanel.java例如访问级别 1、访问级别 2。如何做到这一点。下面是示例代码,因此如果单击级别 1,则 newAccessPanel JFrame 将打开,标题为 * ACCESS LEVEL 1,反之亦然:

newAccessLevels.java

新访问级别.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

新访问面板.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);       
}

}

}

采纳答案by sirandy

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!

我希望这对你有帮助。你好!