Ok ho studiato e sono ritornato, riuscendo a fare quello che volevo senza di te... sei fiero di me?
// importo packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// classe della finestra principale
public class Finestramenu extends Frame {
boolean equ;
int opdef = 0, adv = 0;
float res = 0;
// bottoni base
JButton button=new JButton("=");
JButton b1=new JButton("1"); JButton bf=new JButton("Math");
JButton b2=new JButton("2"); JButton b3=new JButton("3");
JButton b4=new JButton("4"); JButton b5=new JButton("5");
JButton b6=new JButton("6"); JButton b7=new JButton("7");
JButton b8=new JButton("8"); JButton b9=new JButton("9");
JButton bp=new JButton("+"); JButton bx=new JButton("*");
JButton bd=new JButton("/"); JButton b0=new JButton("0");
JButton bm=new JButton("-"); JButton bn=new JButton("C");
// bottoni math
JButton blog=new JButton("log"); JButton bln=new JButton("ln");
JButton bcos=new JButton("cos"); JButton bsin=new JButton("sin");
JButton bpi=new JButton("pi"); JButton be=new JButton("e");
MenuItem aiuto=new MenuItem("Help");
MenuItem somma=new MenuItem("Somma");
MenuItem esci=new MenuItem("Esci");
MenuItem adv1= new MenuItem("Advanced");
MenuItem bas1 = new MenuItem("Base");
Menu file= new Menu("File");
Menu help= new Menu("Help");
MenuBar barra = new MenuBar();
JLabel risultato = new JLabel("*",JLabel.CENTER);
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
public Finestramenu() {
setupEventi();
setupElementi();
setLayout(new GridLayout(6,1));
file.add(bas1); file.add(adv1);
help.add(aiuto); barra.add(file);
barra.setHelpMenu(help);
setMenuBar(barra);
// aggiungo elementi
add(t1); add(b1); add(b2); add(b3); add(b4);
add(b5); add(b6); add(b7); add(b8); add(b9);
add(b0); add(bp); add(bm); add(bd); add(bx);
add(bn); add(button); add(bf);
doLayout();
pack();
addWindowListener(new FinestramenuWindowListener());
}
// metodo main
public static void main(String[] arg) {
Finestramenu frame = new Finestramenu();
frame.setBounds(400,200,220,300);
frame.setTitle("Calc++");
frame.setVisible(true);
}
// Action Listener del Menu
class AscoltatoreMenu implements ActionListener {
public void actionPerformed (ActionEvent e) {
if(e.getActionCommand().compareTo("Esci")==0) {
System.exit(0);
}
if(e.getActionCommand().compareTo("Help")==0) {
showHelp();
}
if(e.getActionCommand().compareTo("Somma")==0) {
Somma();
}
if(e.getActionCommand().compareTo("1")==0) {
t1.setText(t1.getText() + Integer.parseInt("1"));
}
if(e.getActionCommand().compareTo("2")==0) {
t1.setText(t1.getText() + Integer.parseInt("2"));
}
if(e.getActionCommand().compareTo("3")==0) {
t1.setText(t1.getText() + Integer.parseInt("3"));
}
if(e.getActionCommand().compareTo("4")==0) {
t1.setText(t1.getText() + Integer.parseInt("4"));
}
if(e.getActionCommand().compareTo("5")==0) {
t1.setText(t1.getText() + Integer.parseInt("5"));
}
if(e.getActionCommand().compareTo("6")==0) {
t1.setText(t1.getText() + Integer.parseInt("6"));
}
if(e.getActionCommand().compareTo("7")==0) {
t1.setText(t1.getText() + Integer.parseInt("7"));
}
if(e.getActionCommand().compareTo("8")==0) {
t1.setText(t1.getText() + Integer.parseInt("8"));
}
if(e.getActionCommand().compareTo("9")==0) {
t1.setText(t1.getText() + Integer.parseInt("9"));
}
if(e.getActionCommand().compareTo("0")==0) {
t1.setText(t1.getText() + Integer.parseInt("0"));
}
if(e.getActionCommand().compareTo("+")==0) {
if(t1.getText().compareTo("")==0) {
t1.setText("0");
}
opdef = 1;
res = Float.parseFloat(t1.getText());
t1.setText("");
equ = false;
}
if(e.getActionCommand().compareTo("*")==0) {
if(t1.getText().compareTo("")==0) {
t1.setText("0");
}
opdef = 2;
res = Float.parseFloat(t1.getText());
t1.setText("");
equ = false;
}
if(e.getActionCommand().compareTo("/")==0) {
if(t1.getText().compareTo("")==0) {
t1.setText("0");
}
opdef = 4;
res = Float.parseFloat(t1.getText());
t1.setText("");
equ = false;
}
if(e.getActionCommand().compareTo("-")==0) {
if(t1.getText().compareTo("")==0) {
t1.setText("0");
}
opdef = 3;
res = Float.parseFloat(t1.getText());
t1.setText("");
equ = false;
}
if(e.getActionCommand().compareTo("=")==0) {
OperationDo();
}
if(e.getActionCommand().compareTo("C")==0) {
t1.setText("");
res = 0;
}
if(e.getActionCommand().compareTo("Math")==0) {
setMath();
}
if(e.getActionCommand().compareTo("Base")==0) {
setBase();
}
if(e.getActionCommand().compareTo("cos")==0) {
double cos = Math.cos(Double.parseDouble(t1.getText()));
cos = RoundDouble(cos, 3);
t1.setText(Double.toString(cos));
}
if(e.getActionCommand().compareTo("pi")==0) {
t1.setText("3.14159");
}
if(e.getActionCommand().compareTo("Advanced")==0) {
setMath();
}
if(e.getActionCommand().compareTo("Base")==0) {
setBase();
}
System.out.println("res = "+ Float.toString(res));
}
}
// window listener - apparentemente inutile
class FinestramenuWindowListener implements WindowListener
{
public void windowActivated(WindowEvent e) {
System.out.println("Loop on Window Activated");
}
public void windowClosed(WindowEvent e) {
System.out.println("Loop on Window Closed");
}
public void windowClosing(WindowEvent e)
{
System.out.println("Loop on Window Closing");
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
System.out.println("Loop on Window Deactivaded");
}
public void windowDeiconified(WindowEvent e) {
System.out.println("Loop on Window Deiconified");
}
public void windowIconified(WindowEvent e) {
System.out.println("Loop on Window Iconified");
}
public void windowOpened(WindowEvent e) {
System.out.println("Loop on Window Opened");
}
}
// Metodo di aiuto
public void showHelp() {
JFrame frame = new JFrame();
JLabel help_1 = new JLabel("<html>Calc++ Calcoulator" +
"<br>by MagicStcik(c) Software<br><br>" +
"Questo programma è sotto la<br>licenza" +
"<font color='blue'> Creative Commons<br> License" +
"</font>", JLabel.CENTER);
help_1.setBounds(0,10,300,20);
frame.add(help_1);
frame.setBounds(525,300,220,300);
frame.setVisible(true);
}
// setup degli elementi
public void setupElementi() {
risultato.setBounds(300,20,300,30);
t1.setBounds(10,10, 100,40);
t1.setBackground(Color.gray);
t2.setBackground(Color.gray);
button.setSize(40,30);
}
// sistema di controllo somma
public void Somma() {
risultato.setText("+");
}
public void OperationDo() {
float temp = Float.parseFloat(t1.getText());
if(opdef == 1) {
temp = res + temp;
}
if(opdef == 2) {
temp = res * temp;
}
if(opdef == 4) {
if(equ == false) {
temp = res / temp;
}
else {
temp = temp / res;
}
}
if(opdef == 3) {
if(equ == false) {
temp = res - temp;
}
else {
temp = temp - res;
}
}
if(equ == false) {
res = Float.parseFloat(t1.getText());
}
equ = true;
t1.setText(Float.toString(temp));
}
// cambio di layout
public void setMath() {
bf.setText("Base");
addButtonMath();
setLayout(new GridLayout(8,1));
doLayout();
}
public void setBase() {
bf.setText("Math");
removeButtonMath();
setLayout(new GridLayout(6,1));
doLayout();
}
public void addButtonMath() {
add(blog); add(bln); add(bcos);
add(bsin); add(be); add(bpi);
}
public void removeButtonMath() {
remove(blog); remove(bln); remove(bcos);
remove(bsin); remove(be); remove(bpi);
}
//setup degli ActionListener
void setupEventi() {
// listener del menu
aiuto.addActionListener(new AscoltatoreMenu());
somma.addActionListener(new AscoltatoreMenu());
esci.addActionListener(new AscoltatoreMenu());
b1.addActionListener(new AscoltatoreMenu());
adv1.addActionListener(new AscoltatoreMenu());
bas1.addActionListener(new AscoltatoreMenu());
// listener dei bottoni
b2.addActionListener(new AscoltatoreMenu());
b3.addActionListener(new AscoltatoreMenu());
b4.addActionListener(new AscoltatoreMenu());
b5.addActionListener(new AscoltatoreMenu());
b6.addActionListener(new AscoltatoreMenu());
b7.addActionListener(new AscoltatoreMenu());
b8.addActionListener(new AscoltatoreMenu());
b9.addActionListener(new AscoltatoreMenu());
b0.addActionListener(new AscoltatoreMenu());
bp.addActionListener(new AscoltatoreMenu());
bx.addActionListener(new AscoltatoreMenu());
bm.addActionListener(new AscoltatoreMenu());
bd.addActionListener(new AscoltatoreMenu());
bn.addActionListener(new AscoltatoreMenu());
bf.addActionListener(new AscoltatoreMenu());
button.addActionListener(new AscoltatoreMenu());
// listener bottoni math
blog.addActionListener(new AscoltatoreMenu());
bln.addActionListener(new AscoltatoreMenu());
bpi.addActionListener(new AscoltatoreMenu());
be.addActionListener(new AscoltatoreMenu());
bcos.addActionListener(new AscoltatoreMenu());
bsin.addActionListener(new AscoltatoreMenu());
}
public double RoundDouble(double numero, int nCifreDecimali){
double temp_double=Math.round(numero*Math.pow(10,nCifreDecimali));
double temp_appros=temp_double/Math.pow(10, nCifreDecimali);
return temp_appros;
}
}