如何在两个 jframe 之间传递值 [英] How to pass value between two jframes

查看:49
本文介绍了如何在两个 jframe 之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 jframes,我想从打开的另一个 jframe 到其他打开的 jframe 获取价值.当单击 jframe1 open 按钮显示 jframe2 并在文本字段中输入一些文本并单击 ok 按钮时,文本字段值想要获得 jframe1 jlable.如何做到这一点我试过,但我找不到办法做到这一点.

I have two jframes, I want to get value from opened another jframe to other opened jframe. when click jframe1 open button showing jframe2 and type some text in text field and click ok button, text field value want to get jframe1 jlable. how to do this i tried but i can't find a way to do this.

这可能吗?

推荐答案

试试这个

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

class TestFrameExample extends JFrame  implements ActionListener{
    static JLabel label ; 
    public static TestFrameExample test;
    TestFrameExample()
   {
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());
      label = new JLabel("This is a label!");
      JButton button = new JButton("Open");
      button.setText("Press me");
      button.addActionListener(this);
      panel.add(label);
      panel.add(button);
      add(panel);
      setSize(300, 300);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
  }

   public void actionPerformed(ActionEvent a)
   {
          new TestFrameExample1();
  }
  public static void main(String s[]) {
      test=new TestFrameExample();
  }
}

class TestFrameExample1 extends JFrame  implements ActionListener {
  JTextField t;
  TestFrameExample test;
  public TestFrameExample1()
  {
        setSize(300, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(null);
        t=new JTextField();
        t.setBounds(100,20,150,20);
        JButton button=new JButton("oK");
        button.setBounds(100,50,100,30);
        button.addActionListener(this);
        add(t);
        add(button);
    }
    public void actionPerformed(ActionEvent a)
   {
        test.label.setText(t.getText());
   }
  }

这篇关于如何在两个 jframe 之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