Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用侦听器通过单个调用更新多个JTextFields_Java_Listener_Deep Copy - Fatal编程技术网

Java 使用侦听器通过单个调用更新多个JTextFields

Java 使用侦听器通过单个调用更新多个JTextFields,java,listener,deep-copy,Java,Listener,Deep Copy,使用JButtons,我能够创建一个抽象动作,并将其与多个JButtons关联,从而允许我使用一个命令启用/禁用多个按钮 我希望拥有类似的功能,但只需一次调用即可更新多个JTextFields的值。不幸的是,我无法确定使用哪种类型的操作/侦听器或其他什么 下面是创建2个JFrame的基本代码示例。一个帧有一个JButton和一个JTextField。第二帧只有一个JTextField 无论是单击按钮还是在JTextfield中单击Return,计数器都将递增,并且仅出现在frame1的JText

使用JButtons,我能够创建一个抽象动作,并将其与多个JButtons关联,从而允许我使用一个命令启用/禁用多个按钮

我希望拥有类似的功能,但只需一次调用即可更新多个JTextFields的值。不幸的是,我无法确定使用哪种类型的操作/侦听器或其他什么

下面是创建2个JFrame的基本代码示例。一个帧有一个JButton和一个JTextField。第二帧只有一个JTextField

无论是单击按钮还是在JTextfield中单击Return,计数器都将递增,并且仅出现在frame1的JTextfield中。尽管在第2帧的JTextField中按enter键,该值仍仅在第1帧更新

主要目标是通过doData()更新两个帧上的JTextField,但要通过一些操作或侦听器或其他方式来完成,而不是直接引用JTextField本身

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

    import javax.swing.*;

    public class DetachedUpdate 
    {
      JTextField field1 = new JTextField();
      int x = 1;
      public static void main(final String args[]) 
      {
          DetachedUpdate myApp = new DetachedUpdate();
          myApp.field1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("actionPerformed()");
                    myApp.doData();
                }
            });
          buildFrame1(myApp.field1, myApp);
          buildFrame2(myApp.field1, myApp);
      }

      public void doData()
      {
          System.out.println("doData()");
          field1.setText(new Integer(x++).toString());
      }

      public static void buildFrame1(JTextField txtField, DetachedUpdate x)
      {

            JFrame frame = new JFrame("JToolBar Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JToolBar toolbar = new JToolBar();
            toolbar.setRollover(true);

            JButton button1 = new JButton("Button 1");

            button1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("actionPerformed()");
                    x.doData();
                }
            });

            toolbar.add(button1);
            toolbar.add(txtField);
            Container contentPane = frame.getContentPane();
            contentPane.add(toolbar, BorderLayout.NORTH);
            contentPane.setPreferredSize(new Dimension(10,10));
            frame.setSize(500, 500);
            frame.setVisible(true);
      }

      public static void buildFrame2(JTextField txtField, DetachedUpdate x)
      {
          JTextField newField = new JTextField();

            ActionListener[] listeners = txtField.getActionListeners();  
            for (int i = 0; i < listeners.length; i++)
            {
                newField.addActionListener(listeners[i]);
            }

            JFrame frame = new JFrame("JToolBar Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JToolBar toolbar = new JToolBar();
            toolbar.setRollover(true);

            JButton button1 = new JButton("Button 1");

            toolbar.add(newField);
            Container contentPane = frame.getContentPane();
            contentPane.add(toolbar, BorderLayout.NORTH);
            contentPane.setPreferredSize(new Dimension(10,10));
            frame.setSize(500, 500);
            frame.setVisible(true);
      }
    }
导入java.awt.event.*;
导入java.awt.*;
导入java.beans.PropertyChangeEvent;
导入java.beans.PropertyChangeListener;
导入javax.swing.*;
公共类DetachedUpdate
{
JTextField field1=新的JTextField();
int x=1;
公共静态void main(最终字符串参数[])
{
DetachedUpdateMyApp=新DetachedUpdate();
myApp.field1.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
System.out.println(“actionPerformed()”);
myApp.doData();
}
});
构建框架1(myApp.field1,myApp);
构建框架2(myApp.field1,myApp);
}
公共无效数据()
{
System.out.println(“doData()”);
field1.setText(新整数(x++.toString());
}
公共静态void buildFrame1(JTextField txtField,DetachedUpdate x)
{
JFrame=新JFrame(“JToolBar示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar=新的JToolBar();
toolbar.setRollover(true);
JButton button1=新JButton(“按钮1”);
button1.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
System.out.println(“actionPerformed()”);
x、 doData();
}
});
工具栏。添加(按钮1);
工具栏。添加(txtField);
容器contentPane=frame.getContentPane();
添加(工具栏,BorderLayout.NORTH);
setPreferredSize(新维度(10,10));
框架。设置尺寸(500500);
frame.setVisible(true);
}
公共静态void buildFrame2(JTextField txtField,DetachedUpdate x)
{
JTextField newField=新JTextField();
ActionListener[]监听器=txtField.getActionListeners();
for(int i=0;i
执行的
操作本身用于两个字段,但这不会自动同步字段内容。查看
DocumentListener
并尝试对两者使用相同的DocumentListener。

我在发布之前查看了DocumentListener,我的解释是它与管理用户插入的数据有关,而不是用于对字段进行编程更新。否则,您可以引入一个包含要更新的JTextField列表的新类,当发生
actionPerformed
时,您可以调用类似
updateComponents(field.getText())
的方法。我不认为“分享”听众是一种好的做法。