Boa tarde Galera,
Estou com uma dificuldade tremenda em fazer uma Jtextarea para receber os textos pelo teclado virtual.
Alguém teria como dar uma luz? Pois queria que o teclado ficasse desta forma abaixo.
Já tenho parte do teclado feito, segue o código, em cima deste o que posso fazer?
Classe principal.
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
public class Principal extends JFrame{
JTextField txtusu=new JTextField(10);
JPasswordField txtsenha=new JPasswordField(10);
JPopupMenu pop;
JKeyboardPane teclado;
public Principal(){
colocarSkin();
JPanel janela=new JPanel();
JPanel p=new JPanel(new GridLayout(2,2,0,0));
janela.setPreferredSize(new Dimension(200,30));
p.add(new JLabel("Usuario: "));
p.add(txtusu);
p.add(new JLabel("Senha: "));
p.add(txtsenha);
txtsenha.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
pop = new JPopupMenu();
teclado=new JKeyboardPane(txtsenha);
pop.add(teclado);
pop.setVisible(true);
pop.setLocation(txtsenha.getLocationOnScreen().x+112, txtsenha.getLocationOnScreen().y-1);
}
@Override
public void focusLost(FocusEvent arg0) {
pop.setVisible(false);
}
});
txtusu.addFocusListener(new FocusListener(){
@Override
public void focusGained(FocusEvent arg0) {
pop = new JPopupMenu();
teclado=new JKeyboardPane(txtusu);
pop.add(teclado);
pop.setVisible(true);
pop.setLocation(txtsenha.getLocationOnScreen().x+112, txtsenha.getLocationOnScreen().y-1);
}
@Override
public void focusLost(FocusEvent arg0) {
pop.setVisible(false);
}
});
janela.add§;
add(janela);
}
public void colocarSkin(){
try {
UIManager.setLookAndFeel(“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public static void main(String arg[]){
Principal p=new Principal();
p.setVisible(true);
p.setBounds(0, 0, 300, 200);
p.setLocationRelativeTo(null);
p.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
JKeyboardPane
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class JKeyboardPane extends JPanel{
JTextField txt;
String teclas[]={“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“0”,“Q”,“W”,“E”,“R”,“T”,
“Y”,“U”,“I”,“O”,“P”,“A”,“S”,“D”,“F”,“G”,“H”,“J”,“K”,“L”,“Ñ”,“Z”,“X”,“C”,
“V”,“B”,“N”,“M”,"."};
ArrayList botoes=new ArrayList();
JPanel pletras,pespaco;
public JKeyboardPane(JTextField t){
txt=t;
pletras=new JPanel();
setLayout(new BorderLayout());
pletras.setLayout(new GridLayout(4,10,0,0));
ActionListener accion=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(!b.getText().equalsIgnoreCase(" “)){
txt.setText(”"+txt.getText()+b.getText());
}else{
txt.setText(txt.getText()+" ");
}
}
};
for(int i=0;i<37;i++){
if(teclas[i].equalsIgnoreCase(“Z”)){
JLabel l=new JLabel();
pletras.add(l);
}
JButton b=new JButton(teclas[i]);
b.addActionListener(accion);
pletras.add(b);
botoes.add(b);
}
pespaco=new JPanel(new GridLayout(1,3));
JButton bespacio=new JButton(" ");
bespacio.addActionListener(accion);
pespaco.add(new JLabel());
pespaco.add(bespacio);
pespaco.add(new JLabel());
add(pletras);
add(pespaco,BorderLayout.SOUTH);
}
}