【问题标题】:Copy text from JTextField to JTextArea将文本从 JTextField 复制到 JTextArea
【发布时间】:2014-06-18 02:09:11
【问题描述】:

为什么我不能将文本从 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());
            }
        }
    }
}

【问题讨论】:

  • 这段代码执行后会发生什么?您是否在JTextArea 中收到文本? get.getText() 返回什么?
  • get 是 JTexTfield,status 是 JButton,orders 是 textArea,我希望当我在 JTextField 中输入内容时,它必须在 JTextArea 中“复制”

标签: java swing text jtextfield jtextarea


【解决方案1】:

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

客户端类:

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

【讨论】:

  • 如果您要在按下按钮时复制文本,请使用ActionListener
  • class k1 实现 ActionListener, { public void actionPerformed(ActionEvent event){ if(event.getSource() == status){ String geti = get.getText();订单.setText(geti); } }
【解决方案2】:

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

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

或:

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

【讨论】:

  • 但是我想当我按下状态 JButton 来复制它时,我尝试了 ActionListener 但它不起作用!
  • 它不起作用可能是因为我有两个类,但我正在使用继承,所以我必须做的 JtextField 在另一个类中,而 JTextArea 在另一个类中。
  • 所以我猜你需要发布更多代码,如果你想要一些关于如何让它在你的应用程序上工作的进一步帮助......
  • 哦,那时你的需求不是很清楚。您需要从服务器复制到客户端吗? (或相反)。是这个主意吗?
  • 如果您需要从服务器复制到客户端或相反,那么您需要为您的 JTextField 和 JTextArea 添加 getter,以便创建服务器和客户端的代码(此处未显示)可以获取 TextField之一,并使用新的 getter 将其内容放在其他人的 JTextArea 中。
猜你喜欢
  • 2013-10-15
  • 2014-09-02
  • 2011-06-19
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多