Diseño del curso de Java: los requisitos de la calculadora son los siguientes:
importar java.awt.*;
importar java.awt.event.*
importar javax.swing.*; >importar javax.swing.border.*;
importar java.util.LinkedList;
importar java.io.*;
la clase pública CalculatorWindow extiende JFrame implementa ActionListener{
NumberButton numberButton[];
OperationButton OperationButton[];
operación de punto decimal de JButton, operación de signo más y menos, operación de retroceso, signo igual. operación, operación clara, sin;
JTextField resultShow; //Mostrar resultados del cálculo
JTextField showComputerProcess; //Mostrar el proceso de cálculo actual
JTextArea saveComputerProcess / / Mostrar pasos de cálculo
JButton saveButton, copyButton, clearButton;
LinkedListlt; Stringgt; // La lista vinculada se utiliza para almacenar el primer operando, el símbolo de operación y el segundo operando
HandleDigit handleDigit; //Responsable de manejar eventos ActionEvent
HandleOperation handleOperation;
HandleBack handleBack;
HandleClear handleClear;
HandleEquality handleEquality;
HandleDot handleDot;
HandlePOrN handlePOrN;
HandleSin handleSin;
public CalculatorWindow(){ p>
setTitle("Calculadora");
JPanel panelLeft, panelRight;
list=new LinkedList(); new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont( new Fuente("TimesR
oman",Font.BOLD,16));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setEditable(false);
resultShow.setBackground(Color.white);
showComputerProcess=new JTextField();
showComputerProcess.setHorizontalAlignment(JTextField.CENTER);
showComputerProcess. setFont(new Font("Arial",Font.BOLD,16));
showComputerProcess.setBackground(Color.cyan);
showComputerProcess.setEditable(false);
saveComputerProcess=new JTextArea();
saveComputerProcess.setEditable(false);
saveComputerProcess.setFont(new Font("宋体",Font.PLAIN,16))
numberButton=new NumberButton[10];
handleDigit=new HandleDigit(list, resultShow, showComputerProcess
for(int i=0; ilt); ;=9;i){
botónNúmero[i]=nuevo botónNúmero(i);
botónNúmero[i].setFont(nueva fuente("Arial",Font.BOLD, 20));
númeroBotón[i].addActionListener(handleDigit
}
botónOperación=nuevo BotónOperación[4]; > handleOperation=new HandleOperation(list, resultShow,
showComputerProcess,
Símbolos de operación de cadena []={" ", "-", "*", "/"); };
for(int i=0;ilt;4;i){
botónoperación[i]=nueva Ópera
tionButton(símbolo de operación[i]);
botón de operación[i].setFont(new Font("Arial",Font.BOLD,20));
botón de operación[i]. addActionListener(handleOperation);
}
Operación de punto decimal=new JButton(".");
handleDot=new HandleDot(list, resultShow, showComputerProcess)
Operación de punto decimal.addActionListener(handleDot);
Operación de signo positivo y negativo=new JButton(" /-"); (lista, resultadoShow, showComputerProcess);
Operación de signo positivo y negativo.addActionListener(handlePOrN);
Operación de signo igual=new JButton("=");
handleEquality= new HandleEquality(list, resultShow,
showComputerProcess, saveComputerProcess
Operación de signo igual.addActionListener(handleEquality
sin=new JButton); ("sin" );
handleSin=new HandleSin(list, resultShow,
showComputerProcess, saveComputerProcess
sin.addActionListener(handleSin ); p>
Operación de retroceso=new JButton("Retroceso");
handleBack=new HandleBack(list, resultShow, showComputerProcess);
Operación de retroceso.addActionListener(handleBack);
Borrar operación=new JButton("C");
handleClear=new HandleClear(list, resultShow, showComputerProcess);
Borrar operación.addActionListener( handleClear
Borrar operación.setForeground(Color.red);
Retroceso operación.setForeground(Color.red);
Operación de signo igual.setForeground(Color.red);
sin.setForeground(Color.blue);
Operación de signo positivo y negativo.setForeground(Color.blue);
Operación de punto decimal.setForeground(Color.blue);
panelLeft=new JPanel()
panelRight=new JPanel(); panelLeft.setLayout(new BorderLayout());
JPanel centerInLeft=new JPanel();
panelLeft.add(resultShow, BorderLayout.NORTH); panelLeft.add(centerInLeft, BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(4, 5));
centerInLeft.add(numberButton[1]); p>
centerInLeft.add(númeroButton[2]);
centerInLeft.add(númeroButton[3]);
centerInLeft.add(operaciónButton[0]); /p >
centerInLeft.add (operación clara);
centerInLeft.add(numberButton[4]); p >
centerInLeft.add(numberButton[6]);
centerInLeft.add(operaciónButton[1]);
centerInLeft.add(operación de retroceso
centerInLeft.add(númeroBotón[7]);
centroInLeft.add(númeroBotón[8]
centroInLeft.add(númeroBotón[9]); p> p>
centerInLeft.add(operaciónButton[2]);
centerInLeft.add(sin
centerInLeft.add(numberButton[0]); p>
centerInLeft.add (operación con signo positivo y negativo);
centerInLeft.add (operación con punto decimal);
centerInLeft.add(operaciónButton[3]); /p>
p>
centerInLeft.add(operación de signo igual);
panelRight.setLayout(new BorderLayout());
panelRight.add(showComputerProcess, BorderLayout.NORTH);
saveButton=new JButton("Guardar");
copyButton=new JButton("Copiar");
clearButton=new JButton("Borrar");
saveButton.setToolTipText("Guardar el proceso de cálculo en el archivo");
copyButton.setToolTipText("Copiar el proceso de cálculo seleccionado"); .setToolTipText ("Borrar proceso de cálculo");
saveButton.addActionListener(this);
copyButton.addActionListener(this);
panelRight.add(new JScrollPane(saveComputerProcess), BorderLayout.CENTER
JPanel southInPanelRight=new JPanel()
southInPanelRight.add( saveButton) ;
southInPanelRight.add(copyButton);
southInPanelRight.add(clearButton);
panelRight.add(southInPanelRight, BorderLayout.SOUTH);
JSplitPane split=nuevo JSplitPane
(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight
add(split, BorderLayout.CENTER); > setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100, 50, 528,
validar(); /p >
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==copyButton)
saveCompu
terProcess.copy();
if(e.getSource()==clearButton)
saveComputerProcess.setText(null);
if(e.getSource ()==saveButton){
JFileChooser selector=new JFileChooser();
int state=chooser.showSaveDialog(null);
Archivo archivo=chooser .getSelectedFile();
if(file!=nullamp;amp;state==JFileChooser.APPROVE_OPTION){
intentar{ String content=saveComputerProcess.getText();
StringReader leer=nuevo StringReader(contenido);
BufferedReader in= nuevo BufferedReader(leer);
FileWriter outOne=nuevo FileWriter(archivo);
BufferedWriter out= new BufferedWriter(outOne);
String str=null;
while((str=in.readLine())!=null){
salida.write(str);
salida.newLine();
}
in.close(); > out.close();
}
catch(IOException e1){}
}
}
}
public static void main(String args[]){
new CalculatorWindow()
}
}