将数据发送到上一个JFrame [英] Send data to previous JFrame

查看:96
本文介绍了将数据发送到上一个JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JFrame,我有一个主JFrame,它需要第二个JFrame(例如jFrame2).我需要jFrame2有9个按钮,每个按钮中都包含图像(此处是完美的).然后,当在jFrame2中按下按钮时,关闭jFrame2并将一个字符串发送到jFrame1. jFrame1必须等待末尾的jFrame2接收发送jFrame2的字符串,并且由于jFrame1使用该字符串向服务器发送JSON.

I am working with JFrames, I have a main JFrame, which calls for a second JFrame (jFrame2 for example). I need at that jFrame2 has 9 buttons with images within each button (UP HERE ALL PERFECT). Then when in the jFrame2 press a button, close the jFrame2 and send a string to jFrame1. the jFrame1 must be waiting for the end jFrame2 to receive the string that sends the jFrame2, and since the jFrame1 send a JSON to the server with that string.

这是我的示例代码:

////// class jFrame1 //////

public class jFrame1 extends javax.swing.JFrame{

   public JTextField JTextField1; 
   public JTextField JTextField2; 
   public JTextField JTextField3; 
   private JButton jButton1;

   .....

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   jFrame2 regPack = new jFrame2();
   regPack.setLocationRelativeTo(null);
   regPack.setVisible(true);

   //HERE I WANT THE PROGRAM RESPONSE TO WAIT CLOSE AND CONTINUE RUNNING JFrame

   ....
}



////// class jFrame2 //////

public class jFrame2 extends javax.swing.JFrame{

   private JButton jButton1;
   private JButton jButton2;

   public jFrame2(){
   }

   .....

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   //HERE I WANT TO SEND A STRING ON JFRAME1 BEFORE CLOSING THIS JFRAME2
   this.dispose();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
   //HERE I WANT TO SEND A STRING ON JFRAME1 BEFORE CLOSING THIS JFRAME2
   this.dispose();
}

推荐答案

对其进行更改,以使frame2具有frame1可以检索所设置值的方法(获取器).

Change it so that frame2 has a method (getter) that frame1 can retrieve the value that was set.

对其进行更改,以使frame2为模态JDialog.这将使您的代码停止在显示JDialog的位置,但允许UI的其余部分保持响应状态.关闭对话框后,代码(frame1中的代码)将继续执行,您可以通过前面提到的getter从frame2中检索值.

Change it so that frame2 is a modal JDialog. This will cause you code to stop at the point that the JDialog is made visible, but allow the rest of the UI to remain responsive. When the dialog is closed, the code (in frame1) will continue executing and you can retrieve the value from frame2 via the fore mentioned getter.

有关更多信息,请参见如何制作对话框详细信息...

Take a look at How to make dialogs for more details...

这篇关于将数据发送到上一个JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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