java swing JFrame之间 数据传递

问题描述

建立了两个JFrame在java swing当中,但是怎样在两个JFrame之间传递数据,比如说JFrame1里面的一个文本框得到的数据传递到JFrame2的一个文本框中并显示出来,怎样才能实现这个功能? 问题补充:第一个窗口的代码import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/* * DLJM.java * * Created on __DATE__, __TIME__ */import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;/** * * @author __USER__ */public class DLJM extends javax.swing.JFrame {/** Creates new form DLJM */public DLJM() {initComponents();}/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. *///GEN-BEGIN:initComponents// <editor-fold defaultstate="collapsed" desc="Generated Code">private void initComponents() {buttonGroup1 = new javax.swing.ButtonGroup();gLYJM1 = new GLYJM();yHJM1 = new YHJM();zCZH1 = new ZCZH();zHMM1 = new ZHMM();jLabel1 = new javax.swing.JLabel();jTextField1 = new javax.swing.JTextField();jLabel2 = new javax.swing.JLabel();jPasswordField1 = new javax.swing.JPasswordField();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();jRadioButton1 = new javax.swing.JRadioButton();jRadioButton2 = new javax.swing.JRadioButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jLabel3 = new javax.swing.JLabel();jLabel4 = new javax.swing.JLabel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("u6b22u8fceu767bu5f55u90aeu5c40u8ba2u62a5u7cfbu7edf");jLabel1.setText("u7528u6237u8d26u53f7");jLabel2.setText("u5bc6u7801");jButton1.setText("u6ce8u518cu8d26u53f7");jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt);}});jButton2.setText("u627eu56deu5bc6u7801");jButton2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton2ActionPerformed(evt);}});buttonGroup1.add(jRadioButton1);jRadioButton1.setText("u7528u6237");buttonGroup1.add(jRadioButton2);jRadioButton2.setText("u7ba1u7406u5458");jButton3.setText("u767bu5f55");jButton3.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton3ActionPerformed(evt);}});jButton4.setText("u9000u51fa");jButton4.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton4ActionPerformed(evt);}});jLabel3.setFont(new java.awt.Font("楷体", 1, 23));jLabel3.setText("u90aeu5c40u8ba2u62a5u7cfbu7edfu6b22u8fceu60a8");jLabel4.setText("u767bu5f55u8eabu4efd");javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(33, 33, 33).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(layout.createSequentialGroup().addComponent(jButton3,javax.swing.GroupLayout.PREFERRED_SIZE,135,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,55,Short.MAX_VALUE).addComponent(jButton4,javax.swing.GroupLayout.PREFERRED_SIZE,109,javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2).addComponent(jLabel1).addComponent(jLabel4)).addGap(26, 26,26).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jPasswordField1,javax.swing.GroupLayout.DEFAULT_SIZE,137,Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.LEADING,layout.createSequentialGroup().addComponent(jRadioButton1).addGap(27,27,27).addComponent(jRadioButton2)).addComponent(jTextField1,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE,137,Short.MAX_VALUE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton2).addComponent(jButton1)))).addGap(124, 124, 124)).addGroup(layout.createSequentialGroup().addGap(72, 72, 72).addComponent(jLabel3).addContainerGap(168, Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(13, 13, 13).addComponent(jLabel3).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(jButton1).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(28, 28, 28).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jPasswordField1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel2).addComponent(jButton2)).addGap(36, 36, 36).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jRadioButton1).addComponent(jRadioButton2).addComponent(jLabel4)).addGap(46, 46, 46).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton4).addComponent(jButton3)).addContainerGap(38, Short.MAX_VALUE)));pack();}// </editor-fold>//GEN-END:initComponentsprivate void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {zHMM1.setVisible(true);this.dispose();}private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {zCZH1.setVisible(true);this.dispose();}private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {class main {public void main(String[] args) {window win = new window();}}if (this.jTextField1.getText().length() != 0&& this.jPasswordField1.getPassword().length != 0) {if (this.jRadioButton1.isSelected()|| this.jRadioButton2.isSelected()) {if (this.jRadioButton1.isSelected()) {try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=邮局订报管理","root", "root");Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);ResultSet rs = stmt.executeQuery("select*from 客户信息表 where 客户编号="+ this.jTextField1.getText() + "");String password = new String(jPasswordField1.getPassword());if (rs.next()) {if (password.equals(rs.getString("密码").trim())) {javax.swing.JOptionPane.showMessageDialog(null,"登录正确");yHJM1.setVisible(true);this.dispose();} else {javax.swing.JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重新输入");jPasswordField1.setText("");}} else {javax.swing.JOptionPane.showMessageDialog(null,"您输入的用户账号不存在,请重新输入");jTextField1.setText("");jPasswordField1.setText("");}} catch (Exception e) {e.printStackTrace();}} else {try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=邮局订报管理","root", "root");Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);ResultSet rs = stmt.executeQuery("select*from 管理员信息表 where 管理员编号="+ this.jTextField1.getText() + "");String password = new String(jPasswordField1.getPassword());if (rs.next()) {if (password.equals(rs.getString("密码").trim())) {javax.swing.JOptionPane.showMessageDialog(null,"登录正确");gLYJM1.setVisible(true);this.dispose();} else {javax.swing.JOptionPane.showMessageDialog(null,"您输入的密码不正确,请重新输入");jPasswordField1.setText("");}} else {javax.swing.JOptionPane.showMessageDialog(null,"您输入的管理员账号不存在,请重新输入");jTextField1.setText("");jPasswordField1.setText("");}} catch (Exception e) {e.printStackTrace();}}}else {javax.swing.JOptionPane.showMessageDialog(null, "请选择登录的身份!");}} else {javax.swing.JOptionPane.showMessageDialog(null, "请输入完整的登录信息!");}}private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {this.dispose();}/** * @param args * the command line arguments */public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new DLJM().setVisible(true);}});}//GEN-BEGIN:variables// Variables declaration - do not modifyprivate javax.swing.ButtonGroup buttonGroup1;private GLYJM gLYJM1;private javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JButton jButton3;private javax.swing.JButton jButton4;private javax.swing.JLabel jLabel1;private javax.swing.JLabel jLabel2;private javax.swing.JLabel jLabel3;private javax.swing.JLabel jLabel4;private javax.swing.JPasswordField jPasswordField1;private javax.swing.JRadioButton jRadioButton1;private javax.swing.JRadioButton jRadioButton2;private javax.swing.JTextField jTextField1;private YHJM yHJM1;private ZCZH zCZH1;private ZHMM zHMM1;// End of variables declaration//GEN-END:variables}第二个窗口的代码* GLYJM.java * * Created on __DATE__, __TIME__ *//** * * @author __USER__ */public class GLYJM extends javax.swing.JFrame {/** Creates new form GLYJM */public GLYJM() {initComponents();}/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. *///GEN-BEGIN:initComponents// <editor-fold defaultstate="collapsed" desc="Generated Code">private void initComponents() {bZXXGX1 = new BZXXGX();gLYXXGX1 = new GLYXXGX();xGMM1 = new XGMM();jLabel1 = new javax.swing.JLabel();jLabel2 = new javax.swing.JLabel();jLabel3 = new javax.swing.JLabel();jTextField1 = new javax.swing.JTextField();jTextField2 = new javax.swing.JTextField();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jButton5 = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("u6b22u8fceu7ba1u7406u5458u767bu5f55");jLabel1.setFont(new java.awt.Font("楷体", 1, 36));jLabel1.setText("u7ba1u7406u5458u64cdu4f5cu754cu9762");jLabel2.setText("u8d26u53f7");jLabel3.setText("u7528u6237u540d");jTextField1.setEditable(false);jTextField1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jTextField1ActionPerformed(evt);}});jTextField2.setEditable(false);jButton1.setText("u62a5u7eb8u4fe1u606fu66f4u65b0");jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt);}});jButton2.setText("u4fe1u606fu67e5u8be2u4e0eu7edfu8ba1");jButton3.setText("u7ba1u7406u5458u4fe1u606fu66f4u65b0");jButton3.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton3ActionPerformed(evt);}});jButton4.setText("u4feeu6539u5bc6u7801");jButton4.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton4ActionPerformed(evt);}});jButton5.setText("u9000u51fau767bu5f55");jButton5.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton5ActionPerformed(evt);}});javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(100,100,100).addComponent(jLabel1)).addGroup(layout.createSequentialGroup().addGap(82, 82,82).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2).addComponent(jLabel3)).addGap(43, 43,43).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false).addComponent(jTextField1).addComponent(jTextField2,javax.swing.GroupLayout.DEFAULT_SIZE,109,Short.MAX_VALUE))).addGroup(layout.createSequentialGroup().addGap(71, 71,71).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false).addComponent(jButton5,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(jButton3,javax.swing.GroupLayout.DEFAULT_SIZE,125,Short.MAX_VALUE)).addGap(28, 28,28).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false).addComponent(jButton4,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(jButton2,javax.swing.GroupLayout.DEFAULT_SIZE,132,Short.MAX_VALUE)))).addContainerGap(132, Short.MAX_VALUE)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(jTextField2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(33, 33, 33).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton2).addComponent(jButton1)).addGap(37, 37, 37).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton3).addComponent(jButton4)).addGap(34, 34, 34).addComponent(jButton5,javax.swing.GroupLayout.PREFERRED_SIZE,25,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(38, Short.MAX_VALUE)));pack();}// </editor-fold>//GEN-END:initComponentsprivate void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:}private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {xGMM1.setVisible(true);this.dispose();}private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {bZXXGX1.setVisible(true);this.dispose();}private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {this.dispose();}private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {gLYXXGX1.setVisible(true);this.dispose();}/** * @param args the command line arguments */public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new GLYJM().setVisible(true);}});}//GEN-BEGIN:variables// Variables declaration - do not modifyprivate BZXXGX bZXXGX1;private GLYXXGX gLYXXGX1;private javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JButton jButton3;private javax.swing.JButton jButton4;private javax.swing.JButton jButton5;private javax.swing.JLabel jLabel1;private javax.swing.JLabel jLabel2;private javax.swing.JLabel jLabel3;private javax.swing.JTextField jTextField1;private javax.swing.JTextField jTextField2;private XGMM xGMM1;// End of variables declaration//GEN-END:variables}实现功能:当登陆成功后把帐户名和用户名传送过去.帮忙实现一下.

解决方案

写个简单的例子,供你参考吧。package test;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;public class TestJFrame extends JFrame{private SecondJFrame second = new SecondJFrame();public TestJFrame() {this.setLayout(new BorderLayout());final JTextField text = new JTextField("first field");this.add(text, BorderLayout.CENTER);JButton button = new JButton("改变另一个JFrame的值");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String first = text.getText();second.changeTextValue(first);}});this.add(button, BorderLayout.SOUTH);this.setSize(300, 100);this.setLocation(200, 200);this.setVisible(true);this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });}/** * @param args */public static void main(String[] args) {new TestJFrame();}}class SecondJFrame extends JFrame {JTextField text = new JTextField("second field");public SecondJFrame() {this.setLayout(new BorderLayout());this.add(text, BorderLayout.CENTER);this.setSize(200, 100);this.setVisible(true);}public void changeTextValue(String newValue) {text.setText(newValue);}}
解决方案二:
参见:http://www.iteye.com/problems/940813种解决方法1.同个new 对象的形式对 B对象的某一个属性复制 而这个值,就是当前页面的查询值[构造函数传参,在new B的时候,把a中的值,传入到b中,b根据这个值初始化b中原件]2. b提供一个public 的方法,修改 b中原件的属性的值,给A调用,然后调用b的界面刷新(貌似内置的有 this.updateUI(); )的方法3.静态字段传值的方式了

