Java 将文本从JTextField复制到JTextArea

Java 将文本从JTextField复制到JTextArea,java,swing,text,jtextfield,jtextarea,Java,Swing,Text,Jtextfield,Jtextarea,为什么我不能将文本从JTextField复制到JTextAreaorders是一个JTextArea和get是一个JTextField。状态为JButton 类客户端: public client() extends superclass{ super("CLIENT"); setLayout(new FlowLayout()); update = new JLabel("status"); add(update,BorderL

为什么我不能将文本从
JTextField
复制到
JTextArea
orders
是一个
JTextArea
get
是一个
JTextField
。状态为
JButton

类客户端:

  public client() extends superclass{
        super("CLIENT");
        setLayout(new FlowLayout());
        update = new JLabel("status");
        add(update,BorderLayout.SOUTH);
        order = new JPanel();
        add(order,BorderLayout.SOUTH);
        menu = new JList(kveb);
        menu.setVisibleRowCount(8);
        menu.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(new JScrollPane(menu));
        get = new JTextField("chaweret rac gsurt");
        add(get);
        status = new JButton("ORDER!");
        status.setFont(new Font("Serif",Font.BOLD,16));    
        add(status);
        guga k1 = new guga();
        menu.addListSelectionListener(k1);
        get.addActionListener(k1);
        status.addActionListener(k1);
        server tes = new server();
        tes.setSize(300,300);
        tes.setDefaultCloseOperation(EXIT_ON_CLOSE);
        tes.setVisible(true);
    }
类服务器:

public class server extends superclass{
    public server() {
        super("SERVER");
        setLayout(new FlowLayout());
        // public JCheckBox ready;
         // public JCheckBox working;
         // public JCheckBox ordered;
          //public JCheckBox trans;
        //  public JTextField orders;
        ready = new JCheckBox("text");
        ready.setFont(new Font("Serif",Font.BOLD,16));
        add(ready);
         Font x = new Font("Serif",Font.BOLD,16);
         working = new JCheckBox("text here");
         working.setFont(x);
         add(working);
         migebulia = new JCheckBox("text heere");
         migebulia.setFont(x);
         add(migebulia);
         trans = new JCheckBox("mtext");
         trans.setFont(x);
         add(trans);
         orders = new JTextArea("text");
         orders.setFont(new Font("Serif",Font.BOLD,16));
         add(orders);
         guga k2 = new guga();
         ready.addActionListener(k2);
         working.addActionListener(k2);
         ordered.addActionListener(k2);
         trans.addActionListener(k2);
        // orders.addActionListener(k2);    
    }    
}
超级班:

public class superclass extends  JFrame {
      //client
      public JList menu;
      public  JTextField get;
      public JPanel order;
      public JButton status;
      public String kveb[] = {"text","texti","text","text"};
      public JLabel update;
      //server
      public JCheckBox ready;
      public JCheckBox working;
      public JCheckBox ordered;
      public JCheckBox trans;
      public   JTextArea orders;

    public superclass(String string) {

    }

    class guga implements ActionListener, ListSelectionListener,DocumentListener{
        public void actionPerformed(ActionEvent event){
            if(event.getSource() == status){
                event.setSource(get);
                 orders.setText(get.getText());
            }
        }
    }
}

我不知道您的应用程序的结构是什么,但请尝试将我在这里发布的内容与您当前的代码合并,并尽可能减少更改。一旦它起作用,你就可以开始移动东西了

客户端类:

public class Client {

    public JTextField get = new JTextField("chaweret rac gsurt");
    public JButton status = new JButton("ORDER!");

    Client() {

        status.addActionListener(new Superclass.MyListener());
    }
}
服务器类:

public class Server {

    public JTextArea orders = new JTextArea("text");
}
超类:

public class Superclass {

    static Client client = new Client();
    static Server server = new Server();

    public static class MyListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource().equals(client.status))
                server.orders.setText(client.get.getText());
        }
    }
}

注意:根据Java约定,类名应以大写字母开头。

如果只想在按下按钮
状态时复制文本,则可以执行以下操作:

status.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent pE) {
        orders.setText(get.getText());
    }
});
如果要从服务器帧复制到客户端帧或相反的帧,则需要将目标组件传递到构造函数中的父类:

JTextArea destTextArea;
public superclass(String string, JTextArea pDestTextArea) {
    destTextArea = pDestTextArea;
}
....
public void actionPerformed(ActionEvent event){
    if ((event.getSource() == status) && (destTextArea != null)) {
        destTextArea.setText(get.getText());
    }
}
然后,要创建客户端和服务器:

super("CLIENT", null);
super("SERVER", orders);
或:


执行这段代码时会发生什么?您是否在
jtext区域中获得文本?
get.getText()
返回什么?get是JTexTfield,状态是JButton orders是textArea,我希望当我在JTexTfield中键入内容时,它必须在jtextarea中“复制”如果您试图在按下按钮时复制文本,那么使用
ActionListener
。k1类实现ActionListener,{public void actionPerformed(ActionEvent事件){if(event.getSource()==status){String geti=get.getText();orders.setText(geti);}}但我想当我按下状态JButton复制它时,我尝试了ActionListener,但它不起作用!它不起作用可能是因为我有两个类,但我使用的是继承,所以我要做的是JtextField在另一个类中,而JTextArea在另一个类中。所以我想如果你想得到关于如何使它在你的计算机上工作的进一步帮助,你需要发布更多的代码您的应用程序…哦,您当时的需求不是很清楚。您需要从服务器复制到客户端吗?(或相反)。这就是想法吗?如果您需要从服务器复制到客户端或相反的客户端,那么您需要为您的JTextField和JTextArea添加getter,以便代码(此处未显示)这样,服务器和客户机就可以获取其中一个的TextField,并使用新的getter将其内容放在其他服务器的JTextArea中。
super("CLIENT", orders);
super("SERVER", null);