比较JTextArea中的行
创始人
2024-12-13 19:30:07
0

比较JTextArea中的行可以使用以下代码示例中的解决方法:

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

public class TextAreaComparisonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("TextArea Comparison Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea textArea1 = new JTextArea();
        textArea1.setLineWrap(true);
        textArea1.setWrapStyleWord(true);

        JTextArea textArea2 = new JTextArea();
        textArea2.setLineWrap(true);
        textArea2.setWrapStyleWord(true);

        JButton compareButton = new JButton("Compare");
        compareButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] lines1 = textArea1.getText().split("\\n");
                String[] lines2 = textArea2.getText().split("\\n");

                if (lines1.length != lines2.length) {
                    JOptionPane.showMessageDialog(frame, "Number of lines is different");
                    return;
                }

                for (int i = 0; i < lines1.length; i++) {
                    if (!lines1[i].equals(lines2[i])) {
                        JOptionPane.showMessageDialog(frame, "Lines at index " + i + " are different");
                        return;
                    }
                }

                JOptionPane.showMessageDialog(frame, "All lines are the same");
            }
        });

        JPanel panel = new JPanel();
        panel.add(new JScrollPane(textArea1));
        panel.add(new JScrollPane(textArea2));
        panel.add(compareButton);
        frame.add(panel);

        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}

这个示例创建了一个包含两个JTextArea和一个比较按钮的简单Swing应用程序。当点击比较按钮时,它将获取两个文本区域中的行,并逐行比较它们。如果行数不同,它将显示一个消息框指示行数不同。如果行相同,则显示一个消息框指示所有行都相同。

相关内容

热门资讯

今日头条((aapoKER))... 今日头条((aapoKER))外挂透明挂辅助挂(脚本辅助挂)的确真的有挂(可靠教程)-百度1、玩家可...
我来教教大家((governo... 我来教教大家((governorofpoker3))外挂透明挂辅助插件(透视辅助挂)总是真的有挂(2...
交流学习经验((Wpk))外挂... 交流学习经验((Wpk))外挂透明挂辅助软件(wpk辅助)一般真的有挂(力荐教程)-微博热搜1、起透...
专业讨论((wepoker))... 专业讨论((wepoker))外挂透明挂辅助APP(黑科技辅助挂)果真真的有挂(普及教程)-抖音1、...
一分钟教你((fish pok... 一分钟教你((fish poker))外挂透明挂辅助神器(透明辅助挂)一般真的有挂(详细教程)-知乎...
一分钟了解((wePoke))... 一分钟了解((wePoke))外挂透明挂辅助神器(透明辅助挂)就是真的有挂(解密教程)-抖音1、用户...
如何分辨真伪((Wpk))外挂... 如何分辨真伪((Wpk))外挂透明挂辅助插件(德州辅助)果然真的有挂(科技教程)-百度贴吧1、操作简...
重要通知((德州ai))外挂透... 重要通知((德州ai))外挂透明挂辅助脚本(透视辅助挂)本来真的有挂(透明挂教程)-小红书1、系统规...
必备攻略((wEpOke))外... 必备攻略((wEpOke))外挂透明挂辅助挂(德州辅助)果然真的有挂(介绍教程)-微博热搜1、全新机...
玩家爆料((扑克世界))外挂透... 玩家爆料((扑克世界))外挂透明挂辅助插件(黑科技辅助挂)其实真的有挂(2025新版)-抖音;在进入...