Erro com JTextField
jTextField aumenta de tamanho conforme digita
como solucionar ?
Erro com JTextField
jTextField aumenta de tamanho conforme digita
como solucionar ?
o0
código?
nao éh nenhum erro de codigo… eu só criei o jtextfield, dai quando executo o programa e começo a preencher o campo ele aumenta o tamanho conforme vou digitando…
public JFrame_Login() {
initComponents();
setLocationRelativeTo(null);
this.setVisible(true);
this.setResizable(false);
this.setExtendedState(MAXIMIZED_BOTH);
DataHora();
txt_Usuario.requestFocus();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel_Principal = new javax.swing.JPanel();
jPanel_Corpo = new javax.swing.JPanel();
jPanel_Login = new javax.swing.JPanel();
jLabel_Usuario = new javax.swing.JLabel();
jLabel_Senha = new javax.swing.JLabel();
txt_Usuario = new javax.swing.JTextField();
jPasswordField_Senha = new javax.swing.JPasswordField();
jPanel_Rodape = new javax.swing.JPanel();
jPanel_DataHora = new javax.swing.JPanel();
jLabel_DataHora = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel_Principal.setLayout(new java.awt.GridBagLayout());
jPanel_Corpo.setBackground(new java.awt.Color(204, 204, 204));
jPanel_Corpo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel_Corpo.setLayout(new java.awt.GridBagLayout());
jPanel_Login.setLayout(new java.awt.GridBagLayout());
jLabel_Usuario.setFont(new java.awt.Font("Century", 0, 14)); // NOI18N
jLabel_Usuario.setText("Usuário:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel_Login.add(jLabel_Usuario, gridBagConstraints);
jLabel_Senha.setFont(new java.awt.Font("Century", 0, 14));
jLabel_Senha.setText("Senha:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel_Login.add(jLabel_Senha, gridBagConstraints);
txt_Usuario.setFont(new java.awt.Font("Century", 0, 14));
txt_Usuario.setToolTipText("Usuário");
txt_Usuario.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
txt_Usuario.setMaximumSize(new java.awt.Dimension(6, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 150;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel_Login.add(txt_Usuario, gridBagConstraints);
jPasswordField_Senha.setFont(new java.awt.Font("Century", 0, 14));
jPasswordField_Senha.setToolTipText("Senha");
jPasswordField_Senha.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
jPasswordField_Senha.setMaximumSize(new java.awt.Dimension(6, 23));
jPasswordField_Senha.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordField_SenhaKeyPressed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = 150;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel_Login.add(jPasswordField_Senha, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel_Corpo.add(jPanel_Login, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel_Principal.add(jPanel_Corpo, gridBagConstraints);
jPanel_Rodape.setBackground(new java.awt.Color(204, 204, 204));
jPanel_Rodape.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel_Rodape.setLayout(new java.awt.GridBagLayout());
jPanel_DataHora.setLayout(new java.awt.GridBagLayout());
jLabel_DataHora.setFont(new java.awt.Font("Century", 0, 14));
jLabel_DataHora.setText("Data Hora");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
jPanel_DataHora.add(jLabel_DataHora, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel_Rodape.add(jPanel_DataHora, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
jPanel_Principal.add(jPanel_Rodape, gridBagConstraints);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel_Principal, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel_Principal, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jPasswordField_SenhaKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
try {
Login();
} catch (SQLException ex) {
Logger.getLogger(JFrame_Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrame_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame_Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel_DataHora;
private javax.swing.JLabel jLabel_Senha;
private javax.swing.JLabel jLabel_Usuario;
private javax.swing.JPanel jPanel_Corpo;
private javax.swing.JPanel jPanel_DataHora;
private javax.swing.JPanel jPanel_Login;
private javax.swing.JPanel jPanel_Principal;
private javax.swing.JPanel jPanel_Rodape;
private javax.swing.JPasswordField jPasswordField_Senha;
private javax.swing.JTextField txt_Usuario;
// End of variables declaration
private void DataHora() {
Thread thread_Hr = new Thread(new ClockRunnable(), "Clock Thead");
thread_Hr.setDaemon(true);
thread_Hr.start();
}
private void Login() throws SQLException {
if (VerificaLogin()) {
usuarioLogado.setLogin(txt_Usuario.getText());
new JFrame_TelaInicial(usuarioLogado).show();
this.setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "USUÁRIO OU SENHA INVÁLIDA!");
LimpaCampos();
txt_Usuario.requestFocus();
}
}
private boolean VerificaLogin() throws SQLException {
Usuario usuario = new Usuario();
usuario.setLogin(txt_Usuario.getText());
usuario.setSenha(jPasswordField_Senha.getText());
usuarioLogado = usuario;
UsuarioDAO usuarioDAO = new UsuarioDAO();
return usuarioDAO.ValidacaoLogin(usuario);
}
private void LimpaCampos() {
txt_Usuario.setText("");
jPasswordField_Senha.setText("");
}
private class ClockRunnable implements Runnable {
@Override
public void run() {
try {
while (true) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setHora(new Date());
}
});
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
private void setHora(Date date) {
jLabel_DataHora.setText(sdf.format(date));
}
}
Coloque o seu código entre as tags [code], por favor, para facilitar a leitura e te ajudarmos.
Você parece estar usando GridBagLayout. Experimente modificar as restrições (se estiver com o NB em inglês, “constraints”) do JTextField cujo tamanho quer manter fixo. O conselho mais extremo seria parar de usar GridBagLayout e mudar para outro gerenciador de layout.
Esse comportamento que você está descrevendo geralmente acontece quando o text field não possui um número de colunas configuradas.
Especifique um número maior que zero (e sem exageros) de colunas usando o método setColumns que deve resolver.
Obrigado Erick…
A coluna esta com valor 0.
Mudei para 1 e tudo se resolveu…
Thanks…