我使用日食氧气。 我如何“转移”我在textarea的文本字段中写的内容? 请告诉我该怎么做,因为这是我学习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”中写东西时,字符串按Enter键进入textarea
答案 0 :(得分:0)
在定义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()) );
答案 1 :(得分:0)
定义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()) );