2

好的,所以我有这个代码:

package coding.language;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Launcher extends JFrame
{
    private static final long serialVersionUID = 1L;
    JPanel jp = new JPanel();
    static JLabel jl = new JLabel();
    static String[] input;
    JTextField jt = new JTextField(30);
    public static String a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,q,p,r,s,t,v,w,x,y,z;
    Scanner sc = new Scanner(System.in);
    public Launcher()
    {
        setTitle("Cypher");
        setVisible(true);
        setSize(400, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        jp.add(jt);
        jt.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                input = jt.getText().split("\\s+");
                coding();
            }
        });
        jp.add(jl);
        add(jp);
    }
    public static void main(String[] args)
    {
        Launcher t = new Launcher();
    }
    public static void coding()
    {
        for(int za = 0;za<input.length;za++)
        {
        int size = input[za].length();
        switch(size)
        {
        case 0: 
            System.out.println("You need to type in a longer input");
            break;
        case 1:
            a = input[za];
            jl.setText(code(a)+" ");
            break;
        case 2:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            jl.setText(code(a)+code(b)+" ");
            break;
        case 3: 
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            jl.setText(code(a)+code(b)+code(c)+" ");
            break;
        case 4: 
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            jl.setText(code(a)+code(b)+code(c)+code(d)+" ");
            break;
        case 5:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            jl.setText(code(a)+code(b)+code(c)+code(d)+code(e)+" ");
            break;
        case 6:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            f = input[za].substring(5,6);
            jl.setText(code(a)+code(b)+code(c)+code(d)+code(e)+code(f)+" ");
            break;
        case 7:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            f = input[za].substring(5,6);
            g = input[za].substring(6,7);
            jl.setText(code(a)+code(b)+code(c)+code(d)+code(e)+code(f)+code(g)+" ");
            break;
        case 8:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            f = input[za].substring(5,6);
            g = input[za].substring(6,7);
            h = input[za].substring(7,8);
            jl.setText(code(a)+code(b)+code(c)+code(d)+code(e)+code(f)+code(g)+code(h)+" ");
            break;
        case 9:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            f = input[za].substring(5,6);
            g = input[za].substring(6,7);
            h = input[za].substring(7,8);
            i = input[za].substring(8,9);
            jl.setText(code(a)+code(b)+code(c)+code(d)+code(e)+code(f)+code(g)+code(h)+code(i)+" ");
            break;
        case 10:
            a = input[za].substring(0,1);
            b = input[za].substring(1,2);
            c = input[za].substring(2,3);
            d = input[za].substring(3,4);
            e = input[za].substring(4,5);
            f = input[za].substring(5,6);
            g = input[za].substring(6,7);
            h = input[za].substring(7,8);
            i = input[za].substring(8,9);
            j = input[za].substring(9,10);
            System.out.print(code(a)+code(b)+code(c)+code(d)+code(e)+code(f)+code(g)+code(h)+code(i)+code(j)+" ");
            break;
        }
        }
    }
    public static String code(String string)
    {
        switch(string)
        {
        case "a": string = "z";
            break;
        case "b": string = "y";
            break;
        case "c": string = "x";
            break;
        case "d": string = "w";
            break;
        case "e": string = "v";
            break;
        case "f": string = "u";
            break;
        case "g": string = "t";
            break;
        case "h": string = "s";
            break;
        case "i": string = "r";
            break;
        case "j": string = "q";
            break;
        case "k": string = "p";
            break;
        case "l": string = "o";
            break;
        case "m": string = "n";
            break;
        case "n": string = "m";
            break;
        case "o": string = "l";
            break;
        case "p": string = "k";
            break;
        case "q": string = "j";
            break;
        case "r": string = "i";
            break;
        case "s": string = "h";
            break;
        case "t": string = "g";
            break;
        case "u": string = "f";
            break;
        case "v": string = "e";
            break;
        case "w": string = "d";
            break;
        case "x": string = "c";
            break;
        case "y": string = "b";
            break;
        case "z": string = "a";
            break;
        }
        return string;
    }
}

我的问题是如何更改方法 coding() 中的 setText() 函数,以便一次显示超过 1 个单词?我的意思是如果写了 2 个字,代码会将第一个字打印到屏幕上,然后打印第二个,但您看不到第一个字。

4

1 回答 1

0

为了显示文本,我会将其显示在 JTextArea 中。

         public class Frame extends JFrame implements ActionListener{
                public JTextArea Out = new JTextArea();

                public void Output(){ // Creating the JTextArea
                    JScrollPane sp = new JScrollPane(Out);
/*I recommend insterting the JTextArea in a JScrollPane in case the text is 
really long.*/
// The LineWrap and WrapStyleWord is to make sure the words will fit in the JTA
                    Out.setLineWrap(true);
                    Out.setWrapStyleWord(false);
                    Out.setEditable(false); 
// The person will be able to select it but not edit the output
                    sp.setBounds(15, 136, 260, 50);
                    getContentPane().add(sp);
                    sp.setEnabled(true);
                }
                public Frame(){
                    super("My Prog");
                    setLayout(null);
                    Output();
                }
                @Override
                public void actionPerformed(ActionEvent e){
                    if(....){
                         Out.setText(input); //When finiched, it will print it
                    }
                }
        }

跳它的工作,

3751_创造者

PS:我建议使用表来使加密更好

于 2013-09-14T16:04:38.717 回答