Estou errando ao implementar o códico da calculadora, alguém pode me ajudar?
Depois preciso fazer referência a um código de menu, que também vou colocar ai pra vcs verem.
Agradeço.
primeiro a calculadora, não exibe os números e o resuldado dos calculos.
import java.awt.<em>;
import java.awt.event.</em>;
import javax.swing.*;
public class Calc extends JPanel implements ActionListener
{
GridBagConstraints gbc = new GridBagConstraints( );
JTextField theDisplay = new JTextField( );
public Calc( )
{
gbc.weightx = 1.0; gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
ContainerListener listener = new ContainerAdapter( )
{
public void componentAdded(ContainerEvent e)
{
Component comp = e.getChild( );
if (comp instanceof JButton)
((JButton)comp).addActionListener(Calc.this);
}
};
addContainerListener(listener);
gbc.gridwidth = 4;
addGB(this, theDisplay, 0, 0);
JPanel topRow = new JPanel( );
topRow.addContainerListener(listener);
gbc.gridwidth = 1;
gbc.weightx = 1.0;
addGB(topRow, new JButton("C"), 0, 0);
gbc.weightx = 0.33;
addGB(topRow, new JButton("%"), 1, 0);
gbc.weightx = 1.0;
addGB(topRow, new JButton("+"), 2, 0 );
gbc.gridwidth = 4;
addGB(this, topRow, 0, 1);
gbc.weightx = 1.0; gbc.gridwidth = 1;
// dígitos
for(int j=0; j<3; j++)
for(int i=0; i<3; i++)
addGB(this, new JButton("" + ((2-j)*3+i+1) ), i, j+2);
// -, x e divisão
addGB(this, new JButton("-"), 3, 2);
addGB(this, new JButton("x"), 3, 3);
addGB(this, new JButton("u00F7"), 3, 4);
// outra fila
JPanel bottomRow = new JPanel( );
bottomRow.addContainerListener(listener);
gbc.weightx = 1.0;
addGB(bottomRow, new JButton("0"), 0, 0);
gbc.weightx = 0.33;
addGB(bottomRow, new JButton("."), 1, 0);
gbc.weightx = 1.0;
addGB(bottomRow, new JButton("="), 2, 0);
gbc.gridwidth = 4;
addGB(this, bottomRow, 0, 5);
}
void addGB(Container cont, Component comp, int x, int y)
{
if ((cont.getLayout( ) instanceof GridBagLayout) == false)
cont.setLayout(new GridBagLayout( ));
gbc.gridx = x; gbc.gridy = y;
cont.add(comp, gbc);
} // fim de addGB
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand( ).equals(“C”)) theDisplay.setText("");
else
theDisplay.setText(theDisplay.getText( ) + e.getActionCommand( ));
} // fim do if action Performed
public static void main(String[] args)
{
JFrame f = new JFrame(“Calculadora”);
f.addWindowListener(new WindowAdapter( )
{
public void windowClosing(WindowEvent e) { System.exit(0); }
});
f.setSize(200, 250);
f.setLocation(200, 200);
f.setContentPane(new Calc( ));
f.setVisible(true);
}
}
segundo, código do menu. Vou fazer referência da calcluradora com a classe MenuExemplo abaixo.
import java.awt.<em>;
import java.awt.event.</em>;
import javax.swing.<em>;
import javax.swing.event.</em>;
//---------------------------------
// Classe Menu Exemplo
//---------------------------------
public class MenuExemplo extends JFrame
{
private JMenuItem abrir, novo, salvar, sair;
private JMenu arquivo;
private JMenuBar menuBar;
private JMenu editar;
private JMenu sobre;
public MenuExemplo()
{
super(" Menu Exemplo ");
} // fim de MenuExemplo
public void init()
{
setSize( 400,400 );
setLocation( 300,200 );
//----------------------
//construindo os objetos
//----------------------
abrir = new JMenuItem(" Abrir ");
novo = new JMenuItem(" Novo ");
salvar = new JMenuItem(" Salvar ");
sair = new JMenuItem(" Sair ");
arquivo = new JMenu(" Arquivo ");
editar = new JMenu(" Editar ");
sobre = new JMenu(" Sobre ");
//---------------------------
// construindo o menu arquivo
//---------------------------
arquivo.add(abrir);
arquivo.add(novo);
arquivo.add(salvar);
arquivo.addSeparator();
arquivo.add(sair);
//---------------------
//Construindo o MenuBar
//---------------------
menuBar = new JMenuBar();
menuBar.add(arquivo);
setJMenuBar(menuBar);
//--------------------------
// construindo o Menu editar
//--------------------------
menuBar.add(editar);
setJMenuBar(menuBar);
//--------------------------
// construindo o Menu sobre
//--------------------------
menuBar.add(sobre);
setJMenuBar(menuBar);
MenuHandler mh = new MenuHandler();
addWindowListener( new WindowHandler() );
//--------------------------
// registrando Listener
//-------------------------
abrir.addActionListener(mh);
novo.addActionListener(mh);
salvar.addActionListener(mh);
sair.addActionListener(mh);
setVisible(true);
} // fim do public void init
public static void main( String args[] )
{
new MenuExemplo().init();
} // fim de main
class MenuHandler implements ActionListener
{
public void actionPerformed( ActionEvent ae)
{
if ( ae.getSource() == sair )
{
System.exit(0);
}
else
{
System.out.println(ae);
} // fim do else
if ( ae.getSource() == abrir )
{
Calc calc = new Calc();
}
} // fim do void ActionFormed
} // fim da class MenuHandler
class WindowHandler extends WindowAdapter
{
public void windoClosing( WindowEvent we)
{
System.exit(0);
}
} // fim do WindowHandler
} // fim da classe MenuExemplo