时间: 2024-08-02 21:31:53

java swing JFrame之间 数据传递的相关文章

DataX实现oracle到oracle之间数据传递

文章讲的是DataX实现oracle到oracle之间数据传递,首先需要注意的是DATAX是通过JDBC的方式读取ORACLE数据,然后通过OCI的方式写数据,DX也可以通过JDBC写的方式进行,但是OCI比JDBC速度更快. 进入DataX安装目录的bin目录,执行命令 ./datax.py -e 输入交换数据数据库对应的代码,它会自动生成相应的xml配置文件 编辑配置文件参数,有"?"的是必须配置的,默认的可以保持不变 执行代码: vi /home/taobao/datax/job

事件-android surfaceview 和activity之间数据传递

问题描述 android surfaceview 和activity之间数据传递 想实现一个功能 就是在mainActivity中放置一个surfaceview和一个Textview 在surfaceview中添加点击onTouch事件 每次点击 让自己定义的surfaceview的变量 step加一 并时时在mainActivity的TextView中更新step的显示 不知道问题有没有描述清楚 就是想在一个view中时时显示另外一个view中定义的变量 解决方案 public class M

Android基于Intent实现Activity之间数据传递的方法_Android

本文实例讲述了Android基于Intent实现Activity之间数据传递的方法.分享给大家供大家参考,具体如下: MainActivity: package com.test.intentdemo; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import andro

