【问题标题】:transferring string in another place JAVA将字符串转移到另一个地方JAVA
【发布时间】:2018-10-29 22:41:39
【问题描述】:

我使用 Eclipse 氧气。 我怎样才能“转移”我在文本区域的文本字段中写的内容? 请告诉我怎么做,因为这是我学习 Java 的第三天,我做这些事情并不容易。 我在学校学习java。

package layout;

import javax.swing.*;
import java.awt.*;

public class Frame2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame("Acquisti");
        frame.setBounds(50, 50, 400, 300);

        JPanel p1 = new JPanel(new GridLayout(3,2)); 
        p1.setBackground(Color.BLUE);

        frame.add(p1, BorderLayout.NORTH);

        JLabel l1 = new JLabel("Products");
        p1.add(l1);

        JLabel l2 = new JLabel("Price");
        p1.add(l2);

        JTextField tx1 = new JTextField(5);
        p1.add(tx1);

        JTextField tx2 = new JTextField(5);
        p1.add(tx2);

        JPanel p2 = new JPanel(new FlowLayout()); 
        p2.setBackground(Color.CYAN);
        frame.add(p2, BorderLayout.WEST);

        JLabel l3 = new JLabel("Lista");
        p2.add(l3);

        JButton b1 = new JButton("ADD");
        p2.add(b1);

        JTextArea tx = new JTextArea(10, 40);
        p2.add(tx);

        JPanel p3 = new JPanel();
        frame.add(p3, BorderLayout.EAST);

        JLabel l4 = new JLabel("Valuta");
        p3.add(l4);

        JRadioButton rb1 = new JRadioButton("Lire");
        p3.add(rb1);

        JRadioButton rb2 = new JRadioButton("Euro");
        p3.add(rb2);

        JLabel l5 = new JLabel("Totale");
        p3.add(l5);

        JTextArea tx3 = new JTextArea(5,5);
        p3.add(tx3);

        JPanel p4 = new JPanel();
        p4.setBackground(Color.YELLOW);
        frame.add(p4, BorderLayout.SOUTH);
        JButton b2 = new JButton("SAVE");
        p4.add(b2);

        frame.setDefaultCloseOperation(1);
        frame.setVisible(true);
    }
}

例如!! 每次我在第一个文本字段“tx1”中写一些东西时,字符串都会按回车键进入文本区域

【问题讨论】:

  • tx.setText( tx1.getText() ); 但目前尚不清楚您实际上要做什么,因此请谨慎对待。
  • 举个例子!!每次我在第一个文本字段“tx1”中写一些东西时,字符串都会按回车键进入文本区域
  • 阅读 How to Use Text Areas 上的 Swing 教程中的部分,以获得完全符合您要求的工作示例。 this is the third day that i study java - 我建议您保留该教程的链接,因为它包含使用 Swing 组件的所有基础知识。
  • “这是我学习java的第三天了” 那你应该还在整理命令行应用中的语言基础吧!

标签: java swing textarea textfield


【解决方案1】:

在定义了 tx1 和 tx2 之后,给 tx1 添加一个监听器

    tx1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            tx2.setText(tx1.getText()); //updates tx2 when you press enter 
        }
    });

使用 lambda 表达式的另一种更短的语法:

    tx1.addActionListener( e-> tx2.setText(tx1.getText()) );

【讨论】:

    【解决方案2】:

    定义 tx1 和 tx2 然后为 tx1 添加一个监听器

    tx1.addActionListener(new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            tx2.setText(tx1.getText()); //get new value into tx2 when you hit enter 
        }
    });
    

    否则.... 您可以使用 lambda 表达式通过简化方式.....

    tx1.addActionListener( e-> tx2.setText(tx1.getText()) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多