OLá ! Não se qual o erro no código , pois já fiz no final do código o " tfResposta.requestFocus();"
, então quando dou o run , ele não foca no tfResposta , mas foca em outro botão.(btnSair)
Eu quero que foca no tfResposta.
Alguém pode me ajudar ?
package tabuada;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
public class Tabuada_1 {
private JFrame frmTabuada;
private JTextField tfResposta;
private JLabel lblResposta;
private JLabel lblOperacao;
private JButton btnTestar;
private JLabel lblN2;
private JLabel lblN1;
private JLabel label;
int n1,n2,operacao,oculto;
String q , operacao2, respostaCorreta;
int erro,resposta ;
private JLabel lblAcertos_1;
private JLabel lblErros_1;
private JButton btnPular;
private JButton btnVoltar;
private JButton btnSair;
private JLabel lblErros;
private JLabel lblAcertos;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tabuada_1 window = new Tabuada_1();
window.frmTabuada.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Tabuada_1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTabuada = new JFrame();
frmTabuada.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_0) {
frmTabuada.getContentPane().setBounds(100, 100, 700, 800);
}
}
});
frmTabuada.setTitle("Multipli\u00E7\u00E3o");
frmTabuada.getContentPane().setBackground(new Color(255, 255, 51));
frmTabuada.setBounds(100, 100, 1612, 862);
frmTabuada.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTabuada.getContentPane().setLayout(null);
frmTabuada.setResizable(false);
frmTabuada.setLocationRelativeTo(null);
lblN1= new JLabel("1");
lblN1.setFont(new Font("Tahoma", Font.PLAIN, 99));
lblN1.setBounds(111, 218, 165, 198);
frmTabuada.getContentPane().add(lblN1);
lblOperacao = new JLabel("x");
lblOperacao.setFont(new Font("Tahoma", Font.PLAIN, 99));
lblOperacao.setBounds(390, 238, 131, 158);
frmTabuada.getContentPane().add(lblOperacao);
lblN2 = new JLabel("4");
lblN2.setForeground(Color.BLACK);
lblN2.setFont(new Font("Tahoma", Font.PLAIN, 99));
lblN2.setBounds(626, 218, 170, 198);
frmTabuada.getContentPane().add(lblN2);
label = new JLabel("=");
label.setFont(new Font("Tahoma", Font.PLAIN, 99));
label.setBounds(843, 273, 121, 88);
frmTabuada.getContentPane().add(label);
lblResposta = new JLabel("8");
lblResposta.setFont(new Font("Tahoma", Font.PLAIN, 99));
lblResposta.setBounds(1058, 218, 158, 184);
frmTabuada.getContentPane().add(lblResposta);
tfResposta = new JTextField();
tfResposta.setForeground(Color.RED);
tfResposta.setFont(new Font("Tahoma", Font.BOLD, 94));
tfResposta.setBackground(new Color(255, 204, 0));
tfResposta.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()== KeyEvent.VK_ENTER) {
if(tfResposta.getText().isEmpty() ) {
tfResposta.setText("Digite sua resposta!");
tfResposta.setFont(new Font("Tahoma", Font.ITALIC, 16));
}else {
btnTestar.doClick();
}
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if(tfResposta.getText().length()>5) {
arg0.consume();
}
}
});
tfResposta.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()== KeyEvent.VK_ESCAPE) {
frmTabuada.setBounds(100, 100, 900, 600);
frmTabuada.setLocationRelativeTo(null);
}
if(arg0.getKeyCode()== KeyEvent.VK_F) {
frmTabuada.setBounds(100, 100, 1612, 862);
frmTabuada.setLocationRelativeTo(null);
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if(tfResposta.getText().length()>5) {
arg0.consume();
}
}
});
tfResposta.setBounds(704, 533, 344, 59);
frmTabuada.getContentPane().add(tfResposta);
tfResposta.setColumns(10);
lblAcertos = new JLabel("0");
lblAcertos.setFont(new Font("Tahoma", Font.PLAIN, 32));
lblAcertos.setBounds(48, 566, 60, 39);
frmTabuada.getContentPane().add(lblAcertos);
lblErros = new JLabel("1");
lblErros.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblErros.setBounds(50, 682, 46, 39);
frmTabuada.getContentPane().add(lblErros);
frmTabuada.setResizable(false);
frmTabuada.setLocationRelativeTo(null);
btnTestar = new JButton("Testar");
btnTestar.setForeground(new Color(0, 0, 128));
btnTestar.setFont(new Font("Tahoma", Font.BOLD, 27));
btnTestar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evtn) {
if(respostaCorreta.equals(tfResposta.getText())) {
JOptionPane.showMessageDialog(null, "ACERTOU");
lblAcertos.setText(String.valueOf(Integer.parseInt(lblAcertos.getText())+1));
gerarValores();
}else {
JOptionPane.showMessageDialog(null, "<html><font color=red face=arial><i><b> Errou! \n Resposta = "+respostaCorreta);
lblErros.setText(String.valueOf(Integer.parseInt(lblErros.getText())+1));
gerarValores();
}
}
});
btnTestar.setBackground(new Color(255, 165, 0));
btnTestar.setBounds(1058, 527, 170, 71);
frmTabuada.getContentPane().add(btnTestar);
lblErros_1 = new JLabel("Erros");
lblErros_1.setToolTipText("acertos");
lblErros_1.setFont(new Font("Tahoma", Font.PLAIN, 32));
lblErros_1.setBounds(44, 616, 119, 39);
frmTabuada.getContentPane().add(lblErros_1);
lblAcertos_1 = new JLabel("Acertos");
lblAcertos_1.setToolTipText("acertos");
lblAcertos_1.setFont(new Font("Segoe UI", Font.PLAIN, 32));
lblAcertos_1.setBounds(44, 503, 119, 39);
frmTabuada.getContentPane().add(lblAcertos_1);
btnPular = new JButton("PULAR");
btnPular.setForeground(new Color(255, 255, 0));
btnPular.setBackground(new Color(0, 102, 0));
btnPular.setFont(new Font("Tahoma", Font.PLAIN, 30));
btnPular.setBounds(1058, 628, 165, 59);
frmTabuada.getContentPane().add(btnPular);
btnPular.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gerarValores();
}
});
btnVoltar = new JButton("Voltar");
btnVoltar.setBackground(new Color(255, 0, 0));
btnVoltar.setFont(new Font("Tahoma", Font.PLAIN, 34));
btnVoltar.setBounds(53, 37, 184, 59);
frmTabuada.getContentPane().add(btnVoltar);
btnVoltar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i) {
tabuadas.main(null);
frmTabuada.dispose();
}
});
btnSair = new JButton("Ir para o menu ");
btnSair.setForeground(Color.BLUE);
btnSair.setFont(new Font("Tahoma", Font.PLAIN, 30));
btnSair.setBackground(new Color(255, 140, 0));
btnSair.setBounds(1082, 11, 269, 59);
frmTabuada.getContentPane().add(btnSair);
JLabel lblPular = new JLabel("Pulou");
lblPular.setFont(new Font("Tahoma", Font.PLAIN, 29));
lblPular.setBounds(216, 510, 121, 29);
frmTabuada.getContentPane().add(lblPular);
JLabel lblPular_1l = new JLabel("0");
lblPular_1l.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblPular_1l.setBounds(230, 575, 46, 27);
frmTabuada.getContentPane().add(lblPular_1l);
btnPular.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
gerarValores ();
lblPular_1l.setText(String.valueOf(Integer.parseInt(lblPular_1l.getText())+1));
}
});
btnSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i) {
int Sair = JOptionPane.showConfirmDialog(null, "Tem certeza que quer sair do jogo ?");
if(Sair==JOptionPane.YES_OPTION) {
Menu.main(null);
frmTabuada.dispose();
}
if(Sair==JOptionPane.NO_OPTION) {
gerarValores();
}
}
});
gerarValores();
}
public void gerarValores() {
tfResposta.setText(null);
Random r = new Random(); //para gerar valores aleatórios
n1 = r.nextInt(1)+1; //valores de 1 até 10
n2 = r.nextInt(10); //valores de 1 até 10
operacao = r.nextInt(1)+1; //valores de 1 até 5
//operacao =2 -
oculto = r.nextInt(2)+1;
if(operacao == 1) {
lblOperacao.setText("x");
operacao2 = "x";
resposta = n1*n2;
}
if(oculto==1) {
lblN1.setText(String.valueOf(n1));
lblN2.setVisible(false);
lblResposta.setText(String.valueOf(resposta).replace('.', ','));
respostaCorreta = String.valueOf(n2);
tfResposta.setBounds(626, 218, 170, 198);
}else {
lblN2.setVisible(true);
}
if(oculto==2) {
lblN1.setText(String.valueOf(n1));
lblN2.setText(String.valueOf(n2));
lblResposta.setVisible(false);
tfResposta.setBounds(1058, 232, 158, 170);
int rr = (int)resposta;
respostaCorreta = String.valueOf(rr);
} else {
lblResposta.setVisible(true);
}
tfResposta.requestFocus();
System.out.println("Erros : "+lblErros.getText());;
}
}