解析activity之间数据传递方法的详解_Android

1  基于消息的通信机制 Intent--------boudle,extra用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int.string等详细介绍下Intent机制Intent包含两部分:1 目的[action]-------要去到哪里去2 内容[category.data]----------路上带些什么,区分性数据和内容性数据简单数据传递: 复制代码 代码如下: Intent intent = new Intent(LoginActivity.this, MainAct

解析activity之间数据传递方法的详解

1  基于消息的通信机制 Intent--------boudle,extra 用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int.string等 详细介绍下Intent机制 Intent包含两部分: 1 目的[action]-------要去到哪里去 2 内容[category.data]----------路上带些什么,区分性数据和内容性数据 简单数据传递: 复制代码 代码如下: Intent intent = new Intent(LoginActivity.this, M

MasterPage和内容页之间数据传递

MasterPage是asp.net 2.0中的一个新东东.具有如下的优点: 1.使用母版页可以集中处理页的通用功能,以便可以只在一个位置上进行更新. 2.使用母版页可以方便地创建一组控件和代码,并将结果应用于一组页.例如,可以在母版页上使用控件来创建一个应用于所有页的菜单. 3.通过允许控制占位符控件的呈现方式,母版页使您可以在细节上控制最终页的布局. 4.母版页提供一个对象模型,使用该对象模型可以从各个内容页自定义母版页. 在实际的使用中内容页要和MasterPage之间进行数据交换.下面介

JAVA POST与GET数据传递时中文乱码问题解决方法_JSP编程

做了N年的java开发从没有自己写过一个网站,最近乱忙活弄了一个企业家宣传网站. 跟大家一样遇到了中文字符集乱码问题,为了大家能少走一些弯路,所以在此分享一下即简单又实用的解决方法. 1. POST 数据传递 接收页 复制代码 代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEnco

基于startActivityForResult方法处理两个Activity之间数据传递问题_Android

废话不多说了,直接给大家贴代码了. package com.example.testactivityresquest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import

基于startActivityForResult方法处理两个Activity之间数据传递问题

废话不多说了,直接给大家贴代码了. package com.example.testactivityresquest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import