Estou fazendo uma calculadora e tentando habilitar a tecla Enter para o sinal de Igual, além do mouse e não está funcionando. Alguém pode ajudar, segue o código.
final JButton jButtonDivide = new JButton("/"
jButtonDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
alexandrorlandiPosts:1
Estou fazendo uma calculadora e tentando habilitar a tecla Enter para o sinal de Igual, além do mouse e não está funcionando. Alguém pode ajudar, segue o código.
;
;
;
;
;
;
;
;
;
;
{
{
{
{
{
{
{
{
;
;
;
;
;
;
;
;
;
;
;
;
;
;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Calculadora extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField jTextFieldTotal;
Double valor1, valor2;
String sinal;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculadora frame = new Calculadora();
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculadora() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 369, 439);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton jButton0 = new JButton("0"
jButton0.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
}
});
jButton0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+0);
}
});
JButton jButton1 = new JButton("1"
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+1);
}
});
JButton jButton2 = new JButton("2"
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+2);
}
});
JButton jButton3 = new JButton("3"
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+3);
}
});
JButton jButton4 = new JButton("4"
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+4);
}
});
JButton jButton5 = new JButton("5"
jButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+5);
}
});
JButton jButton6 = new JButton("6"
jButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+6);
}
});
JButton jButtonPonto = new JButton("."
jButtonPonto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+"."
}
});
JButton jButtonIgual = new JButton("="
jButtonIgual.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
// if (evt.getKeyCode() == KeyEvent.VK_ENTER){
int tecla=evt.getKeyCode();
if (tecla==KeyEvent.VK_ENTER) {
valor2 = Double.parseDouble(jTextFieldTotal.getText());
jTextFieldTotal.setText(String.valueOf(sinal));
if(sinal == "divide"
jTextFieldTotal.setText(String.valueOf(valor1 / valor2));
}
else if(sinal == "multiplica"
jTextFieldTotal.setText(String.valueOf(valor1 * valor2));
}
else if(sinal == "soma"
jTextFieldTotal.setText(String.valueOf(valor1 + valor2));
}
else if(sinal == "subtrai"
jTextFieldTotal.setText(String.valueOf(valor1 - valor2));
}
}
}
});
jButtonIgual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
valor2 = Double.parseDouble(jTextFieldTotal.getText());
jTextFieldTotal.setText(String.valueOf(sinal));
if(sinal == "divide"
jTextFieldTotal.setText(String.valueOf(valor1 / valor2));
}
else if(sinal == "multiplica"
jTextFieldTotal.setText(String.valueOf(valor1 * valor2));
}
else if(sinal == "soma"
jTextFieldTotal.setText(String.valueOf(valor1 + valor2));
}
else if(sinal == "subtrai"
jTextFieldTotal.setText(String.valueOf(valor1 - valor2));
}
}
});
JButton jButton7 = new JButton("7"
jButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+7);
}
});
JButton jButton8 = new JButton("8"
jButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+
}
});
JButton jButton9 = new JButton("9"
jButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(jTextFieldTotal.getText()+9);
}
});
final JButton jButtonDivide = new JButton("/"
jButtonDivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
valor1 = Double.parseDouble(jTextFieldTotal.getText());
sinal = "divide";
jTextFieldTotal.setText(""
}
});
JButton jButtonMultiplica = new JButton("*"
jButtonMultiplica.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
valor1 = Double.parseDouble(jTextFieldTotal.getText());
sinal = "multiplica";
jTextFieldTotal.setText(""
}
});
JButton jButtonSubtrai = new JButton("-"
jButtonSubtrai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
valor1 = Double.parseDouble(jTextFieldTotal.getText());
sinal = "subtrai";
jTextFieldTotal.setText(""
}
});
JButton jButtonSoma = new JButton("+"
jButtonSoma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
valor1 = Double.parseDouble(jTextFieldTotal.getText());
sinal = "soma";
jTextFieldTotal.setText(""
}
});
JPanel panel = new JPanel();
panel.setBackground(new Color(204, 51, 0));
jTextFieldTotal = new JTextField();
jTextFieldTotal.setEditable(false);
jTextFieldTotal.setFont(new Font("Tahoma", Font.PLAIN, 13));
jTextFieldTotal.setColumns(10);
JButton btnC = new JButton("Clear / Limpar"
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jTextFieldTotal.setText(null);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(30)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(jTextFieldTotal)
.addComponent(btnC, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(jButton7, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton8, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton9, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonDivide, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton5, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton6, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonMultiplica, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonSubtrai, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(jButton0, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonPonto, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonIgual, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jButtonSoma, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(31, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(jTextFieldTotal, GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnC)
.addGap(10)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(jButton7, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonDivide, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonMultiplica, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonSubtrai, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(jButtonSoma, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonIgual, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonPonto, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton0, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
.addGap(36))
);
JLabel lblNewLabel = new JLabel("Calculadora"
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setForeground(Color.WHITE);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(116)
.addComponent(lblNewLabel)
.addContainerGap(131, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel)
.addContainerGap(18, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
contentPane.setLayout(gl_contentPane);
}
}
Relacionados
Paleta HTML no NetBeans VAZIA
http://javafree.uol.com.br/topic-896242-Paleta-HTML-no-NetBeans-VAZIA.html
[ RESOLVIDO ] Captar valor int em JOptionPane
http://javafree.uol.com.br/topic-869403-RESOLVIDO-Captar-valor-int-em-JOptionPane.html
Problema Calculadora feita com GUI.
http://javafree.uol.com.br/topic-896157-Problema-Calculadora-feita-com-GUI.html
Implementar JComboBox em JTable AbstractTableModel
http://javafree.uol.com.br/topic-889752-Implementar-JComboBox-em-JTable-AbstractTableModel.html
Problema ao armazenar informações JTextField
http://javafree.uol.com.br/topic-896049-Problema-ao-armazenar-informacoes-JTextField.html