栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java制作一个GUI实现字符串的相关操作(合并、比较、检索、清除)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java制作一个GUI实现字符串的相关操作(合并、比较、检索、清除)

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;



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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号