Duvida java swing

0 respostas
S

Ola estou fazendo uma calculadora em java mas as operaçoes matematicas nao estao aparecendo.

Me ajudem

codigo abaixo:

import java.awt.EventQueue;

public class TelaCalculadora {

private MotorCalculadora m = new MotorCalculadora();

private JFrame frmCalculadora;
private JTextField LCD;

/**
 * Launch the application.
 */
public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				TelaCalculadora window = new TelaCalculadora();
				window.frmCalculadora.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
}

/**
 * Create the application.
 */
public TelaCalculadora() {
	initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
	frmCalculadora = new JFrame();
	frmCalculadora.getContentPane().setBackground(Color.BLACK);
	frmCalculadora.setTitle("Calculadora");
	frmCalculadora.setBounds(100, 100, 703, 369);
	frmCalculadora.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frmCalculadora.getContentPane().setLayout(null);
	
	LCD = new JTextField();
	LCD.setBackground(new Color(51, 204, 255));
	LCD.setBounds(10, 11, 667, 36);
	frmCalculadora.getContentPane().add(LCD);
	LCD.setColumns(10);
	
	JButton btnNewButton = new JButton("%");
	btnNewButton.setBackground(new Color(51, 204, 255));
	btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			
		
			
		}
	});
	btnNewButton.setBounds(618, 114, 59, 45);
	frmCalculadora.getContentPane().add(btnNewButton);
	
	JButton button = new JButton(".");
	button.setBackground(new Color(51, 204, 255));
	button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			if (!(LCD.getText().contains("."))) {
				
				LCD.setText(LCD.getText()+".");
				
			}
			
		}
	});
	button.setBounds(480, 274, 59, 45);
	frmCalculadora.getContentPane().add(button);
	
	JButton btnx_1 = new JButton("10^x");
	btnx_1.setBackground(new Color(51, 204, 255));
	btnx_1.setFont(new Font("Tahoma", Font.PLAIN, 10));
	btnx_1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(1);
			
			LCD.setText("");
			
		}
	});
	btnx_1.setBounds(273, 275, 59, 45);
	frmCalculadora.getContentPane().add(btnx_1);
	
	JButton btnx = new JButton("1/x");
	btnx.setFont(new Font("Tahoma", Font.PLAIN, 12));
	btnx.setBackground(new Color(51, 204, 255));
	btnx.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			
			
		}
	});
	btnx.setBounds(618, 167, 59, 45);
	frmCalculadora.getContentPane().add(btnx);
	
	JButton button_3 = new JButton("=");
	button_3.setBackground(new Color(51, 204, 255));
	button_3.setFont(new Font("Tahoma", Font.PLAIN, 15));
	button_3.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			LCD.setText(""+m.calcular());
			
		}
	});
	button_3.setBounds(618, 223, 59, 97);
	frmCalculadora.getContentPane().add(button_3);
	
	JButton button_4 = new JButton("\u221A");
	button_4.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(5);
			
			LCD.setText("");
			
			
		}
	});
	button_4.setBackground(new Color(51, 204, 255));
	button_4.setBounds(618, 58, 59, 45);
	frmCalculadora.getContentPane().add(button_4);
	
	JButton button_5 = new JButton("1");
	button_5.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+1);
			
		}
	});
	button_5.setBackground(new Color(51, 204, 255));
	button_5.setBounds(342, 223, 59, 45);
	frmCalculadora.getContentPane().add(button_5);
	
	JButton button_6 = new JButton("2");
	button_6.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+2);
			
		}
	});
	button_6.setBackground(new Color(51, 204, 255));
	button_6.setBounds(411, 223, 59, 45);
	frmCalculadora.getContentPane().add(button_6);
	
	JButton button_7 = new JButton("3");
	button_7.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+3);
			
		}
	});
	button_7.setBackground(new Color(51, 204, 255));
	button_7.setBounds(480, 223, 59, 45);
	frmCalculadora.getContentPane().add(button_7);
	
	JButton btnFe = new JButton("F-E");
	btnFe.setFont(new Font("Tahoma", Font.PLAIN, 11));
	btnFe.setBackground(new Color(51, 204, 255));
	btnFe.setBounds(10, 274, 59, 45);
	frmCalculadora.getContentPane().add(btnFe);
	
	JButton btnExp = new JButton("Exp");
	btnExp.setFont(new Font("Tahoma", Font.PLAIN, 11));
	btnExp.setBackground(new Color(51, 204, 255));
	btnExp.setBounds(70, 274, 60, 45);
	frmCalculadora.getContentPane().add(btnExp);
	
	JButton btnMod = new JButton("mod");
	btnMod.setBackground(new Color(51, 204, 255));
	btnMod.setFont(new Font("Tahoma", Font.PLAIN, 11));
	btnMod.setBounds(140, 274, 59, 45);
	frmCalculadora.getContentPane().add(btnMod);
	
	JButton btnLog = new JButton("log");
	btnLog.setBackground(new Color(51, 204, 255));
	btnLog.setBounds(204, 274, 59, 45);
	frmCalculadora.getContentPane().add(btnLog);
	
	JButton button_12 = new JButton("0");
	button_12.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+0);
			
		}
	});
	button_12.setBackground(new Color(51, 204, 255));
	button_12.setBounds(342, 275, 128, 45);
	frmCalculadora.getContentPane().add(button_12);
	
	JButton button_13 = new JButton("+");
	button_13.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(1);
			
			LCD.setText("");
			
		}
	});
	button_13.setBackground(new Color(51, 204, 255));
	button_13.setFont(new Font("Tahoma", Font.PLAIN, 16));
	button_13.setBounds(549, 273, 59, 45);
	frmCalculadora.getContentPane().add(button_13);
	
	JButton button_14 = new JButton("-");
	button_14.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(2);
			
			LCD.setText("");
			
		}
	});
	button_14.setBackground(new Color(51, 204, 255));
	button_14.setFont(new Font("Tahoma", Font.PLAIN, 18));
	button_14.setBounds(549, 220, 59, 45);
	frmCalculadora.getContentPane().add(button_14);
	
	JButton button_15 = new JButton("*");
	button_15.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(3);
			
			LCD.setText("");
			
		}
	});
	button_15.setBackground(new Color(51, 204, 255));
	button_15.setFont(new Font("Tahoma", Font.PLAIN, 16));
	button_15.setBounds(549, 167, 59, 45);
	frmCalculadora.getContentPane().add(button_15);
	
	JButton button_16 = new JButton("\u00F7");
	button_16.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(4);
			
			LCD.setText("");
			
		}
	});
	button_16.setBackground(new Color(51, 204, 255));
	button_16.setFont(new Font("Tahoma", Font.PLAIN, 15));
	button_16.setBounds(549, 114, 59, 45);
	frmCalculadora.getContentPane().add(button_16);
	
	JButton button_17 = new JButton("\u00B1");
	button_17.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
		}
	});
	button_17.setBackground(new Color(51, 204, 255));
	button_17.setBounds(549, 58, 59, 45);
	frmCalculadora.getContentPane().add(button_17);
	
	JButton button_18 = new JButton("6");
	button_18.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+6);
			
		}
	});
	button_18.setBackground(new Color(51, 204, 255));
	button_18.setBounds(480, 168, 60, 45);
	frmCalculadora.getContentPane().add(button_18);
	
	JButton btnC = new JButton("9");
	btnC.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+9);
			
		}
	});
	btnC.setBackground(new Color(51, 204, 255));
	btnC.setBounds(480, 114, 60, 45);
	frmCalculadora.getContentPane().add(btnC);
	
	JButton btnC_1 = new JButton("C");
	btnC_1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText("");
			
		}
	});
	btnC_1.setBackground(new Color(51, 204, 255));
	btnC_1.setBounds(480, 58, 59, 45);
	frmCalculadora.getContentPane().add(btnC_1);
	
	JButton button_21 = new JButton("5");
	button_21.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+5);
			
		}
	});
	button_21.setBackground(new Color(51, 204, 255));
	button_21.setBounds(411, 168, 59, 45);
	frmCalculadora.getContentPane().add(button_21);
	
	JButton button_22 = new JButton("8");
	button_22.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+8);
			
		}
	});
	button_22.setBackground(new Color(51, 204, 255));
	button_22.setBounds(411, 114, 59, 45);
	frmCalculadora.getContentPane().add(button_22);
	
	JButton button_24 = new JButton("4");
	button_24.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+4);
			
		}
	});
	button_24.setBackground(new Color(51, 204, 255));
	button_24.setBounds(342, 168, 59, 45);
	frmCalculadora.getContentPane().add(button_24);
	
	JButton button_25 = new JButton("7");
	button_25.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(LCD.getText()+7);
			
		}
	});
	button_25.setBackground(new Color(51, 204, 255));
	button_25.setBounds(342, 114, 59, 45);
	frmCalculadora.getContentPane().add(button_25);
	
	JButton btnCe = new JButton("CE");
	btnCe.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText("Apagado");
			
		}
	});
	btnCe.setBackground(new Color(51, 204, 255));
	btnCe.setBounds(411, 58, 59, 45);
	frmCalculadora.getContentPane().add(btnCe);
	
	JButton btnX_2 = new JButton("x^1/3");
	btnX_2.setFont(new Font("Tahoma", Font.PLAIN, 9));
	btnX_2.setBackground(new Color(51, 204, 255));
	btnX_2.setBounds(273, 223, 59, 45);
	frmCalculadora.getContentPane().add(btnX_2);
	
	JButton btnXy_1 = new JButton("x^1/y");
	btnXy_1.setFont(new Font("Tahoma", Font.PLAIN, 9));
	btnXy_1.setBackground(new Color(51, 204, 255));
	btnXy_1.setBounds(273, 168, 59, 45);
	frmCalculadora.getContentPane().add(btnXy_1);
	
	JButton btnN = new JButton("n!");
	btnN.setBackground(new Color(51, 204, 255));
	btnN.setBounds(273, 114, 59, 45);
	frmCalculadora.getContentPane().add(btnN);
	
	JButton button_30 = new JButton(")");
	button_30.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText(")");
			
		}
	});
	button_30.setBackground(new Color(51, 204, 255));
	button_30.setBounds(342, 58, 59, 45);
	frmCalculadora.getContentPane().add(button_30);
	
	JButton btnX_1 = new JButton("x^3");
	btnX_1.setBackground(new Color(51, 204, 255));
	btnX_1.setBounds(204, 223, 59, 45);
	frmCalculadora.getContentPane().add(btnX_1);
	
	JButton btnXy = new JButton("x^y");
	btnXy.setBackground(new Color(51, 204, 255));
	btnXy.setBounds(204, 168, 59, 45);
	frmCalculadora.getContentPane().add(btnXy);
	
	JButton btnX = new JButton("sin");
	btnX.setBackground(new Color(51, 204, 255));
	btnX.setBounds(140, 114, 59, 45);
	frmCalculadora.getContentPane().add(btnX);
	
	JButton button_34 = new JButton("(");
	button_34.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			LCD.setText("(");
			
		}
	});
	button_34.setBackground(new Color(51, 204, 255));
	button_34.setBounds(273, 58, 59, 45);
	frmCalculadora.getContentPane().add(button_34);
	
	JButton btnTan = new JButton("tan");
	btnTan.setBackground(new Color(51, 204, 255));
	btnTan.setBounds(140, 223, 59, 45);
	frmCalculadora.getContentPane().add(btnTan);
	
	JButton btnCos = new JButton("cos");
	btnCos.setBackground(new Color(51, 204, 255));
	btnCos.setBounds(140, 168, 59, 45);
	frmCalculadora.getContentPane().add(btnCos);
	
	JButton btnSin = new JButton("x^2");
	btnSin.setBackground(new Color(51, 204, 255));
	btnSin.setBounds(204, 114, 59, 45);
	frmCalculadora.getContentPane().add(btnSin);
	
	JButton btnIn = new JButton("In");
	btnIn.setBackground(new Color(51, 204, 255));
	btnIn.setBounds(204, 58, 59, 45);
	frmCalculadora.getContentPane().add(btnIn);
	
	JButton btnTanh = new JButton("tanh");
	btnTanh.setBackground(new Color(51, 204, 255));
	btnTanh.setBounds(71, 223, 59, 45);
	frmCalculadora.getContentPane().add(btnTanh);
	
	JButton btnCosh = new JButton("cosh");
	btnCosh.setBackground(new Color(51, 204, 255));
	btnCosh.setBounds(71, 168, 59, 45);
	frmCalculadora.getContentPane().add(btnCosh);
	
	JButton btnSinh = new JButton("sinh");
	btnSinh.setBackground(new Color(51, 204, 255));
	btnSinh.setBounds(71, 114, 59, 45);
	frmCalculadora.getContentPane().add(btnSinh);
	
	JButton btnInv = new JButton("Inv");
	btnInv.setBackground(new Color(51, 204, 255));
	btnInv.setBounds(140, 58, 59, 45);
	frmCalculadora.getContentPane().add(btnInv);
	
	JButton btnOnoff = new JButton("ON/OFF");
	btnOnoff.setBackground(new Color(51, 204, 255));
	btnOnoff.setBounds(10, 58, 120, 45);
	frmCalculadora.getContentPane().add(btnOnoff);
	
	JButton btnInt = new JButton("Int");
	btnInt.setBackground(new Color(51, 204, 255));
	btnInt.setBounds(10, 114, 59, 43);
	frmCalculadora.getContentPane().add(btnInt);
	
	JButton btnDms = new JButton("dms");
	btnDms.setFont(new Font("Tahoma", Font.PLAIN, 11));
	btnDms.setBackground(new Color(51, 204, 255));
	btnDms.setBounds(10, 168, 59, 45);
	frmCalculadora.getContentPane().add(btnDms);
	
	JButton button_46 = new JButton("\u03C0");
	button_46.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			m.setn1(Double.parseDouble(LCD.getText()));
			
			m.setoperacao(6);
			
			LCD.setText("");			
			
		}
	});
	button_46.setBackground(new Color(51, 204, 255));
	button_46.setFont(new Font("Tahoma", Font.PLAIN, 16));
	button_46.setBounds(10, 223, 59, 45);
	frmCalculadora.getContentPane().add(button_46);
}

}

e a outra classe:

public class MotorCalculadora {

private double n1;
private double n2;
private int operacao;
private double resultado = 0;

public void setoperacao (int operacao) {
	
	this.operacao = operacao;
	
}

public void setn1 (double n1) {
	
	this.n1 = n1;
	
}

public void setn2 (double n2) {
	
	this.n2 = n2;
	
}

public double calcular () {
	
switch (this.operacao) {

case 1:
	
resultado = n1 + n2;

break;

case 2:
	
resultado = n1 - n2;

break;

case 3:
	
resultado = n1 * n2;

break;

case 4:
	
    if (n2==0) {
    	
    	this.resultado=0;
    	
    }else{
    	
    	this.resultado = n1/n2; 
    	
    }
    default:
    	
    	this.resultado = 0;
    	break;
    	
case 5:
	
	this.resultado = Math.sqrt(n1);	
     
}   

return this.resultado;	

}

}

Criado 14 de novembro de 2015
Respostas 0
Participantes 1