Boa noite pessoal. Estou desenvolvendo um projeto desktop utilizando o NetBeans 11.3. Eu crio os Frames, coloco os campos, os botões, menus e tudo funciona normal quando eu ponho para executar o projeto abrindo o frame de inicio. Porém a hora que eu coloco uma imagem de ícone de um Label para utilizar como background, ou coloco um ícone nos botões, ou em qualquer parte do projeto, ele simplesmente não abre o frame. Poderiam me ajudar?
Traga alguma imagem do frame ou algum código. Não temos ideia do que fez.
Por exemplo: esta é meu frame de login. Quando eu adiciono uma label, apago o seu texto e coloco um icon, o frame simplesmente não abre ao executar.
package telas;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButtonEntrar = new javax.swing.JButton();
jLabelSenha = new javax.swing.JLabel();
jLabelUsuario = new javax.swing.JLabel();
jTextFieldUsuario = new javax.swing.JTextField();
jPasswordFieldSenha = new javax.swing.JPasswordField();
jButtonSair = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Von Ahn Joias e Acessórios");
setResizable(false);
getContentPane().setLayout(null);
jButtonEntrar.setText("Entrar");
jButtonEntrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonEntrarActionPerformed(evt);
}
});
getContentPane().add(jButtonEntrar);
jButtonEntrar.setBounds(120, 300, 63, 23);
jLabelSenha.setText("Senha:");
getContentPane().add(jLabelSenha);
jLabelSenha.setBounds(80, 250, 50, 30);
jLabelUsuario.setText("Usuário:");
getContentPane().add(jLabelUsuario);
jLabelUsuario.setBounds(80, 210, 50, 30);
jTextFieldUsuario.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldUsuarioActionPerformed(evt);
}
});
getContentPane().add(jTextFieldUsuario);
jTextFieldUsuario.setBounds(130, 210, 137, 30);
getContentPane().add(jPasswordFieldSenha);
jPasswordFieldSenha.setBounds(130, 250, 137, 30);
jButtonSair.setText("Sair");
jButtonSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSairActionPerformed(evt);
}
});
getContentPane().add(jButtonSair);
jButtonSair.setBounds(203, 300, 70, 23);
setSize(new java.awt.Dimension(416, 439));
setLocationRelativeTo(null);
}// </editor-fold>
private void jTextFieldUsuarioActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButtonEntrarActionPerformed(java.awt.event.ActionEvent evt) {
if(jTextFieldUsuario.getText().equals("klismann")&&jPasswordFieldSenha.getText().equals("123")){
inicio inicio = new inicio(); //Aqui instanciando e chamando a tela inicio
inicio.setVisible(true);
dispose(); //Este comando fechará a tela de login
JOptionPane.showMessageDialog(rootPane, "Bem-vindo(a).");
}else{
JOptionPane.showMessageDialog(rootPane, "Senha ou usuários inválidos.");
}
}
private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
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(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonEntrar;
private javax.swing.JButton jButtonSair;
private javax.swing.JLabel jLabelSenha;
private javax.swing.JLabel jLabelUsuario;
private javax.swing.JPasswordField jPasswordFieldSenha;
private javax.swing.JTextField jTextFieldUsuario;
// End of variables declaration
}
Repliquei parte de seu código no Netbeans 11.3 e funcionou sem problemas.
Observei que no seu método initComponents() está diferente.
jLabelUsuario = new javax.swing.JLabel();
jLabelSenha = new javax.swing.JLabel();
jTextFieldUsuario = new javax.swing.JTextField();
jPasswordFieldSenha = new javax.swing.JPasswordField();
jButtonEntrar = new javax.swing.JButton();
jButtonSair = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabelUsuario.setText("jLabel2");
jLabelSenha.setText("jLabel3");
jTextFieldUsuario.setText("jTextField1");
jPasswordFieldSenha.setText("jPasswordField1");
jButtonEntrar.setText("jButton1");
jButtonSair.setText("jButton2");
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/java_32.png"))); // NOI18N
Veja que no Label com a imagem inserida a linha aparece no meu código o que não ocorre no seu exemplo de login.
Minha sugestão é refazer o Frame do zero.