BuonGiorno a tutti

premetto che le mie conoscenze in Java sono minime per cui chiedo in anticipo scusa se la domanda verrà posta male o possa risultare banale ai più esperti.

in rete ho recuperato il codice sorgente di una tastiera virtuale in Java.

ho personalizzato tutti i pulsanti ma ora avrei encessità di inserire dei pulsanti che anzichè simulare il tasto premuto e inviare il comando del tasto , valorizzino delle variabili che poi andrò ad utilizzare per avere differenti modalità di visualizzazione

di seguito parte del codice che ho modificato

VirtualKeypadPanel.java :


int modalita = 0 ;
setLayout(gridBagLayout);
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
// colonna , riga , dimensione .
if(modalita == 0) {
addKeyButton(0, 1, 1, "q", KeyAction.forKeyType(KeyEvent.VK_Q));
addKeyButton(1, 1, 1, "w", KeyAction.forKeyType(KeyEvent.VK_W));
addKeyButton(2, 1, 1, "e", KeyAction.forKeyType(KeyEvent.VK_E));
addKeyButton(3, 1, 1, "r", KeyAction.forKeyType(KeyEvent.VK_R));
addKeyButton(4, 1, 1, "t", KeyAction.forKeyType(KeyEvent.VK_T));
addKeyButton(5, 1, 1, "y", KeyAction.forKeyType(KeyEvent.VK_Y));
addKeyButton(6, 1, 1, "u", KeyAction.forKeyType(KeyEvent.VK_U));
addKeyButton(7, 1, 1, "i", KeyAction.forKeyType(KeyEvent.VK_I));
addKeyButton(8, 1, 1, "o", KeyAction.forKeyType(KeyEvent.VK_O));
addKeyButton(9, 1, 1, "p", KeyAction.forKeyType(KeyEvent.VK_P));


addKeyButton(10, 4, 1, "Mode", ??????????? )
}


if(modalita == 1) {

addKeyButton(0, 1, 1, "Q", KeyAction.forKeyType(KeyEvent.VK_Q));
addKeyButton(1, 1, 1, "W", KeyAction.forKeyType(KeyEvent.VK_W));
addKeyButton(2, 1, 1, "E", KeyAction.forKeyType(KeyEvent.VK_E));
addKeyButton(3, 1, 1, "R", KeyAction.forKeyType(KeyEvent.VK_R));
addKeyButton(4, 1, 1, "T", KeyAction.forKeyType(KeyEvent.VK_T));
addKeyButton(5, 1, 1, "Y", KeyAction.forKeyType(KeyEvent.VK_Y));
addKeyButton(6, 1, 1, "U", KeyAction.forKeyType(KeyEvent.VK_U));
addKeyButton(7, 1, 1, "I", KeyAction.forKeyType(KeyEvent.VK_I));
addKeyButton(8, 1, 1, "O", KeyAction.forKeyType(KeyEvent.VK_O));
addKeyButton(9, 1, 1, "P", KeyAction.forKeyType(KeyEvent.VK_P));


addKeyButton(10, 4, 1, "Mode", ??????????? )
}


private void addKeyButton(int gridx, int gridy, int columns,
String buttonText, KeyAction... keyActions) {
KeyButton keyButton = new KeyButton(buttonText, keyActions);
keyButton.setFont(buttonFont); // Same Font for all buttons.

addButton(gridx, gridy, columns, keyButton);
}

private void addButton(int gridx, int gridy, int columns, KeyButton keyButton) {
keyButton.addActionListener(actionListener); // Same ActionListener for all buttons.

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = columns;
gbc.weightx = 3.0;
gbc.weighty = 3.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(4, 4, 4, 4);

add(keyButton, gbc);
}


per comodità non ho inserito nel post tutti i tasti.

settando manualmente la variabile modalita a 0 o 1 vedo la tastiera con i caratteri maiuscoli e minuscoli , l'ultimo pulsante e il pulsante mode che deve valorizzare la variabile modalità

questo invece il codice delle altre classi

KeyButton.java



import javax.swing.JButton;

/*
* KeyButton is a JButton with one or more KeyAction objects.
*/
public class KeyButton extends JButton {
private static final long serialVersionUID = 1L;

private KeyAction[] keyActions;

public KeyButton(String text, KeyAction keyAction) {
super(text);
keyActions = new KeyAction[] { keyAction };
}

public KeyButton(String text, KeyAction[] keyActions) {
super(text);
this.keyActions = keyActions.clone();
}

public KeyAction[] getKeyActions() {
return keyActions.clone();
}
}


KeyAction.java:


/*
* KeyAction defines immutable objects representing an "action" of a keyboard key.
*/
public class KeyAction {
private final int code;
private final Mode mode;

public KeyAction(int code, Mode mode) {
if (mode == null) {
throw new IllegalArgumentException("mode cannot be null");
}

this.code = code;
this.mode = mode;
}

public int getCode() {
return code;
}

public Mode getMode() {
return mode;
}

// Factory method for a "press" action.
public static KeyAction forKeyPress(int code) {
return new KeyAction(code, Mode.PRESS);
}

// Factory method for a "release" action.
public static KeyAction forKeyRelease(int code) {
return new KeyAction(code, Mode.RELEASE);
}

// Factory method for a "type" action.
public static KeyAction forKeyType(int code) {
return new KeyAction(code, Mode.TYPE);
}


public enum Mode {
PRESS, // press of a key
RELEASE, // release of a key
TYPE // type (press & release) of a key
}
}




Qualcuno saprebbe indicarmi che (e dove) devo inserire sul pulsante mode per impostare la variabile modalità?

Grazie 1000 per la collaborazione

Andrea