编写一程序,实现窗口将两个输入框内的字符串连接起来。文件保存为T3.java。
时间: 2024-05-14 11:16:41 浏览: 91
好的,这个问题很简单。以下是T3.java的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class T3 extends JFrame implements ActionListener {
private JTextField textField1, textField2;
private JButton button;
private JLabel label;
public T3() {
super("字符串连接");
textField1 = new JTextField(10);
textField2 = new JTextField(10);
button = new JButton("连接");
button.addActionListener(this);
label = new JLabel("结果:");
JPanel panel1 = new JPanel();
panel1.add(textField1);
panel1.add(textField2);
JPanel panel2 = new JPanel();
panel2.add(button);
JPanel panel3 = new JPanel();
panel3.add(label);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel1, BorderLayout.NORTH);
contentPane.add(panel2, BorderLayout.CENTER);
contentPane.add(panel3, BorderLayout.SOUTH);
setContentPane(contentPane);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String s1 = textField1.getText();
String s2 = textField2.getText();
String result = "结果:" + s1 + s2;
label.setText(result);
}
}
public static void main(String[] args) {
T3 window = new T3();
window.setVisible(true);
}
}
```
这是一个基于Java Swing的GUI应用程序,有两个文本框和一个按钮,用户可以在文本框中输入字符串并点击按钮,程序将把两个字符串连接到一起并在标签中显示结果。
阅读全文