|
|
| (4 revisiones intermedias por el mismo usuario no mostrado) |
| Línea 1: |
Línea 1: |
| − | =Proyecto de calculadora=
| + | {{:Usuario:ManuelRomero/CursoJavaUml/Practicas/PracticaCalculadora/nav}} |
| − | *El siguiente código está sin comentar es el que hemos realizado en clase
| + | |
| − | {{Media|'''Muchas gracias a todas/os por vuestra atención''''}} | + | |
| − | | + | |
| − | Disfrutad el fin de semana
| + | |
| − | <source lang=java>
| + | |
| − | package calucladora;
| + | |
| − | import javax.swing.*;
| + | |
| − | import java.awt.*;
| + | |
| − | import java.awt.event.*;
| + | |
| − | | + | |
| − | | + | |
| − | | + | |
| − | public class VentanaCalculadora extends JFrame implements ActionListener{
| + | |
| − | | + | |
| − | //Estdo de operacion de la calculadora
| + | |
| − | final int REAL =0;
| + | |
| − | final int RACIONAL = 1;
| + | |
| − |
| + | |
| − | int modoOperacion =REAL;
| + | |
| − |
| + | |
| − | JButton bDiv = new JButton();
| + | |
| − | JButton bSep = new JButton();
| + | |
| − |
| + | |
| − | // Definimos atributos de mi clase
| + | |
| − | JTextField tPantalla = new JTextField();
| + | |
| − |
| + | |
| − | //Definimos botones
| + | |
| − | Character [] botones ={'7','8','9','C',(char)8592, //Caracter retroceso
| + | |
| − | '4','5','6','+','%',
| + | |
| − | '1','2','3','-',(char)177,//caracter +/-
| + | |
| − | '0','*','/','.','='};
| + | |
| − |
| + | |
| − |
| + | |
| − | Font fuente = new Font("Arial", Font.BOLD,18);
| + | |
| − |
| + | |
| − | GridLayout lBotones = new GridLayout(4,5,5,5);
| + | |
| − | BorderLayout lPpal = new BorderLayout();
| + | |
| − |
| + | |
| − | JPanel pBotones = new JPanel();
| + | |
| − |
| + | |
| − | //Menus
| + | |
| − | JMenuBar barraMenu = new JMenuBar();
| + | |
| − | JMenu mEstilo = new JMenu("Estilo");
| + | |
| − | JMenu mAyuda = new JMenu("Ayuda");
| + | |
| − | JMenu mSalir = new JMenu("Salir");
| + | |
| − |
| + | |
| − | JRadioButtonMenuItem bRacional = new JRadioButtonMenuItem("Racionall");
| + | |
| − | JRadioButtonMenuItem bReal = new JRadioButtonMenuItem("Real");
| + | |
| − |
| + | |
| − | ButtonGroup grupo = new ButtonGroup();
| + | |
| − |
| + | |
| − | VentanaCalculadora(){
| + | |
| − | addBotones();
| + | |
| − | addMenu();
| + | |
| − |
| + | |
| − | setJMenuBar(barraMenu);
| + | |
| − |
| + | |
| − | setLayout(lPpal);
| + | |
| − | tPantalla.setHorizontalAlignment(JTextField.RIGHT);
| + | |
| − | tPantalla.setFont(fuente);
| + | |
| − | | + | |
| − |
| + | |
| − | add(tPantalla,BorderLayout.NORTH);
| + | |
| − | add(pBotones, BorderLayout.CENTER);
| + | |
| − | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
| + | |
| − | setVisible(true);
| + | |
| − | pack();
| + | |
| − | }
| + | |
| − |
| + | |
| − | private void addMenu(){
| + | |
| − | barraMenu.add(mEstilo);
| + | |
| − | barraMenu.add(mAyuda);
| + | |
| − | barraMenu.add(mSalir);
| + | |
| − |
| + | |
| − | //Añadir escuchadores de evento a los botones
| + | |
| − | bReal.addActionListener(this);
| + | |
| − | bRacional.addActionListener(this);
| + | |
| − |
| + | |
| − |
| + | |
| − | grupo.add(bRacional);
| + | |
| − | grupo.add(bReal);
| + | |
| − |
| + | |
| − | bReal.setSelected(true);
| + | |
| − |
| + | |
| − | mEstilo.add(bRacional);
| + | |
| − | mEstilo.add(bReal);
| + | |
| − |
| + | |
| − |
| + | |
| − |
| + | |
| − |
| + | |
| − | }
| + | |
| − | private void addBotones(){
| + | |
| − |
| + | |
| − | pBotones.setLayout(lBotones);
| + | |
| − |
| + | |
| − | for (int i=0;i<20;i++){
| + | |
| − |
| + | |
| − | JButton b = new JButton(botones[i].toString());
| + | |
| − | b.setFont(fuente);
| + | |
| − | b.addActionListener(this);
| + | |
| − | pBotones.add(b);
| + | |
| − | if (i==17){
| + | |
| − | bDiv=b;
| + | |
| − | }
| + | |
| − | if (i==18){
| + | |
| − | bSep=b;
| + | |
| − | }
| + | |
| − | | + | |
| − |
| + | |
| − | }
| + | |
| − | }
| + | |
| − |
| + | |
| − | public void actionPerformed(ActionEvent e) {
| + | |
| − | if (e.getSource() ==bReal){
| + | |
| − | modoOperacion=REAL;
| + | |
| − | setTitle("Calculadora Real");
| + | |
| − | bDiv.setText("/");
| + | |
| − | bSep.setText(".");
| + | |
| − | }else
| + | |
| − | if (e.getSource() ==bRacional){
| + | |
| − | modoOperacion=RACIONAL;
| + | |
| − | setTitle("Calculadora Racional");
| + | |
| − | bDiv.setText(":");
| + | |
| − | bSep.setText("/");
| + | |
| − |
| + | |
| − | }else{
| + | |
| − | String c = ((JButton)e.getSource()).getText();
| + | |
| − | tPantalla.setText(tPantalla.getText()+c);
| + | |
| − | }
| + | |
| − | }
| + | |
| − | public static void main (String []s){
| + | |
| − | VentanaCalculadora v = new VentanaCalculadora();
| + | |
| − | }
| + | |
| − | | + | |
| − | }
| + | |
| − | | + | |
| − | </source>
| + | |