JTextField数据在不同的帧中,数据存储在全局变量中?

时间:2014-09-18 16:08:19

标签: java variables

我有2个帧,每个帧有1个文本字段,我想在一个字符串的第一帧文本字段中捕获并将其传递到文本字段中的另一个帧,但是将数据保存在全局变量类型String中。

(对不起我的英文)

2 个答案:

答案 0 :(得分:1)

Java没有“全局”变量,您也不想使用它。相反,您需要在M-V-C模型(模型 - 视图 - 控制)上稍微构建代码,并在模型中保存关键文本String。然后,您可以使用任何GUI窗口来监听模型以更改此变量,然后他们可以做出相应的反应。

如果您只想让两个JTextField共享完全相同的文本,那么您可以做的另一件事就是让它们共享相同的模型 - Document for JTextFields。

另外,请注意,您很少希望GUI同时显示多个JFrame。如果您需要第二个从属窗口,请转到对话框,例如JDialog。


修改
例如

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class SharedData {
   private static void createAndShowGui() {
      SimpleModel model = new SimpleModel();
      MyPanel1 panel1 = new MyPanel1(model);
      MyPanel2 panel2 = new MyPanel2(model);

      JFrame frame = new JFrame("SharedData");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel1);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Dialog", false);
      dialog.getContentPane().add(panel2);
      dialog.pack();
      dialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyPanel2 extends JPanel {
   private JTextField field = new JTextField(10);
   private UpdateAction updateAction = new UpdateAction("Update");
   private JButton updateDataBtn = new JButton(updateAction);
   private SimpleModel model;

   public MyPanel2(SimpleModel model) {
      this.model = model;
      field.addActionListener(updateAction);

      add(field);
      add(updateDataBtn);
   }

   private class UpdateAction extends AbstractAction {
      public UpdateAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (model != null) {
            model.setMyText(field.getText());
         }
      }
   }

}

class MyPanel1 extends JPanel {
   private JTextField field = new JTextField(10);
   private SimpleModel model;

   public MyPanel1(SimpleModel model) {
      field.setFocusable(false);
      this.model = model;
      add(field);

      model.addPropertyChangeListener(new ModelListener());
   }

   private class ModelListener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         if (SimpleModel.MY_TEXT.equals(evt.getPropertyName())) {
            field.setText(model.getMyText());
         }
      }
   }

}

class SimpleModel {
   public static final String MY_TEXT = "my text";
   private SwingPropertyChangeSupport support = new SwingPropertyChangeSupport(this);
   private String myText = "";

   public String getMyText() {
      return myText;
   }

   public void setMyText(String myText) {
      String oldValue = this.myText;
      String newValue = myText;
      this.myText = myText;
      support.firePropertyChange(MY_TEXT, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      support.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      support.removePropertyChangeListener(listener);
   }

}

编辑2
如果您希望两个JTextField共享与用户输入相同的数据,那么让它们共享模型(JTextFields的文档)。例如

import javax.swing.*;

public class SharedData2 {
   private static void createAndShowGui() {
      JTextField field1 = new JTextField(10);
      JTextField field2 = new JTextField(10);

      // ******** key below
      field2.setDocument(field1.getDocument());

      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();

      panel1.add(field1);
      panel2.add(field2);

      JFrame frame = new JFrame("SharedData2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel1);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Dialog", false);
      dialog.add(panel2);
      dialog.pack();
      int x = frame.getLocation().x + 200;
      int y = frame.getLocation().y + 200;

      dialog.setLocation(x, y);
      dialog.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:-1)

我解决了问题。

////第1帧(principio.java)发送///此帧发送内容

private void txtingresaKeyPressed(java.awt.event.KeyEvent evt) {                                      
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        fecha_1 obj = new fecha_1();
        obj.setVisible(true);
        fecha_1.txtfin.setText(txtingresa.getText());
        this.setVisible(false);
        }
    } 

///////第2帧(fecha_1.java)收到内容//////////

private void txtfinKeyPressed(java.awt.event.KeyEvent evt) {                                  
    if(evt.getKeyCode()==KeyEvent.VK_ENTER){
        periodo();
        txtini.setText(null);
        String fu = principio.txtingresa.getText();
        txtfin.setText(fu);//INGRESA REPETIDAMENTE EL VALOR INGRESADO EN PRINCIPIO CAJA TXTINGRESA

    }
} 

//代码重复但从不删除给他第一个表单的默认内容(principio.java)