首页 > 解决方案 > 多个文本输入和按钮 Java

问题描述

我正在制作一个运行 Caeser Cipher 的 Java 程序。我创建了 2 种使用 Caeser Cipher 进行编码和解码的方法,它们就在这里。

//encoder method
public static String encoder(String text, int shift)
{
    String alpha = "abcdefghijklmnopqrstuvwxyz";
    String total = "";

    for (int i = 0; i < text.length(); i++)
    {
        String temp = text.substring(i, i + 1);
        if (!temp.equalsIgnoreCase(" "))
        {
            int e = alpha.indexOf(temp);
            e += shift;
            if (e >= 26)
                e -= 26;
            total += alpha.substring(e, e + 1);
        }
        else
            total += " ";
    }
    return total;
}

//decoder method
public static String decoder(String text, int shift)
{
    String alpha = "abcdefghijklmnopqrstuvwxyz";
    String total = "";

    for (int i = 0; i < text.length(); i++)
    {
        String temp = text.substring(i, i + 1);
        if (!temp.equalsIgnoreCase(" "))
        {
            int e = alpha.indexOf(temp);
            e -= shift;
            if (e <= 0)
                e += 26;
            total += alpha.substring(e, e + 1);
        }
        else
            total += " ";
    }
    return total;
}

我正在尝试创建一个提示 2 个文本框的程序:每个文本框旁边都有文本,上面写着“输入文本:”和“班次编号:”。在那些文本输入框下,我想要 2 个显示编码和解码的按钮。每个盒子都会运行相应的方法。我是创建 JPanels 和 JOptionPane 的新手,我已经查看了几天,但我仍然感到困惑。在我添加的课程之前:

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

我从 Stack Overflow 上的另一个问题中发现了这一点,但我仍然感到困惑。

谢谢!

标签: javajpaneljoptionpane

解决方案


推荐阅读