¿Cómo escribir un subprograma de calculadora en lenguaje JAVA?
El código específico es el siguiente:
importar javax.swing.*;
importar java.awt.event.*;
importar java .awt.*;
Calculadora de clase pública ?extiende JFrame implementa ActionListener ?{
JFrame privado jf;
JButton privado[] todos los botones;
JButton privado clearButton;
JTextField privado jtf;
Calculadora pública() {
//Crear una instancia del componente gráfico
jf=new JFrame("Calculadora de Ren Jing 1.0: versión JAVA");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){ p>
p>
System.exit(0);
}
});?
allButtons=nuevo JButton[16] ;
clearButton=new JButton("clear");
jtf=new JTextField(25);
jtf.setEditable(false);
Cadena str="123+456-789*0.=/";
for(int i=0;i allButtons[i]= new JButton(str.substring(i,i+1)); } } public void init(){ //Diseño completo jf.setLayout(new BorderLayout()); JPanel northPanel=new JPanel(); JPanel centerPanel=nuevo JPanel( ); JPanel surPanel=nuevo JPanel(); northPanel.setLayout(nuevo FlowLayout()); centerPanel.setLayout( nuevo GridLayout(4, 4)); southPanel.setLayout(nuevo FlowLayout()); northPanel.add(jtf); for(int i=0;i <16;i++){ centerPanel.add(allButtons[i]); } southPanel.add(clearButton); jf.add(northPanel,BorderLayout.NORTH); jf.add(centerPanel,BorderLayout.CENTER); jf.add(southPanel,BorderLayout .SOUTH); addEventHandler(); } //Agregar escucha de eventos public void addE ventHandler(){ jtf.addActionListener(this); for(int i=0;i todos los botones[ i].addActionListener(this); } clearButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Código auxiliar de método generado automáticamente Calculator.this.jtf.setText(""); } }); } //Procesamiento de eventos public void actionPerformed(ActionEvent e) { //Procesamiento completo de eventos aquí ¿Hacer el cálculo? el controlador puede ejecutar String action=e.getActionCommand(); if(action=="+"||action=="-"||action=="*" ||action=="/"){ } } public void setFontAndColor(){ Fuente f=new Font("宋体",Font.BOLD,24); jtf.setFont(f); jtf.setBackground(new Color(0x8f,0xa0,0xfb)); for(int i=0;i<16;i++){ todos los botones[i].setFont(f); todos los botones[i].setForeground (Color.RED); } } public void showMe(){ init(); setFontAndColor(); jf.pack(); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ nueva Calculadora().showMe(); } }