You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将jTextField1与jTextField2的文本拼接后显示到jTextField3中

How to Concatenate Text from Two JTextFields and Display in a Third in Swing

Got it, let's break this down step by step. The core goal is to grab text from jTextField1 and jTextField2, join them together, and show the result in jTextField3—specifically when the inputs are "abcd" and "efg" respectively. Here's a complete, ready-to-use example:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextConcatenator extends JFrame {
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JButton concatButton;

    public TextConcatenator() {
        // Set up UI components
        jTextField1 = new JTextField(10);
        jTextField2 = new JTextField(10);
        jTextField3 = new JTextField(15);
        jTextField3.setEditable(false); // Make result field read-only for better UX
        concatButton = new JButton("Combine Text");

        // Layout the components
        JPanel panel = new JPanel();
        panel.add(new JLabel("First Input:"));
        panel.add(jTextField1);
        panel.add(new JLabel("Second Input:"));
        panel.add(jTextField2);
        panel.add(concatButton);
        panel.add(new JLabel("Result:"));
        panel.add(jTextField3);

        // Configure the frame
        add(panel);
        setTitle("Text Combiner");
        setSize(400, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Add action logic to the button
        concatButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Get text from both input fields
                String input1 = jTextField1.getText().trim();
                String input2 = jTextField2.getText().trim();

                // Check if inputs match your specified values
                if ("abcd".equals(input1) && "efg".equals(input2)) {
                    String combinedText = input1 + input2;
                    jTextField3.setText(combinedText);
                } else {
                    // Optional: Clear result or show a hint if inputs are wrong
                    jTextField3.setText("");
                    JOptionPane.showMessageDialog(null, "Please enter 'abcd' in the first field and 'efg' in the second!");
                }
            }
        });
    }

    public static void main(String[] args) {
        // Run UI code on Swing's Event Dispatch Thread (thread-safe practice)
        SwingUtilities.invokeLater(() -> new TextConcatenator().setVisible(true));
    }
}

Quick Notes:

  • Input Validation: The code checks if the inputs exactly match "abcd" and "efg" before concatenating—just like you asked. If you want to concatenate any input (not just those specific values), you can remove the if condition and directly set jTextField3.setText(input1 + input2).
  • Read-Only Result: We set jTextField3 to non-editable so users can't modify the result manually.
  • Thread Safety: Using SwingUtilities.invokeLater ensures the UI runs on the correct thread, which is a must for stable Swing apps.

内容的提问来源于stack exchange,提问作者Manu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:56:00