El primer paso de la suma, resta y división alterna del código fuente
La siguiente es una calculadora que escribí cuando estaba en la universidad. Se puede modificar ligeramente.
Importar Java .awt
Importar transferencia de datos Java
p> p>
Importar javax.swing.*;
Importar Java.awt.event.*;
Importar Java.text.*;
Clase pública M06001526 {
Public static void main(String[] args) {
Nueva calculadora();
}
}
Class Calculator extiende JFrame para implementar ActionListener
{
/**
*
*/
privado estático final largo serialVersionUID = 1L;
JTextArea show = new JTextArea(1, 20);
JButton bt1, bt2, bt3, bt4, bt5 , bt6, bt7, bt8, bt9, bt10, bt11, btAdd, btSub, btMult, btDiv, btop, btClear, back
JButton btequ
JMenuItem copyMenu, pasteMenu, exitFile, s , t, about, me;
doble prenum = 0;
doble nownum = 0;
formato de número nf = formato de número getinstance();
double nownum = 0; p>
Booleano clear = false
Bandera booleana = false;
int dianornot = 0;
operación de caracteres;
clip del portapapeles = kit de herramientas getdefaulttoolkit().
getsystem portapapeles();
Calculadora()
{
establecer diseño(nuevo diseño de flujo());
setResizable( false );
setLocation(450, 300);
setSize(250, 250);
add(new JLabel(" ");
mostrar set editable(false);
Agregar (mostrar);
Menú principal de JMenuBar = new JMenuBar(); >setJMenuBar(menú principal);
JMenu editar menú = nuevo JMenu(" editar ");
JMenu ver menú = nuevo JMenu(" ver ");
Menú de ayuda JMenu = new JMenu(" ayuda ");
menú principal agregar(editar menú);
mainMenu.add(ver menú);
menú principal agregar(menú de ayuda);
copiar menú = nuevo JMenuItem(" copiar ");
pegar menú = nuevo JMenuItem(" pegar ");
salir del archivo = new JMenuItem(" salir ");
copiar menú . addactionlistener(this);
pegar menú . p>salir del archivo . addactionlistener(this);
menú editar
menú editar . menú . agregar(salir del archivo);
t = new JMenuItem("●estándar ");
S = new JMenuItem("tipo científico"); >ver menú .add(t);
viewMenu.add
t .addactionlistener(this);
s . >
acerca de = nuevo JMenuItem(" acerca de ");
yo = nuevo JMenuItem(" autor ");
helpMenu.add(acerca de);
helpMenu.add(yo);
acerca de . addactionlistener(this);
yo . addactionlistener(this); Rectángulo().
Bt 1 = nuevo JButton(" 1 ");
bt2 = nuevo JButton(" 2 ");
bt3 = nuevo JButton(" 3 ");
bt4 = nuevo JButton(" 4 ");
bt5 = nuevo JButton(" 5 ");
bt6 = nuevo JButton(" 6 "); ");
bt7 = nuevo JButton(" 7 ");
bt8 = nuevo JButton(" 8 ");
bt9 = nuevo JButton(" 9 ");
Bt 10 = nuevo JButton(" 0 ");
bt11=nuevo JButton(" . ");
Bt 11 . setsize (18, 6);
bt11.setBounds(0, 0, 8, 6);
btAdd = nuevo JButton("+"); Bt sub = nuevo JButton("-");
Bt mult = nuevo JButton("×");
Bt div = nuevo JButton("/");
Bt clear = nuevo JButton(" reset ");
Atrás = nuevo JButton(" Atrás ");
btop = nuevo JButton("+/-") ;
btequ = new JButton(" = ");
Agregar (Bt 1);
Agregar (bt2);
Agregar (bt3);
Agregar (bt4);
Agregar (bt5); bt7);
Agregar (bt8);
Agregar (bt9);
Agregar (Bt 10);
Agregar (Bt 11);
añadir(btAdd);
añadir(Bt sub);
añadir(Bt mult);
añadir( Bt div);
Agregar (btop);
Agregar (atrás);
Agregar (Bt borrar); btequ);
Bt 1. addactionlistener(this);
bt2. addactionlistener(this);
bt3. addactionlistener(this);
bt4 . addactionlistener(este);
bt5 . addactionlistener(este);
bt6 . ) );
bt8 . addactionlistener(esto);
bt9 .
Bt 11 . addactionlistener(esto);
btadd . addactionlistener(este);
btsub .
mult . addactionlistener(this);
Bt div . addactionlistener(this);
Bt clear . ;
btop . addactionlistener(this);
btequ . addactionlistener(this);
set visible(true); ("Calculadora");
this . setdefaultcloseoperation(JFrame.
DISPOSE_ON_CLOSE);
}
Se ha realizado una operación pública no válida (evento de operación e)
{
if(clear ==true )
{
show . settext(null);
}
else if(e . getsource()= = Bt 1 )
{
if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | | operación = = ' * ' | | operación = = '/')&&mark==true)
show .settext(null);
show . p>marca = false
}
else if (e.getSource()==bt2)
{ if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | operación = = ' * ' | showtext( nulo);
show . append(" 2 ");
marca = false
}
else if (e.getSource). ()==bt3)
{ if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | | operación = = ' * ' | | operación = = '/')&&mark==true)
mostrar settext(null);
mostrar append(" 3 " );
mark = false}
else if (e.getSource()==bt4)
{ if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | operación = = ' * ' | operación = = '/')&&mark==true)
show . >mostrar . append(" 4 ");
marca = false}
else if (e.getSource()==bt5 )
{ if( (operación = = ' = ' | | operación = = '+' | | operación = = '-' | | operación = = ' * ' | | operación = = '/' )& amp;& ampmark==true)
mostrar . settext(null);
mostrar append(" 5 ");
marca = false }
else if ( e.getSource()==bt6)
{ if((operación = = ' = ' |
| operación = = '+' | operación = = '-' | operación = = ' * ' | show . null);
show . append(" 6 ");
mark = false}
else if (e.getSource()==bt7)
{ if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | | operación = = ' * ' | | operación = = '/' )& amp;& ampmark==true)
mostrar .settext(null);
mostrar append(" 7 "); /p>
else if (e.getSource()==bt8)
{ if((operación = = ' = ' | | operación = = '+' | | operación = = '- ' | | operación = = ' * ' | operación = = '/')&&mark==true)
show . " 8 ");
marca = false}
else if (e.getSource()==bt9)
{ if(( operación = = ' = ' | operación = = '+' | operación = = '-' operación = = '/')&&mark==true) | . settext(null);
show . append(" 9 "
mark = false}
else if(e . getsource()=. Bt 10)
{
if((operación = = ' = ' | | operación = = '+' | | operación = = '-' | | operación = = ' * ' | | operación = = '/')&&mark==true)
mostrar . >
mark = false}
else if(e . getsource()= = Bt 11)
{
string s 1 = gettext. ();
for(int I = 0; i<s 1 . length(); i++)
{
char a 1 = s 1 . I);
if (a1== ' . ')
dianornot = 1;
} p>
if (show.getCaretPosition ()! = 0 & amp& ampdianor
not==0)
show.append(" . ");
dianornot = 0;
}
si no ( e.getSource()==btAdd)
{
prenum = double . parse double(show . gettext());
mark = true p>
operación = '+';
}
si no (e.getSource()==btSub)
{
prenum = double . parse double(show . gettext());
mark = true
operación = '-';
} p>
else if (e.getSource()==btMult)
{ prenum = double . parse double(show . gettext());
mark = true
operación = ' * ';}
si no (e.getSource()==btDiv)
{
prenum = double .parse double(show .gettext());
marca = true
operación = '/';}
else if (e.getSource() ==btClear)
{ show . settext(null);}
else if(e . getsource()== = back &&(operación!='=' | |mark==false))
{//show . /show.insert(",s.length());
show.settext(null);
for(int I = 0; i<s.length() -1; i++)
{
char a = s . charat(I
mostrar . gettext()+a); >
}
//show . setcaretposition(0);
}
//else if (e.getSource()= =)
else if(e.getSource()==btop)
{
cadena S2 = show . (s2.charAt(0)=='-')
{
show . settext(null);
for(int I = 1;i<S2 . longitud();i++)
{
char a2 = S2 . charat(I);
show . );
}
//show . settext(S2);
}
else {show. , 0);
//mostrar setcare.
tposition(2);
//show . settext(S2);
}
}
else if(e.getSource ()==btequ)
{Resultado de cadena;
nownum = double . parse double(show . gettext());
Cambiar (operación) {
Caso "+":
int num=max(getFloatNum(nownum), getFloatNum(prenum));
//String temp 2 = double . tostring(nownum);
nf . setminimumcfractiondigits(num);
doble re = prenum+nownum
nf . >
resultado = double . tostring(re);
mostrar . show .settext(null);
operación = ' = ';
marca = true
Rotura;
Caso '-' :
num=max(getFloatNum(nownum), getFloatNum(prenum));
//String temp 2 = double . nf . setminimumcfractiondigits(num);
re = prenum-nownum;
nf formato(re);
resultado = double .
mostrar . settext(null);
show.setText(resultado);
//show . >operación = ' = ';
marca = true
Break;
Caso ' * ':
num =max(getFloatNum (nownum),getFloatNum(prenum));
//String temp 2 = double . tostring(nownum);
nf .setminimumcfractiondigits(num);
p>
re = prenum * nownum
nf . settext (nulo);
show.setText(resultado);
//show .settext(null);
operación = ' = '; p >
marca = true
Break;
Caso "/":
num=max(getFloatNum(nownum), getFloatNum(prenum)) ;
//Temp de cadena 2 = double .tostring(nownum);
dígitos(num);
re = prenum/nownum;
nf . p>
p>
mostrar .settext(nulo);
mostrar.setText(resultado);
//mostrar . >
operación = ' = ';
marca = true
Interrupción;
Predeterminado: interrupción
}
}
else if(e.getSource()==about)
{
Joptionpane. mostrar mensaje de diálogo (mostrar, "Calculadora simple", "Acerca de la calculadora",
JOptionPane.info_message
}
else if (e.getSource(); )==yo)
{
Panel de opciones. Mostrar cuadro de diálogo del mensaje (mostrar, "Copyright de * * *", "Acerca del autor",
JOptionPane.info_message
}
else if(); e.getSource()==exitFile)
{
system . exit(0);
}
si no (e) .getSource()==copyMenu)
{
cadena copiar texto = mostrar . gettext();
setClipboardText(recortar, copiar texto ); p>
}
else if(e . getsource()= = pegar menú)
{try
{
show .settext(getClipboardText(clip));
}
catch(Exception ee){}
}
} p>
public int getFloatNum(doble a)
{
int num = 0;
cadena s = tostring(a);
for(int I = 0; i<s.length().
i++)
{
num = num+1;
char b = s . == ' . ')
num = 0;
}
Cantidad devuelta;
}
public int max(int a, int b)
{ if(a & gt; b)
Devuelve a;
En caso contrario, devuelve b;
}
Vuelo estático protegido setClipboardText(recorte del portapapeles, escritura de cadenas)
{
tText transferible = nueva selección de cadenas (escríbeme) ;
clip.setContents(tText, null);
}
Lanzamientos de cadena estática protegida getClipboardText(clip del portapapeles)
p>Excepción
{
//Obtener el contenido del portapapeles
clip transferibleT = clip obtener contenido(null);
if. (clip != null) {
//Comprueba si el contenido es de tipo texto.
if(clipt . is dataflavorsupported(data flavour . string flama))
return(String)clipt gettransferdata(data flama . String flama);
}
Devolver nulo
}
}