将文本从JTextField复制到JTextArea

时间:2014-05-01 15:03:02

标签: java swing text jtextfield jtextarea

为什么我无法将文字从JTextField复制到JTextAreaordersJTextAreagetJTextField。状态为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());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果您只想在按下status按钮时复制文本,那么您可以这样做:

status.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent pE) {
        orders.setText(get.getText());
    }
});

如果要从Server Frame复制到Client框架或相反,您需要将目标组件传递给构造函数中的父类:

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);

或:

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

答案 1 :(得分:0)

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

客户端类:

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约定,类名应以大写字母开头。