栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

嗨,这是一种实现方法,您只需要用构造函数构造一个新的Jframe即可获取所需的参数。

第一个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);    }}

第二个Jframe,在其中接收消息。

import javax.swing.Jframe;public class frame2 extends Jframe {    public frame2(String message) {        super();        setVisible(true);        setTitle(message);    }}

希望对您有所帮助。问候!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号