Pessoal, boa noite.
Estou com dificuldades de implementar o método FocusListener(). No campo Nome da minha janela tem que ficar com a cor de fundo amarela assim que eu passo o cursor do mouse em cima do campo e ao tirar o cursor volta na cor branca.
Utilizei estes métodos abaixo mais mesmo assim o meu programa está com erro alguém poderia me ajudar por favor.
Muito Obrigada.
[code]
package lab09;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JSeparator;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import javax.swing.JOptionPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
/**
*
* @author 30955475
*/
public class janela extends JFrame {
private JLabel jlAgencia, jlConta, jlNome, jlEndereco, jlTelefone, jlCpf;
private JTextField jtfAgencia, jtfConta, jtfNome, jtfEndereco, jtfTelefone, jtfCpf;
private JSeparator jSeparator01, jSeparator02;
private JRadioButton jrbCorrente, jrbPoupanca;
private ButtonGroup bgContas;
private JButton jbConsultar, jbAtualizar, jbFechar;
public janela() {
setSize(400, 255);
setTitle("Linguagem de Programacao II");
centralizar();
setResizable(false);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent evt) {
janelaWindowOpened();
}
@Override
public void windowClosing(WindowEvent evt) {
janelaWindowClosing();
}
});
jtfNome.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent evt) {
jtfNome.setBackground(Color.YELLOW);
}
});
jlAgencia = new JLabel();
jlAgencia.setText("Codigo da Agencia:");
jlAgencia.setSize(110, 18);
jlAgencia.setLocation(10, 10);
add(jlAgencia);
jtfAgencia = new JTextField();
jtfAgencia.setSize(50, 20);
jtfAgencia.setLocation(120, 10);
add(jtfAgencia);
jlConta = new JLabel();
jlConta.setText("Numero da Conta:");
jlConta.setSize(105, 18);
jlConta.setLocation(205, 10);
add(jlConta);
jtfConta = new JTextField();
jtfConta.setSize(60, 20);
jtfConta.setLocation(315, 10);
add(jtfConta);jtfNome.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfNome.setBackground(Color.YELLOW);
}
});
jSeparator01 = new JSeparator();
jSeparator01.setSize(365, 10);
jSeparator01.setLocation(10, 40);
add(jSeparator01);
jlNome = new JLabel();
jlNome.setText("Nome:");
jlNome.setSize(60, 18);
jlNome.setLocation(10, 50);
jlNome.setHorizontalAlignment(SwingConstants.RIGHT);
add(jlNome);
jtfNome = new JTextField();
jtfNome.setSize(300, 20);
jtfNome.setLocation(75, 50);
add(jtfNome);
jlEndereco = new JLabel();
jlEndereco.setText("Endereco:");
jlEndereco.setSize(60, 18);
jlEndereco.setLocation(10, 75);
jlEndereco.setHorizontalAlignment(SwingConstants.RIGHT);
add(jlEndereco);
jtfEndereco = new JTextField();
jtfEndereco.setSize(300, 20);
jtfEndereco.setLocation(75, 75);
add(jtfEndereco);
jlTelefone = new JLabel();
jlTelefone.setText("Telefone:");
jlTelefone.setSize(60, 18);
jlTelefone.setLocation(10, 100);
jlTelefone.setHorizontalAlignment(SwingConstants.RIGHT);
add(jlTelefone);
jtfTelefone = new JTextField();
jtfTelefone.setSize(300, 20);
jtfTelefone.setLocation(75, 100);
add(jtfTelefone);
jlCpf = new JLabel();
jlCpf.setText("CPF:");
jlCpf.setSize(60, 18);
jlCpf.setLocation(10, 125);
jlCpf.setHorizontalAlignment(SwingConstants.RIGHT);
add(jlCpf);
jtfCpf = new JTextField();
jtfCpf.setSize(300, 20);
jtfCpf.setLocation(75, 125);
add(jtfCpf);
jrbCorrente = new JRadioButton();
jrbCorrente.setText("Conta Corrente");
jrbCorrente.setSize(111, 20);
jrbCorrente.setLocation(100, 150);
jrbCorrente.setMnemonic('c');
jrbCorrente.setSelected(true);
add(jrbCorrente);
jrbPoupanca = new JRadioButton();
jrbPoupanca.setText("Conta Poupanca");
jrbPoupanca.setSize(118, 20);
jrbPoupanca.setLocation(225, 150);
jrbPoupanca.setMnemonic('p');
add(jrbPoupanca);
bgContas = new ButtonGroup();
bgContas.add(jrbCorrente);
bgContas.add(jrbPoupanca);
jSeparator02 = new JSeparator();
jSeparator02.setSize(365, 10);
jSeparator02.setLocation(10, 180);
add(jSeparator02);
jbConsultar = new JButton();
jbConsultar.setText("Consultar");
jbConsultar.setSize(100, 23);
jbConsultar.setLocation(35, 190);
jbConsultar.setMnemonic('s');
add(jbConsultar);
jbAtualizar = new JButton();
jbAtualizar.setText("Atualizar");
jbAtualizar.setSize(100, 23);
jbAtualizar.setLocation(145, 190);
jbAtualizar.setMnemonic('a');
jbAtualizar.setEnabled(false);
add(jbAtualizar);
jbFechar = new JButton();
jbFechar.setText("Fechar");
jbFechar.setSize(100, 23);
jbFechar.setLocation(255, 190);
jbFechar.setMnemonic('f');
add(jbFechar);
}
private void centralizar() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension janela = getSize();
if (janela.height > screen.height) {
setSize(janela.width, screen.height);
}
if (janela.width > screen.width) {
setSize(screen.width, janela.height);
}
setLocation((screen.width - janela.width) / 2,
(screen.height - janela.height) / 2);
}
private void janelaWindowOpened() {
JOptionPane.showMessageDialog(this, "Universidade Presbiteriana Mackenzie");
}
private void janelaWindowClosing(){
JOptionPane.showMessageDialog(this,"Obrigado por utilizar nosso sistema!");
}
public static void main(String[] args) {
janela janela = new janela();
janela.setVisible(true);
}
private void addFocusListener() {
throw new UnsupportedOperationException("Not yet implemented");
}
}[list]