2013-07-26 2 views
2

ok j'ai donc ce code:Comment utiliser jtextfield pour imprimer 2 mots?

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

ma question est de savoir comment puis-je changer la setText() dans la méthode de codage() donc je peux afficher plus de 1 mot à la fois? Je veux dire, s'il y a 2 mots écrits le code imprimera le premier à l'écran, puis imprimer le second, mais vous ne pouvez pas voir le premier ..

+0

voudrez peut-être essayer d'ajouter les deux mots, puis les ajouter à la JTextField . – Lenymm

+0

Ou vous obtenez la première chaîne avec getText(), concattez la deuxième chaîne et setText() –

+0

merci jannis :) –

Répondre

0

Pour afficher le texte, je voudrais le faire apparaître dans un 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 
        } 
       } 
     } 

Hop cela fonctionne,

3751_Creator

PS: Je recommande tables usising pour rendre le chiffrement mieux

Questions connexes