import javax.swing.*;
import java.awt.event.*;
public class TextFieldToLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("文本字段到标签的示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
JLabel label = new JLabel("请输入文本并按回车:");
JTextField textField = new JTextField(20);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userInput = textField.getText();
label.setText(userInput);
}
});
frame.setLayout(new java.awt.FlowLayout());
frame.add(label);
frame.add(textField);
frame.setVisible(true);
}
}