Java 计算器:屏幕上不显示算术运算符
Java Calculator: Arithmetic operators do not appear on the screen
我在 Java 中制作了一个计算器,但我想在屏幕上显示算术运算符。我不知道为什么这不会发生,我认为这是因为计算方法立即覆盖,我不知道。为了在屏幕上进行重置,我使用了一个名为 reset 的布尔变量。
我发表评论以澄清代码。也许错误出在 class Operadores 或方法 Calcular.
PD:我在 Java 中相对较新,所以对我的代码的任何建议都很受欢迎。
你好:)
package reto1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------//
public class Reto1 {
public static void main(String[] args) {
MarcoCalculadora micalculadora = new MarcoCalculadora();
micalculadora.setVisible(true);
micalculadora.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------//
class MarcoCalculadora extends JFrame{
//Establecemos el acomodo del marco de la calculadora
BorderLayout acomodoMarco = new BorderLayout();
//Hacemos el constructor para dar caracteristicas al marco
public MarcoCalculadora(){
setTitle("CALCULADORA"); //Here I set a Title
setLayout(acomodoMarco); //Here I set the layout
setBounds(500,300, 380, 400); //I set the bounds
LaminasCalculadoras milamina = new LaminasCalculadoras();
add(milamina); //Adding
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------//
class LaminasCalculadoras extends JPanel{
JTextField pantalla = new JTextField(""); //Creating the object pantalla (screen)
JPanel milamina2 = new JPanel(); //Creating the panel for the buttons
private boolean reseteo = true;
private double resultado;
private String ultimaOperacion;
private String h;
public LaminasCalculadoras(){
//////////////////////////////////Screen////////////////////////////////////////////////////////
setLayout(new BorderLayout(20,20)); //Layout of the screen
pantalla.setEnabled(false); //Setting enabled the screen to avoid write in it
pantalla.setPreferredSize(new Dimension(400,50)); //Size of the screen
add(pantalla, BorderLayout.NORTH); //Adding the screen
//////////////////////////////////Buttons////////////////////////////////////////////////////////
milamina2.setLayout(new GridLayout(4,4,20,20)); //Matrix of buttons
add(milamina2, BorderLayout.CENTER); //Adding the buttons in the center of the panel that has the screen in the north
ActionListener a = new Numeros(); //Here I put the Actionlistener in the buttons
ActionListener b = new Operadores();
ActionListener c = new Borrado();
ponerBoton("7", a);
ponerBoton("8", a);
ponerBoton("9", a);
ponerBoton("/", b);
ponerBoton("4", a);
ponerBoton("5", a);
ponerBoton("6", a); //Here I create the buttons with the method ponerBoton
ponerBoton("*", b);
ponerBoton("1", a);
ponerBoton("2", a);
ponerBoton("3", a);
ponerBoton("-", b);
ponerBoton("C", c);
ponerBoton("0", a);
ponerBoton("=",b);
ponerBoton("+", b);
ultimaOperacion="=";
}
private void ponerBoton(String numero, ActionListener oyente){ //Method to create the Buttons and activate the action listener
JButton boton = new JButton(numero);
boton.addActionListener(oyente);
milamina2.add(boton);
}
//##################### Numbers ##############################//
private class Numeros implements ActionListener{ //Class to write the numbers in the screen
@Override
public void actionPerformed(ActionEvent e) { //Method
String entrada=e.getActionCommand(); //Getting the text of every button clicked
if(reseteo==true){ //Here I reset the screen after of click an arithemetic operator
pantalla.setText(" ");
reseteo=false;
}
pantalla.setText(pantalla.getText()+entrada); //Writting the text in screen
}
}
//##################### Operators ##############################//
private class Operadores implements ActionListener{ //Class to write the operators in the screen
//In this class I call the method calcular to do the calculations
@Override
public void actionPerformed(ActionEvent e) { //Method to write the operators in screen
String operador=e.getActionCommand();
if(reseteo==false){ //Here I want to reset the screen after click an operator
h=pantalla.getText(); //Here I get what it is in screen
pantalla.setText(" ");
reseteo=true;
}
pantalla.setText(operador); //Here I want to write the operator
calcular(Double.parseDouble(h)); //Here I call the method to do the calculations
ultimaOperacion = operador; //This variable tells what operation to do
}
public void calcular(double x){ //Method to do the calculations
switch (ultimaOperacion) {
case "+":
resultado+=x;
break;
case "-":
resultado-=x;
break;
case "*":
resultado*=x;
break;
case "/":
resultado/=x;
break;
case "=":
resultado=x;
break;
default:
break;
}
pantalla.setText(""+ resultado);
}
}
//##################### Erase ##############################//
private class Borrado implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
pantalla.setText(" ");
reseteo = true;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------//
从calcular(double x)
中删除pantalla.setText(""+ resultado);
并移动到以下位置,条件如下:
calcular(Double.parseDouble(h)); //Here I call the method to do the calculations
if ("=".equals(operador)) {
pantalla.setText(""+ resultado);
}
ultimaOperacion = operador; //This variable tells what operation to do
这只会在按下“=”时打印计算值。
我在 Java 中制作了一个计算器,但我想在屏幕上显示算术运算符。我不知道为什么这不会发生,我认为这是因为计算方法立即覆盖,我不知道。为了在屏幕上进行重置,我使用了一个名为 reset 的布尔变量。
我发表评论以澄清代码。也许错误出在 class Operadores 或方法 Calcular.
PD:我在 Java 中相对较新,所以对我的代码的任何建议都很受欢迎。
你好:)
package reto1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------//
public class Reto1 {
public static void main(String[] args) {
MarcoCalculadora micalculadora = new MarcoCalculadora();
micalculadora.setVisible(true);
micalculadora.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------//
class MarcoCalculadora extends JFrame{
//Establecemos el acomodo del marco de la calculadora
BorderLayout acomodoMarco = new BorderLayout();
//Hacemos el constructor para dar caracteristicas al marco
public MarcoCalculadora(){
setTitle("CALCULADORA"); //Here I set a Title
setLayout(acomodoMarco); //Here I set the layout
setBounds(500,300, 380, 400); //I set the bounds
LaminasCalculadoras milamina = new LaminasCalculadoras();
add(milamina); //Adding
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------//
class LaminasCalculadoras extends JPanel{
JTextField pantalla = new JTextField(""); //Creating the object pantalla (screen)
JPanel milamina2 = new JPanel(); //Creating the panel for the buttons
private boolean reseteo = true;
private double resultado;
private String ultimaOperacion;
private String h;
public LaminasCalculadoras(){
//////////////////////////////////Screen////////////////////////////////////////////////////////
setLayout(new BorderLayout(20,20)); //Layout of the screen
pantalla.setEnabled(false); //Setting enabled the screen to avoid write in it
pantalla.setPreferredSize(new Dimension(400,50)); //Size of the screen
add(pantalla, BorderLayout.NORTH); //Adding the screen
//////////////////////////////////Buttons////////////////////////////////////////////////////////
milamina2.setLayout(new GridLayout(4,4,20,20)); //Matrix of buttons
add(milamina2, BorderLayout.CENTER); //Adding the buttons in the center of the panel that has the screen in the north
ActionListener a = new Numeros(); //Here I put the Actionlistener in the buttons
ActionListener b = new Operadores();
ActionListener c = new Borrado();
ponerBoton("7", a);
ponerBoton("8", a);
ponerBoton("9", a);
ponerBoton("/", b);
ponerBoton("4", a);
ponerBoton("5", a);
ponerBoton("6", a); //Here I create the buttons with the method ponerBoton
ponerBoton("*", b);
ponerBoton("1", a);
ponerBoton("2", a);
ponerBoton("3", a);
ponerBoton("-", b);
ponerBoton("C", c);
ponerBoton("0", a);
ponerBoton("=",b);
ponerBoton("+", b);
ultimaOperacion="=";
}
private void ponerBoton(String numero, ActionListener oyente){ //Method to create the Buttons and activate the action listener
JButton boton = new JButton(numero);
boton.addActionListener(oyente);
milamina2.add(boton);
}
//##################### Numbers ##############################//
private class Numeros implements ActionListener{ //Class to write the numbers in the screen
@Override
public void actionPerformed(ActionEvent e) { //Method
String entrada=e.getActionCommand(); //Getting the text of every button clicked
if(reseteo==true){ //Here I reset the screen after of click an arithemetic operator
pantalla.setText(" ");
reseteo=false;
}
pantalla.setText(pantalla.getText()+entrada); //Writting the text in screen
}
}
//##################### Operators ##############################//
private class Operadores implements ActionListener{ //Class to write the operators in the screen
//In this class I call the method calcular to do the calculations
@Override
public void actionPerformed(ActionEvent e) { //Method to write the operators in screen
String operador=e.getActionCommand();
if(reseteo==false){ //Here I want to reset the screen after click an operator
h=pantalla.getText(); //Here I get what it is in screen
pantalla.setText(" ");
reseteo=true;
}
pantalla.setText(operador); //Here I want to write the operator
calcular(Double.parseDouble(h)); //Here I call the method to do the calculations
ultimaOperacion = operador; //This variable tells what operation to do
}
public void calcular(double x){ //Method to do the calculations
switch (ultimaOperacion) {
case "+":
resultado+=x;
break;
case "-":
resultado-=x;
break;
case "*":
resultado*=x;
break;
case "/":
resultado/=x;
break;
case "=":
resultado=x;
break;
default:
break;
}
pantalla.setText(""+ resultado);
}
}
//##################### Erase ##############################//
private class Borrado implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
pantalla.setText(" ");
reseteo = true;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------//
从calcular(double x)
中删除pantalla.setText(""+ resultado);
并移动到以下位置,条件如下:
calcular(Double.parseDouble(h)); //Here I call the method to do the calculations
if ("=".equals(operador)) {
pantalla.setText(""+ resultado);
}
ultimaOperacion = operador; //This variable tells what operation to do
这只会在按下“=”时打印计算值。