问题描述
建立了两个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.静态字段传值的方式了