java制作一个GUI实现字符串的相关操作(合并、比较、检索、清除)
1、使用Box容器
2、字符串比较compareTo()
3、字符串检索indexOf()
4、提示弹窗JOptionPane.showMessageDialog()
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* java制作一个GUI实现字符串的相关操作(合并、比较、检索、清除)
*/
public class StringGUI {
StringGUI() {
//顶层容器
JFrame jf = new JFrame("字符串操作");
//各个Box容器
Box hBox1 = Box.createHorizontalBox();
Box hBox2 = Box.createHorizontalBox();
Box hBox3 = Box.createHorizontalBox();
Box hBox4 = Box.createHorizontalBox();
Box hBox = Box.createHorizontalBox();
Box vBox = Box.createVerticalBox();
//标题
JLabel title = new JLabel("字符串操作");
//第一行
JLabel label1 = new JLabel("第一个字符串:");
TextField in1 = new TextField();
hBox1.add(label1);
hBox1.add(in1);
//第二行
JLabel label2 = new JLabel("第二个字符串:");
TextField in2 = new TextField();
hBox2.add(label2);
hBox2.add(in2);
//第三行
JLabel label3 = new JLabel(" 结 果: ");
TextField in3 = new TextField();
hBox3.add(label3);
hBox3.add(Box.createHorizontalStrut(15));
hBox3.add(in3);
//第四行
JButton btn1 = new JButton("合并");
JButton btn2 = new JButton("比较");
JButton btn3 = new JButton("检索");
JButton btn4 = new JButton("清除");
hBox4.add(Box.createHorizontalStrut(50));
hBox4.add(btn1);
hBox4.add(Box.createHorizontalStrut(20));
hBox4.add(btn2);
hBox4.add(Box.createHorizontalStrut(20));
hBox4.add(btn3);
hBox4.add(Box.createHorizontalStrut(20));
hBox4.add(btn4);
hBox4.add(Box.createHorizontalStrut(50));
//添加至竖直的Box容器
vBox.add(Box.createVerticalStrut(20));
vBox.add(title);
vBox.add(Box.createVerticalStrut(15));
vBox.add(hBox1);
vBox.add(Box.createVerticalStrut(15));
vBox.add(hBox2);
vBox.add(Box.createVerticalStrut(15));
vBox.add(hBox3);
vBox.add(Box.createVerticalStrut(15));
vBox.add(hBox4);
vBox.add(Box.createVerticalStrut(30));
//将竖直的Box添加至横向Box,并在左右加间隔
hBox.add(Box.createHorizontalStrut(40));
hBox.add(vBox);
hBox.add(Box.createHorizontalStrut(40));
//将最终的横向Box添加至顶层容器
jf.add(hBox);
//窗体不可变大小
jf.setResizable(false);
//自适应窗体大小
jf.pack();
//窗体居中
jf.setLocationRelativeTo(null);
//窗体显示出来
jf.setVisible(true);
//关闭窗体结束进程
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//各按键添加事件监听
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (in1.getText().length() == 0 && in2.getText().length() == 0)
JOptionPane.showMessageDialog(null, "请正确输入!", "警告", JOptionPane.ERROR_MESSAGE);
else
in3.setText(in1.getText() + in2.getText());
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (in1.getText().length() == 0 || in2.getText().length() == 0)
JOptionPane.showMessageDialog(null, "请正确输入!", "警告", JOptionPane.ERROR_MESSAGE);
else if (in1.getText().compareTo(in2.getText()) > 0)
in3.setText("第一个字符串较大");
else if (in1.getText().compareTo(in2.getText()) == 0)
in3.setText("两个字符串相等");
else if (in1.getText().compareTo(in2.getText()) < 0)
in3.setText("第二个字符串较大");
}
});
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s1 = in1.getText();
String s2 = in2.getText();
if (s1.length() == 0 || s2.length() == 0)
JOptionPane.showMessageDialog(null, "请正确输入!", "警告", JOptionPane.ERROR_MESSAGE);
else if (s2.indexOf(s1) >= 0) {
in3.setText("字符串1存在字符串2的第" + (s2.indexOf(s1) + 1) + "个位置");
} else if (s1.indexOf(s2) >= 0) {
in3.setText("字符串2存在字符串1的第" + (s1.indexOf(s2) + 1) + "个位置");
} else {
in3.setText("未能成功检索");
}
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
in1.setText("");
in2.setText("");
in3.setText("");
JOptionPane.showMessageDialog(null, "清除已完成!", "清除", JOptionPane.INFORMATION_MESSAGE);
}
});
}
public static void main(String[] args) {
new StringGUI();
}
}