Olá pesoal, primeiramente gostaria de falar que sou novo no campo da programação. Fui fazer um projeto com Interface Gráfica usando FormJPanel, porém aparece um “erro” que me impede de executar o arquivo. Procurei e apliquei diversas recomendações obtidas de sites, porém nada solucionou o problema. Já criei outros projetos com JPanel e todos aparecem a mesma mensagem, diferentemente de quando faço um projeto Java normal, este executa normalmente. Se alguém puder me ajudar eu ficaria extremamente grato, abaixo estou deixando o print da mensagem, abraço.
Nenhuma classe principal localizada
13 Respostas
uma aplicação começa sempre pela classe Principal, e esta classe deve conter o método main
public static void main(String[] args) {
}
de posse disse você tem que apontar no projeto qual classe deve ser iniciada primeiro.
não é “Classe” main amigo, é “método” main,
dê uma olhada neste código deve ter sim, quando possui método main e não foi definido no projeto, o netBeans reconhece e pergunta. depois ele toma essa classe sempre como base.
se olhar embaixo da foto vai ver oque estou te falando, bem acima da linha vermelha de carregamento do video
bom, ele usou o comando
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
utilizei no meu projeto, porém agora não abre painel do meu projeto… sabes oq poderia ser?
baixei também todo o projeto do video em questão, porém a tela também não está abrindo…
poste todo o código pra mim verificar
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package aula7;
import javax.swing.JOptionPane;
/**
*
-
@author Gilberto Toledo
*/
public class Principal extends javax.swing.JFrame {/**
- Creates new form Principal
*/
public Principal() {
initComponents();
setLocationRelativeTo(null);
}
/**
-
This method is called from within the constructor to initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is always
-
regenerated by the Form Editor. */ @SuppressWarnings(“unchecked”) // private void initComponents() {
grupo_sexo = new javax.swing.ButtonGroup(); t_nome = new javax.swing.JLabel(); btn_enviar = new javax.swing.JButton(); c_nome = new javax.swing.JTextField(); chb_idade = new javax.swing.JCheckBox(); rb_masc = new javax.swing.JRadioButton(); rb_fem = new javax.swing.JRadioButton(); jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
t_nome.setText(“Nome:”);btn_enviar.setText(“Enviar”); btn_enviar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_enviarActionPerformed(evt); } });
chb_idade.setText(“Maior de idade”);grupo_sexo.add(rb_masc);
rb_masc.setText(“Masculino”);grupo_sexo.add(rb_fem);
rb_fem.setText(“Feminino”);jLabel1.setText(“Sexo:”);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(139, 139, 139) .addComponent(btn_enviar)) .addGroup(layout.createSequentialGroup() .addGap(42, 42, 42) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(t_nome) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(rb_masc) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(rb_fem)) .addComponent(c_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(chb_idade)))) .addContainerGap(87, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(25, 25, 25) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(t_nome) .addComponent(c_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(chb_idade) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(rb_masc) .addComponent(rb_fem) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addComponent(btn_enviar) .addContainerGap()) );
pack();
}//
private void btn_enviarActionPerformed(java.awt.event.ActionEvent evt) { String Idade = “”; if(chb_idade.isSelected()) Idade = “Você é maior de idade”; else Idade = “Você é menor de idade”;
String sexo = ""; if(rb_masc.isSelected()) sexo = "Masculino"; else if(rb_fem.isSelected()) sexo = "Feminino"; else sexo = "Não informado"; JOptionPane.showMessageDialog(rootPane,"Seu nome é "+c_nome.getText()+" e "+Idade+" e sexo "+sexo,"Alerta",JOptionPane.INFORMATION_MESSAGE);
}
/**
-
@param args the command line arguments
/
public static void main(String args[]) {
/ Set the Nimbus look and feel /
//
/ 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(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Principal().setVisible(true); } }); }
- For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
// Variables declaration - do not modify private javax.swing.JButton btn_enviar; private javax.swing.JTextField c_nome; private javax.swing.JCheckBox chb_idade; private javax.swing.ButtonGroup grupo_sexo; private javax.swing.JLabel jLabel1; private javax.swing.JRadioButton rb_fem; private javax.swing.JRadioButton rb_masc; private javax.swing.JLabel t_nome; // End of variables declaration }
- Creates new form Principal
aqui rodou beleza, experimenta clicar com o botão direito em cima do arquivo da classe, e então vá em executar arquivo
eu atualizei meu java, não sei se teve algo a ver, e agora funcionou o do site, porém o meu projeto continua com o mesmo problema, se não for pedir muito, poderia dar uma olhada e testar para ver se pelo menos abre o painel ai?
Aqui está o código (deve haver algum erro terrível de funcionamento caso seja selecionado as teclas, entretanto, como disse anteriormente, estou aprendendo hehe)
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
package testeinterface;
import javax.swing.JOptionPane;
import javax.swing.RootPaneContainer;
/**
*
-
@author Douglas
*/
public class Principal extends javax.swing.JPanel {int num1 =0; int num2 =0; char operacao; double resultado; /**
- Creates new form calculadora
*/
public Principal() {
initComponents();
}
/**
-
This method is called from within the constructor to initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is always
-
regenerated by the Form Editor. */ @SuppressWarnings(“unchecked”) // private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); btn_calcular = new javax.swing.JButton(); btn_n2 = new javax.swing.JButton(); btn_n1 = new javax.swing.JButton(); btn_n4 = new javax.swing.JButton(); btn_n3 = new javax.swing.JButton(); btn_n6 = new javax.swing.JButton(); btn_n5 = new javax.swing.JButton(); btn_n8 = new javax.swing.JButton(); btn_n7 = new javax.swing.JButton(); btn_n0 = new javax.swing.JButton(); btn_n9 = new javax.swing.JButton(); btn_multiplicar = new javax.swing.JButton(); btn_dividir = new javax.swing.JButton(); btn_subtrair = new javax.swing.JButton(); btn_somar = new javax.swing.JButton();
jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { “Title 1”, “Title 2”, “Title 3”, “Title 4” } )); jScrollPane1.setViewportView(jTable1);
btn_calcular.setText(“Calcular”); btn_calcular.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_calcularActionPerformed(evt); } });
btn_n2.setText(“2”); btn_n2.setAlignmentY(0.0F); btn_n2.setBorder(null); btn_n2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n2ActionPerformed(evt); } });
btn_n1.setText(“1”); btn_n1.setAlignmentY(0.0F); btn_n1.setBorder(null); btn_n1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n1ActionPerformed(evt); } });
btn_n4.setText(“4”); btn_n4.setAlignmentY(0.0F); btn_n4.setBorder(null); btn_n4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n4ActionPerformed(evt); } });
btn_n3.setText(“3”); btn_n3.setAlignmentY(0.0F); btn_n3.setBorder(null); btn_n3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n3ActionPerformed(evt); } });
btn_n6.setText(“6”); btn_n6.setAlignmentY(0.0F); btn_n6.setBorder(null); btn_n6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n6ActionPerformed(evt); } });
btn_n5.setText(“5”); btn_n5.setAlignmentY(0.0F); btn_n5.setBorder(null); btn_n5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n5ActionPerformed(evt); } });
btn_n8.setText(“8”); btn_n8.setAlignmentY(0.0F); btn_n8.setBorder(null); btn_n8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n8ActionPerformed(evt); } });
btn_n7.setText(“7”); btn_n7.setAlignmentY(0.0F); btn_n7.setBorder(null); btn_n7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n7ActionPerformed(evt); } });
btn_n0.setText(“0”); btn_n0.setAlignmentY(0.0F); btn_n0.setBorder(null); btn_n0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n0ActionPerformed(evt); } });
btn_n9.setText(“9”); btn_n9.setAlignmentY(0.0F); btn_n9.setBorder(null); btn_n9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_n9ActionPerformed(evt); } });
btn_multiplicar.setText("*"); btn_multiplicar.setAlignmentY(0.0F); btn_multiplicar.setBorder(null); btn_multiplicar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_multiplicarActionPerformed(evt); } });
btn_dividir.setText("/"); btn_dividir.setAlignmentY(0.0F); btn_dividir.setBorder(null); btn_dividir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_dividirActionPerformed(evt); } });
btn_subtrair.setText("-"); btn_subtrair.setAlignmentY(0.0F); btn_subtrair.setBorder(null); btn_subtrair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_subtrairActionPerformed(evt); } });
btn_somar.setText("+"); btn_somar.setAlignmentY(0.0F); btn_somar.setBorder(null); btn_somar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_somarActionPerformed(evt); } });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btn_n1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btn_n2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(btn_n7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btn_n8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(btn_n4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btn_n5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(btn_n0, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn_n9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn_n3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn_subtrair, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_multiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_dividir, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(btn_somar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(btn_calcular)) .addContainerGap(35, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_n2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_n4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_n8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_n9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_somar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btn_n0, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_subtrair, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addComponent(btn_multiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btn_dividir, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE) .addComponent(btn_calcular) .addContainerGap()) ); }//
private void btn_n2ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n2.isSelected()) { if (num1 == 0) { num1 = 2; } else if (num2 == 0) { num2 = 2; } } }
private void btn_n1ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n1.isSelected()) { if (num1 == 0) { num1 = 1; } else if (num2 == 0) { num2 = 1; } } }
private void btn_n4ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n4.isSelected()) { if (num1 == 0) { num1 = 4; } else if (num2 == 0) { num2 = 4; } } }
private void btn_n3ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n3.isSelected()) { if (num1 == 0) { num1 = 3; } else if (num2 == 0) { num2 = 3; } } }
private void btn_n6ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n6.isSelected()) { if (num1 == 0) { num1 = 6; } else if (num2 == 0) { num2 = 6; } } }
private void btn_n5ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n5.isSelected()) { if (num1 == 0) { num1 = 5; } else if (num2 == 0) { num2 = 5; } } }
private void btn_n8ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n8.isSelected()) { if (num1 == 0) { num1 = 8; } else if (num2 == 0) { num2 = 8; } } }
private void btn_n7ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n7.isSelected()) { if (num1 == 0) { num1 = 7; } else if (num2 == 0) { num2 = 7; } } }
private void btn_n0ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n0.isSelected()) { if (num1 == 0) { num1 = 0; } else if (num2 == 0) { num2 = 0; } } }
private void btn_n9ActionPerformed(java.awt.event.ActionEvent evt) { if (btn_n9.isSelected()) { if (num1 == 0) { num1 = 9; } else if (num2 == 0) { num2 = 9; } } }
private void btn_multiplicarActionPerformed(java.awt.event.ActionEvent evt) { if (operacao == ‘\0’) { operacao = 42; } else { JOptionPane.showMessageDialog(null, “Operação matemática já foi escolhida.”); } }
private void btn_dividirActionPerformed(java.awt.event.ActionEvent evt) { if (operacao == ‘\0’) { operacao = 47; } else { JOptionPane.showMessageDialog(null, “Operação matemática já foi escolhida.”); } }
private void btn_subtrairActionPerformed(java.awt.event.ActionEvent evt) { if (operacao == ‘\0’) { operacao = 45; } else { JOptionPane.showMessageDialog(null, “Operação matemática já foi escolhida.”); } }
private void btn_somarActionPerformed(java.awt.event.ActionEvent evt) { if (operacao == ‘\0’) { operacao = 43; } else { JOptionPane.showMessageDialog(null, “Operação matemática já foi escolhida.”); } }
private void btn_calcularActionPerformed(java.awt.event.ActionEvent evt) { if (num1 != 0 && num2 !=0 && operacao != ‘\0’){ resultado = num1+operacao+num2; } JOptionPane.showMessageDialog(null, resultado); }
- Creates new form calculadora
public static void main(String args[]) {
/* Set the Nimbus look and feel <em>/
//
/</em> If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see <a href="http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html">http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html</a>
*/
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(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_calcular;
private javax.swing.JButton btn_dividir;
private javax.swing.JButton btn_multiplicar;
private javax.swing.JButton btn_n0;
private javax.swing.JButton btn_n1;
private javax.swing.JButton btn_n2;
private javax.swing.JButton btn_n3;
private javax.swing.JButton btn_n4;
private javax.swing.JButton btn_n5;
private javax.swing.JButton btn_n6;
private javax.swing.JButton btn_n7;
private javax.swing.JButton btn_n8;
private javax.swing.JButton btn_n9;
private javax.swing.JButton btn_somar;
private javax.swing.JButton btn_subtrair;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
Olha não foi facil hehehe
tinha e tem diversos erros, sei que está começando, mas não desista,
abaixo código só defina o tamanho que quiser do form, e ainda tem uns erros que voce vai ter que corrigir, abraço
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JOptionPane;
/**
*
* @author Douglas
*/
public class Principal extends javax.swing.JFrame {
int num1 = 0;
int num2 = 0;
char operacao;
double resultado;
public Principal() {
initComponents();
setVisible(true);
setLocationRelativeTo(null);
this.setSize(300, 400 );
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
btn_calcular = new javax.swing.JButton();
btn_n2 = new javax.swing.JButton();
btn_n1 = new javax.swing.JButton();
btn_n4 = new javax.swing.JButton();
btn_n3 = new javax.swing.JButton();
btn_n6 = new javax.swing.JButton();
btn_n5 = new javax.swing.JButton();
btn_n8 = new javax.swing.JButton();
btn_n7 = new javax.swing.JButton();
btn_n0 = new javax.swing.JButton();
btn_n9 = new javax.swing.JButton();
btn_multiplicar = new javax.swing.JButton();
btn_dividir = new javax.swing.JButton();
btn_subtrair = new javax.swing.JButton();
btn_somar = new javax.swing.JButton();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
btn_calcular.setText("Calcular");
btn_calcular.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_calcularActionPerformed(evt);
}
});
btn_n2.setText("2");
btn_n2.setAlignmentY(0.0F);
btn_n2.setBorder(null);
btn_n2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n2ActionPerformed(evt);
}
});
btn_n1.setText("1");
btn_n1.setAlignmentY(0.0F);
btn_n1.setBorder(null);
btn_n1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n1ActionPerformed(evt);
}
});
btn_n4.setText("4");
btn_n4.setAlignmentY(0.0F);
btn_n4.setBorder(null);
btn_n4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n4ActionPerformed(evt);
}
});
btn_n3.setText("3");
btn_n3.setAlignmentY(0.0F);
btn_n3.setBorder(null);
btn_n3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n3ActionPerformed(evt);
}
});
btn_n6.setText("6");
btn_n6.setAlignmentY(0.0F);
btn_n6.setBorder(null);
btn_n6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n6ActionPerformed(evt);
}
});
btn_n5.setText("5");
btn_n5.setAlignmentY(0.0F);
btn_n5.setBorder(null);
btn_n5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n5ActionPerformed(evt);
}
});
btn_n8.setText("8");
btn_n8.setAlignmentY(0.0F);
btn_n8.setBorder(null);
btn_n8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n8ActionPerformed(evt);
}
});
btn_n7.setText("7");
btn_n7.setAlignmentY(0.0F);
btn_n7.setBorder(null);
btn_n7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n7ActionPerformed(evt);
}
});
btn_n0.setText("0");
btn_n0.setAlignmentY(0.0F);
btn_n0.setBorder(null);
btn_n0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n0ActionPerformed(evt);
}
});
btn_n9.setText("9");
btn_n9.setAlignmentY(0.0F);
btn_n9.setBorder(null);
btn_n9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_n9ActionPerformed(evt);
}
});
btn_multiplicar.setText("*");
btn_multiplicar.setAlignmentY(0.0F);
btn_multiplicar.setBorder(null);
btn_multiplicar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_multiplicarActionPerformed(evt);
}
});
btn_dividir.setText("/");
btn_dividir.setAlignmentY(0.0F);
btn_dividir.setBorder(null);
btn_dividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_dividirActionPerformed(evt);
}
});
btn_subtrair.setText("-");
btn_subtrair.setAlignmentY(0.0F);
btn_subtrair.setBorder(null);
btn_subtrair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_subtrairActionPerformed(evt);
}
});
btn_somar.setText("+");
btn_somar.setAlignmentY(0.0F);
btn_somar.setBorder(null);
btn_somar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_somarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btn_n1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_n2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(btn_n7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_n8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(btn_n4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_n5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btn_n0, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_n9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(1, 1, 1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_n3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn_subtrair, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_multiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_dividir, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(btn_somar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addComponent(btn_calcular))
.addContainerGap(35, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_n2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_n4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_n8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_n9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_somar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_n0, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_subtrair, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(btn_multiplicar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_dividir, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addComponent(btn_calcular)
.addContainerGap())
);
}// </editor-fold>
private void btn_n2ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n2.isSelected()) {
if (num1 == 0) {
num1 = 2;
} else if (num2 == 0) {
num2 = 2;
}
}
}
private void btn_n1ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n1.isSelected()) {
if (num1 == 0) {
num1 = 1;
} else if (num2 == 0) {
num2 = 1;
}
}
}
private void btn_n4ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n4.isSelected()) {
if (num1 == 0) {
num1 = 4;
} else if (num2 == 0) {
num2 = 4;
}
}
}
private void btn_n3ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n3.isSelected()) {
if (num1 == 0) {
num1 = 3;
} else if (num2 == 0) {
num2 = 3;
}
}
}
private void btn_n6ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n6.isSelected()) {
if (num1 == 0) {
num1 = 6;
} else if (num2 == 0) {
num2 = 6;
}
}
}
private void btn_n5ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n5.isSelected()) {
if (num1 == 0) {
num1 = 5;
} else if (num2 == 0) {
num2 = 5;
}
}
}
private void btn_n8ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n8.isSelected()) {
if (num1 == 0) {
num1 = 8;
} else if (num2 == 0) {
num2 = 8;
}
}
}
private void btn_n7ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n7.isSelected()) {
if (num1 == 0) {
num1 = 7;
} else if (num2 == 0) {
num2 = 7;
}
}
}
private void btn_n0ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n0.isSelected()) {
if (num1 == 0) {
num1 = 0;
} else if (num2 == 0) {
num2 = 0;
}
}
}
private void btn_n9ActionPerformed(java.awt.event.ActionEvent evt) {
if (btn_n9.isSelected()) {
if (num1 == 0) {
num1 = 9;
} else if (num2 == 0) {
num2 = 9;
}
}
}
private void btn_multiplicarActionPerformed(java.awt.event.ActionEvent evt) {
if (operacao == '\0') {
operacao = 42;
} else {
JOptionPane.showMessageDialog(null, "Operação matemática já foi escolhida.");
}
}
private void btn_dividirActionPerformed(java.awt.event.ActionEvent evt) {
if (operacao == '\0') {
operacao = 47;
} else {
JOptionPane.showMessageDialog(null, "Operação matemática já foi escolhida.");
}
}
private void btn_subtrairActionPerformed(java.awt.event.ActionEvent evt) {
if (operacao == '\0') {
operacao = 45;
} else {
JOptionPane.showMessageDialog(null, "Operação matemática já foi escolhida.");
}
}
private void btn_somarActionPerformed(java.awt.event.ActionEvent evt) {
if (operacao == '\0') {
operacao = 43;
} else {
JOptionPane.showMessageDialog(null, "Operação matemática já foi escolhida.");
}
}
private void btn_calcularActionPerformed(java.awt.event.ActionEvent evt) {
if (num1 != 0 && num2 != 0 && operacao != '\0') {
resultado = num1 + operacao + num2;
}
JOptionPane.showMessageDialog(null, resultado);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (InstantiationException ex) {
// java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (javax.swing.UnsupportedLookAndFeelException ex) {
// java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// }
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_calcular;
private javax.swing.JButton btn_dividir;
private javax.swing.JButton btn_multiplicar;
private javax.swing.JButton btn_n0;
private javax.swing.JButton btn_n1;
private javax.swing.JButton btn_n2;
private javax.swing.JButton btn_n3;
private javax.swing.JButton btn_n4;
private javax.swing.JButton btn_n5;
private javax.swing.JButton btn_n6;
private javax.swing.JButton btn_n7;
private javax.swing.JButton btn_n8;
private javax.swing.JButton btn_n9;
private javax.swing.JButton btn_somar;
private javax.swing.JButton btn_subtrair;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
po, muito obrigado mesmo, no fim acabei vendo as cagadas que eu fiz e consegui arrumar uma grande parte, outras nem tanto kk muito obrigado mesmo, abraço.


