3
I have this mistake here:
run:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Win32GraphicsConfig.java:222)
at java.awt.Window.init(Window.java:505)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:189)
at Main.Interface.<init>(Interface.java:19)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
at Main.Interface.<init>(Interface.java:17)
at Main.AçaoDasTeclas$AçãoDosBotoes.<init>(AçaoDasTeclas.java:82)
at Main.AçaoDasTeclas.<init>(AçaoDasTeclas.java:24)
Class 1:
package Main;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
public class Interface extends javax.swing.JFrame {
char operacao;
double num1, num2, resultado;
String numant, temp, copiar;
AçaoDasTeclas adt = new AçaoDasTeclas();
public Interface() {
initComponents();
adt.AçõesDoTeclado(Painel);
}
/**
* 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() {
Painel = new javax.swing.JPanel();
Op = new javax.swing.JTextField();
Display = new javax.swing.JTextField();
BT0 = new javax.swing.JButton();
BT1 = new javax.swing.JButton();
BT2 = new javax.swing.JButton();
BT3 = new javax.swing.JButton();
BT4 = new javax.swing.JButton();
BT5 = new javax.swing.JButton();
BT6 = new javax.swing.JButton();
BT7 = new javax.swing.JButton();
BT8 = new javax.swing.JButton();
BT9 = new javax.swing.JButton();
BTSomar = new javax.swing.JButton();
BTDiminuir = new javax.swing.JButton();
BTMultiplicar = new javax.swing.JButton();
BTDividir = new javax.swing.JButton();
BTLimpar = new javax.swing.JButton();
BTce = new javax.swing.JButton();
BTPonto = new javax.swing.JButton();
BTraiz = new javax.swing.JButton();
BTtrocar = new javax.swing.JButton();
BTIgual = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu1 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora");
setResizable(false);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
Op.setEditable(false);
Op.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
Op.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
Display.setEditable(false);
Display.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
Display.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
Display.setFocusable(false);
BT0.setText("0");
BT0.setFocusable(false);
BT0.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
BT0.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
BT0.addActionListener(adt.ActBT0);
BT1.setText("1");
BT1.addActionListener(adt.ActBT1);
BT2.setText("2");
BT2.addActionListener(adt.ActBT2);
BT3.setText("3");
BT3.addActionListener(adt.ActBT3);
BT4.setText("4");
BT4.addActionListener(adt.ActBT4);
BT5.setText("5");
BT5.addActionListener(adt.ActBT5);
BT6.setText("6");
BT6.addActionListener(adt.ActBT6);
BT7.setText("7");
BT7.addActionListener(adt.ActBT7);
BT8.setText("8");
BT8.addActionListener(adt.ActBT8);
BT9.setText("9");
BT9.addActionListener(adt.ActBT9);
BTSomar.setText("+");
BTSomar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTSomarActionPerformed(evt);
}
});
BTDiminuir.setText("-");
BTDiminuir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTDiminuirActionPerformed(evt);
}
});
BTMultiplicar.setText("*");
BTMultiplicar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTMultiplicarActionPerformed(evt);
}
});
BTDividir.setText("/");
BTDividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTDividirActionPerformed(evt);
}
});
BTLimpar.setText("C");
BTLimpar.setToolTipText("Limpar visor.");
BTLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTLimparActionPerformed(evt);
}
});
BTce.setText("CE");
BTce.setToolTipText("Apagar a ultima operação feita.");
BTce.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTceActionPerformed(evt);
}
});
BTPonto.setText(".");
BTPonto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTPontoActionPerformed(evt);
}
});
BTraiz.setText("√¯");
BTraiz.setToolTipText("Raiz quadrada.");
BTraiz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTraizActionPerformed(evt);
}
});
BTtrocar.setText("-\\+");
BTtrocar.setToolTipText("Trocar o sinal do número.");
BTtrocar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTtrocarActionPerformed(evt);
}
});
BTIgual.setText("=");
BTIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BTIgualActionPerformed(evt);
}
});
jMenu2.setMnemonic('e');
jMenu2.setText("Editar");
jMenuItem1.setMnemonic('c');
jMenuItem1.setText("Copiar");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuItem2.setMnemonic('v');
jMenuItem2.setText("Colar");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
jMenu1.setText("Sobre");
jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jMenu1MouseClicked(evt);
}
});
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void SOp(String op) {
Op.setText(Display.getText() + " " + op);
}
private void ResultOp(char op) throws NumberFormatException {
operacao = op;
num1 = Double.parseDouble(Display.getText());
Display.setText(null);
}
private void BTPontoActionPerformed(java.awt.event.ActionEvent evt) {
if("".equals(Display.getText())) {
Display.setText("0.");
} else {
Display.setText(Display.getText() + ".");
}
}
private void BTSomarActionPerformed(java.awt.event.ActionEvent evt) {
SOp("+");
ResultOp('+');
}
private void BTIgualActionPerformed(java.awt.event.ActionEvent evt) {
Op.setText(null);
num2 = Double.parseDouble(Display.getText());
if(operacao == '+'){
resultado = num1 + num2;
}
else if(operacao == '-'){
resultado = num1 - num2;
}
else if(operacao == '*'){
resultado = num1 * num2;
}
else{
resultado = num1 / num2;
}
NumberFormat.getNumberInstance().format(resultado);
Display.setText(Double.toString(resultado));
}
private void BTDiminuirActionPerformed(java.awt.event.ActionEvent evt) {
SOp("-");
ResultOp('-');
}
private void BTMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {
SOp("*");
ResultOp('*');
}
private void BTDividirActionPerformed(java.awt.event.ActionEvent evt) {
SOp("/");
ResultOp('/');
}
private void BTceActionPerformed(java.awt.event.ActionEvent evt) {
num2 = 0;
Display.setText(String.valueOf(num1));
}
private void BTLimparActionPerformed(java.awt.event.ActionEvent evt) {
num1 = 0;
num2 = 0;
Display.setText(null);
Op.setText(null);
}
private void BTraizActionPerformed(java.awt.event.ActionEvent evt) {
temp = Display.getText();
Op.setText("√¯" + "("+ temp +")" );
temp = String.valueOf(Math.sqrt(Double.parseDouble(Display.getText())));
Display.setText(temp);
}
private void BTtrocarActionPerformed(java.awt.event.ActionEvent evt) {
temp = String.valueOf(-1 * Double.parseDouble(Display.getText()));
Display.setText(temp);
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
copiar = Display.getText();
Clipboard board = Toolkit.getDefaultToolkit().getSystemClipboard();
ClipboardOwner Selecao = new StringSelection(temp);
board.setContents((Transferable) Selecao, Selecao);
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
temp = copiar;
Display.setText(temp);
}
private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {
new Sobre(this, true).setVisible(true);
}
private void formKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == KeyEvent.VK_ADD) {
SOp("+");
ResultOp('+');
} else if(evt.getKeyCode() == KeyEvent.VK_SUBTRACT) {
SOp("-");
ResultOp('-');
} else if(evt.getKeyCode() == KeyEvent.VK_MULTIPLY) {
SOp("*");
ResultOp('*');
} else if(evt.getKeyCode() == KeyEvent.VK_DIVIDE) {
SOp("/");
ResultOp('/');
} else if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
Op.setText(null);
num2 = Double.parseDouble(Display.getText());
if(operacao == '+'){
resultado = num1 + num2;
}
else if(operacao == '-'){
resultado = num1 - num2;
}
else if(operacao == '*'){
resultado = num1 * num2;
}
else {
resultado = num1 / num2;
}
Display.setText(String.valueOf(resultado));
} else if(evt.getKeyCode() == KeyEvent.VK_PERIOD) {
Display.setText(Display.getText() + ".");
} else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
Display.setText("");
BTPonto.setEnabled(true);
} else if(evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
if(Display.getText().length() > 0) {
String substring = Display.getText().substring(0, Display.getText().length() - 1);
Display.setText(substring);
}
}
if(Display.getText().contains(".")) {
BTPonto.setEnabled(false);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<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 ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Interface().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JButton BT0;
public static javax.swing.JButton BT1;
public static javax.swing.JButton BT2;
public static javax.swing.JButton BT3;
public static javax.swing.JButton BT4;
public static javax.swing.JButton BT5;
public static javax.swing.JButton BT6;
public static javax.swing.JButton BT7;
public static javax.swing.JButton BT8;
public static javax.swing.JButton BT9;
private javax.swing.JButton BTDiminuir;
private javax.swing.JButton BTDividir;
private javax.swing.JButton BTIgual;
private javax.swing.JButton BTLimpar;
private javax.swing.JButton BTMultiplicar;
private javax.swing.JButton BTPonto;
private javax.swing.JButton BTSomar;
private javax.swing.JButton BTce;
private javax.swing.JButton BTraiz;
private javax.swing.JButton BTtrocar;
public javax.swing.JTextField Display;
private javax.swing.JTextField Op;
private javax.swing.JPanel Painel;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
Class 2:
public class AçaoDasTeclas {
private final JButton BT0 = Interface.BT0;
private final JButton BT1 = Interface.BT1;
private final JButton BT2 = Interface.BT2;
private final JButton BT3 = Interface.BT3;
private final JButton BT4 = Interface.BT4;
private final JButton BT5 = Interface.BT5;
private final JButton BT6 = Interface.BT6;
private final JButton BT7 = Interface.BT7;
private final JButton BT8 = Interface.BT8;
private final JButton BT9 = Interface.BT9;
public AçãoDosBotoes ActBT0 = new AçãoDosBotoes(0);
public AçãoDosBotoes ActBT1 = new AçãoDosBotoes(1);
public AçãoDosBotoes ActBT2 = new AçãoDosBotoes(2);
public AçãoDosBotoes ActBT3 = new AçãoDosBotoes(3);
public AçãoDosBotoes ActBT4 = new AçãoDosBotoes(4);
public AçãoDosBotoes ActBT5 = new AçãoDosBotoes(5);
public AçãoDosBotoes ActBT6 = new AçãoDosBotoes(6);
public AçãoDosBotoes ActBT7 = new AçãoDosBotoes(7);
public AçãoDosBotoes ActBT8 = new AçãoDosBotoes(8);
public AçãoDosBotoes ActBT9 = new AçãoDosBotoes(9);
public void AçõesDoTeclado(javax.swing.JPanel painel) {
ActionMap ActMap = painel.getActionMap();
ActMap.put(BT0, ActBT0);
ActMap.put(BT1, ActBT1);
ActMap.put(BT2, ActBT2);
ActMap.put(BT3, ActBT3);
ActMap.put(BT4, ActBT4);
ActMap.put(BT5, ActBT5);
ActMap.put(BT6, ActBT6);
ActMap.put(BT7, ActBT7);
ActMap.put(BT8, ActBT8);
ActMap.put(BT9, ActBT9);
painel.setActionMap(ActMap);
InputMap imap = painel.getInputMap(javax.swing.JPanel.WHEN_IN_FOCUSED_WINDOW);
imap.put(KeyStroke.getKeyStroke("0"), BT0);
imap.put(KeyStroke.getKeyStroke("1"), BT1);
imap.put(KeyStroke.getKeyStroke("2"), BT2);
imap.put(KeyStroke.getKeyStroke("3"), BT3);
imap.put(KeyStroke.getKeyStroke("4"), BT4);
imap.put(KeyStroke.getKeyStroke("5"), BT5);
imap.put(KeyStroke.getKeyStroke("6"), BT6);
imap.put(KeyStroke.getKeyStroke("7"), BT7);
imap.put(KeyStroke.getKeyStroke("8"), BT8);
imap.put(KeyStroke.getKeyStroke("9"), BT9);
imap.put(KeyStroke.getKeyStroke("NUMPAD0"), BT0);
imap.put(KeyStroke.getKeyStroke("NUMPAD1"), BT1);
imap.put(KeyStroke.getKeyStroke("NUMPAD2"), BT2);
imap.put(KeyStroke.getKeyStroke("NUMPAD3"), BT3);
imap.put(KeyStroke.getKeyStroke("NUMPAD4"), BT4);
imap.put(KeyStroke.getKeyStroke("NUMPAD5"), BT5);
imap.put(KeyStroke.getKeyStroke("NUMPAD6"), BT6);
imap.put(KeyStroke.getKeyStroke("NUMPAD7"), BT7);
imap.put(KeyStroke.getKeyStroke("NUMPAD8"), BT8);
imap.put(KeyStroke.getKeyStroke("NUMPAD9"), BT9);
}
public class AçãoDosBotoes extends AbstractAction {
private final int numero;
private final JTextField Display;
public AçãoDosBotoes(int numero) {
super(Integer.toString(numero));
this.Display = new Interface().Display;
this.numero = numero;
}
@Override
public void actionPerformed(ActionEvent e) {
Display.setText(Display.getText() + numero);
}
}
}
How do you resolve??
The problem is caused by an infinite loop in the constructor of a Jframe. It could post the code of its class?
– utluiz
I just posted Jframe’s builder... Okay?
– Dongabi
Unfortunately, we still can’t identify the loop. You can put the whole exception and the whole class?
– Dener
The exception did not give to put everything but it seems to me to be a repetitive thing...
– Dongabi
In
AçaoDasTeclas
you instance several timesAçãoDosBotoes
, inAçãoDosBotoes
you instanceInterface
, inInterface
you instanceAçaoDasTeclas
and so for all ever =D– Bruno César