Ciao,
sto svolgendo un esercizio in cui mi si chiede di creare un cifrario per sostituzione.
Nello specifico:
Scrivere un'applicazione che implementi una codifica per sostituzione. Occorerà un'area di testo, al centro della finestra, che non possa essere modificata. Quest'area di testo mostrerà la codifica, in un formato come A->C, B->Q, C->F etc. Ciò significa che A verrebbe sostituita da C, B da Q, C da F etc. Il codice sarà generato una lettera alla volta dalla A alla Z. Dovranno essere presenti 26 pulsanti. Il primo specificherà la lettera da sostituire alla A, il secondo alla B etc etc.
Dopo la pressione del pulsante, si aggiunga la sostituzione corrispondente e si nasconda il pulsante premuto.
Ora, io ho un problema nella creazione dell'actionPerformed perché, dato un array char[] alfabeto contenente tutte le lettere e un ciclo for per scandagliare tale array, quando digito il tasto A, invece di farmi apparire "a -->A" mi fa apparire "z-->A".
Cosa sbaglio? Grazie
Ecco il codice (e in rosso la parte in questione)
codice:
package interfacceutentegrafiche;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class CodificaSostituzione extends JFrame implements ActionListener {
public static final int LARGHEZZA = 500;
public static final int ALTEZZA = 300;
public static final int RIGHE = 2;
public static final int CARATTERI_PER_RIGA = 40;
private JTextArea testo;
private char[] alfabeto = "abcdefghilmnopqrstuvz".toCharArray();
public CodificaSostituzione(){
super();
setTitle("Codifica per sostituzione!");
setSize(LARGHEZZA,ALTEZZA);
Container pannelloGrande = getContentPane();
pannelloGrande.setBackground(Color.LIGHT_GRAY);
JPanel pannelloLettere = new JPanel();
pannelloLettere.setBackground(Color.LIGHT_GRAY);
setLayout(new FlowLayout());
add(pannelloLettere,"North");
JButton A = new JButton("A");
A.setBackground(Color.LIGHT_GRAY);
A.addActionListener(this);
add(A);
JButton B = new JButton("B");
B.setBackground(Color.LIGHT_GRAY);
B.addActionListener(this);
add(B);
JButton C = new JButton("C");
C.setBackground(Color.LIGHT_GRAY);
C.addActionListener(this);
add(C);
JButton D = new JButton("D");
D.setBackground(Color.LIGHT_GRAY);
D.addActionListener(this);
add(D);
JButton E = new JButton("E");
E.setBackground(Color.LIGHT_GRAY);
E.addActionListener(this);
add(E);
JButton F = new JButton("F");
F.setBackground(Color.LIGHT_GRAY);
F.addActionListener(this);
add(F);
JButton G = new JButton("G");
G.setBackground(Color.LIGHT_GRAY);
G.addActionListener(this);
add(G);
JButton H = new JButton("H");
H.setBackground(Color.LIGHT_GRAY);
H.addActionListener(this);
add(H);
JButton I = new JButton("I");
I.setBackground(Color.LIGHT_GRAY);
I.addActionListener(this);
add(I);
JButton L = new JButton("L");
L.setBackground(Color.LIGHT_GRAY);
L.addActionListener(this);
add(L);
JButton M = new JButton("M");
M.setBackground(Color.LIGHT_GRAY);
M.addActionListener(this);
add(M);
JButton N = new JButton("N");
N.setBackground(Color.LIGHT_GRAY);
N.addActionListener(this);
add(N);
JButton O = new JButton("O");
O.setBackground(Color.LIGHT_GRAY);
O.addActionListener(this);
add(O);
JButton P = new JButton("P");
P.setBackground(Color.LIGHT_GRAY);
P.addActionListener(this);
add(P);
JButton Q = new JButton("Q");
Q.setBackground(Color.LIGHT_GRAY);
Q.addActionListener(this);
add(Q);
JButton R = new JButton("R");
R.setBackground(Color.LIGHT_GRAY);
R.addActionListener(this);
add(R);
JButton S = new JButton("S");
S.setBackground(Color.LIGHT_GRAY);
S.addActionListener(this);
add(S);
JButton T = new JButton("T");
T.setBackground(Color.LIGHT_GRAY);
T.addActionListener(this);
add(T);
JButton U = new JButton("U");
U.setBackground(Color.LIGHT_GRAY);
U.addActionListener(this);
add(U);
JButton V = new JButton("V");
V.setBackground(Color.LIGHT_GRAY);
V.addActionListener(this);
add(V);
JButton Z = new JButton("Z");
Z.setBackground(Color.LIGHT_GRAY);
Z.addActionListener(this);
add(Z);
JPanel pannelloTesto = new JPanel();
pannelloTesto.setBackground(Color.LIGHT_GRAY);
testo = new JTextArea(RIGHE, CARATTERI_PER_RIGA);
testo.setBackground(Color.WHITE);
pannelloTesto.add(testo);
add(pannelloTesto,"South");
}
public void actionPerformed(ActionEvent e){
for(int i=0; i<21;i++){
if(e.getActionCommand()=="A"){
testo.setText(alfabeto[i] + "--> A");
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CodificaSostituzione sostituzione = new CodificaSostituzione();
sostituzione.setVisible(true);
}
}