Eu fiz uma calculadora e adicionei esse key pressed em todos os meus botões da calculadora.
Porém , como adicionei por exemplo o key pressed no vk_ Divide , dando run , apertando no “divisão” , ele aparece no JText . E eu não quero que aparece no JText.
Olha meu código :
package Calculadora;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URI;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
public class telaSoma {
private JFrame frame;
private JTextField tfText;
private JButton btn1;
float valor1, valor2;
String sinal;
private JButton btnMais;
private JLabel lblMostraConta;
private JButton btnApagarLetra;
private JButton btn0;
private JButton btnX;
private JButton btnDiv;
private JButton btnQuadrado;
private JButton btnPorcentagem;
private JButton btnMenos;
private JButton btnC;
private JButton btn9;
private JButton btn8;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btnIgual;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
telaSoma window = new telaSoma();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public telaSoma() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.BLACK);
frame.setBounds(100, 100, 506, 570);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
tfText = new JTextField();
tfText.setFont(new Font("Tahoma", Font.PLAIN, 20));
tfText.setBorder(new LineBorder(Color.BLUE, 5));
tfText.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if(tfText.getText().length()>200) {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_0) {
btn0.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_1) {
btn1.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_2) {
btn2.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_3) {
btn3.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_4) {
btn4.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_5) {
btn5.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_6) {
btn6.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_7) {
btn7.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_8) {
btn8.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_9) {
btn9.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_ENTER) {
btnIgual.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_ADD) {
btnMais.doClick();
}
if(e.getKeyCode()==KeyEvent.VK_DIVIDE) {
btnDiv.doClick();
}
}
});
tfText.setBounds(23, 59, 451, 98);
frame.getContentPane().add(tfText);
tfText.setColumns(10);
btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn7.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn7.setBackground(new Color(173, 216, 230));
btn7.setBounds(23, 184, 76, 56);
frame.getContentPane().add(btn7);
btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn8.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn8.setBackground(new Color(173, 216, 230));
btn8.setBounds(110, 184, 76, 56);
frame.getContentPane().add(btn8);
btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn9.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn9.setBackground(new Color(173, 216, 230));
btn9.setBounds(196, 184, 76, 56);
frame.getContentPane().add(btn9);
btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn4.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn4.setBackground(new Color(173, 216, 230));
btn4.setBounds(23, 251, 76, 56);
frame.getContentPane().add(btn4);
btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn5.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn5.setBackground(new Color(173, 216, 230));
btn5.setBounds(110, 251, 76, 56);
frame.getContentPane().add(btn5);
btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn6.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn6.setBackground(new Color(173, 216, 230));
btn6.setBounds(196, 251, 76, 56);
frame.getContentPane().add(btn6);
btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn1.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn1.setBackground(new Color(173, 216, 230));
btn1.setBounds(23, 318, 76, 56);
frame.getContentPane().add(btn1);
btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn2.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn2.setBackground(new Color(173, 216, 230));
btn2.setBounds(110, 318, 76, 56);
frame.getContentPane().add(btn2);
btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent g4) {
tfText.setText(tfText.getText());
tfText.requestFocus();
}
});
btn3.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn3.setBackground(new Color(173, 216, 230));
btn3.setBounds(196, 318, 76, 56);
frame.getContentPane().add(btn3);
btnC = new JButton("C");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText("");
lblMostraConta.setText("");
}
});
btnC.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnC.setBackground(new Color(173, 216, 230));
btnC.setBounds(293, 189, 56, 46);
frame.getContentPane().add(btnC);
btnApagarLetra = new JButton("<--");
btnApagarLetra.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ApagarLetra = tfText.getText();
ApagarLetra = ApagarLetra.substring(0, ApagarLetra.length() - 1);
tfText.setText(ApagarLetra);
tfText.requestFocus();
}
});
btnApagarLetra.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnApagarLetra.setBackground(new Color(173, 216, 230));
btnApagarLetra.setBounds(359, 191, 114, 41);
frame.getContentPane().add(btnApagarLetra);
btnMais = new JButton("+");
btnMais.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1= Float.parseFloat(tfText.getText());
lblMostraConta.setText(tfText.getText()+ "+");
tfText.setText("");
sinal="soma";
tfText.requestFocus();
}
});
btnMais.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnMais.setBackground(new Color(173, 216, 230));
btnMais.setBounds(332, 256, 66, 46);
frame.getContentPane().add(btnMais);
btnMenos = new JButton("-");
btnMenos.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1 = Float.parseFloat(tfText.getText());
lblMostraConta.setText(tfText.getText()+"-");
sinal = "subtração";
tfText.setText("");
tfText.requestFocus();
}
});
btnMenos.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnMenos.setBackground(new Color(173, 216, 230));
btnMenos.setBounds(407, 256, 66, 46);
frame.getContentPane().add(btnMenos);
btnX = new JButton("x");
btnX.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1=Float.parseFloat(tfText.getText());
lblMostraConta.setText(tfText.getText()+ "x");
tfText.setText("");
sinal = "vezes";
}
});
btnX.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnX.setBackground(new Color(173, 216, 230));
btnX.setBounds(332, 318, 56, 46);
frame.getContentPane().add(btnX);
btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1= Float.parseFloat(tfText.getText());
lblMostraConta.setText(tfText.getText()+ "/");
tfText.setText("");
sinal = "divisão";
}
});
btnDiv.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnDiv.setBackground(new Color(173, 216, 230));
btnDiv.setBounds(407, 318, 66, 46);
frame.getContentPane().add(btnDiv);
btn0 = new JButton("0");
btn0.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_0) {
tfText.setText(tfText.getText()+"0");
tfText.requestFocus();
}
}
});
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfText.setText(tfText.getText()+"0");
tfText.requestFocus();
tfText.requestFocus();
}
});
btn0.setFont(new Font("Tahoma", Font.PLAIN, 26));
btn0.setBackground(new Color(173, 216, 230));
btn0.setBounds(23, 395, 249, 46);
frame.getContentPane().add(btn0);
btnIgual = new JButton("=");
btnIgual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sinal == "soma") {
valor2= Float.parseFloat(tfText.getText());
lblMostraConta.setText("");
tfText.setText(String.valueOf(valor1+valor2));
}
if(sinal == "subtração") {
valor2= Float.parseFloat(tfText.getText());
lblMostraConta.setText("");
tfText.setText(String.valueOf(valor1-valor2));
}
if(sinal == "vezes") {
valor2= Float.parseFloat(tfText.getText());
lblMostraConta.setText("");
tfText.setText(String.valueOf(valor1*valor2));
}
if(sinal== "divisão") {
valor2= Float.parseFloat(tfText.getText());
lblMostraConta.setText("");
tfText.setText(String.valueOf(valor1/valor2));
}
if(sinal== "porc") {
valor2= Float.parseFloat(tfText.getText());
lblMostraConta.setText("");
tfText.setText(String.valueOf((valor1*valor2)/100));
}
}
});
btnIgual.setFont(new Font("Tahoma", Font.BOLD, 35));
btnIgual.setBackground(new Color(50, 205, 50));
btnIgual.setBounds(23, 457, 450, 46);
frame.getContentPane().add(btnIgual);
btnPorcentagem = new JButton("%");
btnPorcentagem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1= Float.parseFloat(tfText.getText());
tfText.setText("");
sinal = "porc";
}
});
btnPorcentagem.setBackground(new Color(176, 224, 230));
btnPorcentagem.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnPorcentagem.setBounds(332, 375, 66, 56);
frame.getContentPane().add(btnPorcentagem);
btnQuadrado = new JButton("x^2");
btnQuadrado.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
valor1= Float.parseFloat(tfText.getText());
tfText.setText("");
sinal = "ao2";
if(sinal== "ao2") {
tfText.setText(String.valueOf(valor1*valor1));
}
}
});
btnQuadrado.setBackground(new Color(176, 224, 230));
btnQuadrado.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnQuadrado.setBounds(410, 375, 81, 56);
frame.getContentPane().add(btnQuadrado);
lblMostraConta = new JLabel("");
lblMostraConta.setForeground(Color.WHITE);
lblMostraConta.setBounds(23, 11, 451, 37);
frame.getContentPane().add(lblMostraConta);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu_2 = new JMenu("exibir");
menuBar.add(mnNewMenu_2);
JMenu mnNewMenu = new JMenu("editar");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("copiar(ctrl + c)");
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("colar( ctrl + v)");
mnNewMenu.add(mntmNewMenuItem_1);
JMenu mnNewMenu_1 = new JMenu("ajuda");
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("sobre calculadora");
mnNewMenu_1.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("increver no meu canal");
mntmNewMenuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
URI link = new URI("https://www.youtube.com/channel/UCHeamJetCCQLSTxhrz_00bg");
Desktop.getDesktop().browse(link);
} catch (Exception e2) {
// TODO: handle exception
}
}
});
mnNewMenu_1.add(mntmNewMenuItem_3);
System.out.println(valor1+ " "+ sinal+ " "+ valor2 );
}
}