Programa sobre calculadora binaria em java

1 resposta
C

como eu posso fazer 1 programa em java que apertando 2 botões com valores 0 e 1 quando apertado o botão 0 aparecia 1 no visor e virce versa com o outro botão.

1 Resposta

B
public class Calculadora extends JPanel implements ActionListener{

	private JTextField text;
	private JButton button0;
	private JButton button1;
	
	public Calculadora(){
		super(new BorderLayout());
		text = new JTextField(10);
		button0 = new JButton("0");
		button1 = new JButton("1");
		button0.addActionListener(this);
		button1.addActionListener(this);
		JPanel buttonPanel = new JPanel(); 
		buttonPanel.add(button0);
	    buttonPanel.add(button1);
	    Box hTextBox = Box.createHorizontalBox();
        Box vBox = Box.createVerticalBox();
        hTextBox.add(text);
        vBox.add(hTextBox);
        add(vBox,BorderLayout.PAGE_START);
        add(buttonPanel,BorderLayout.PAGE_END);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == button0) {
			String texto = text.getText();
			texto +="0";
			text.setText(texto);
		}
		if (e.getSource() == button1) {
			String texto = text.getText();
			texto +="1";
			text.setText(texto);
		}
		
	}
	
	public static void createAndShowGUI() {
		  JFrame.setDefaultLookAndFeelDecorated(true);
		  JDialog.setDefaultLookAndFeelDecorated(true);
		  JFrame frame = new JFrame("Binarios");
	      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		  JComponent newContentPane = new Calculadora();
		  newContentPane.setOpaque(true); //content panes must be opaque
		  frame.setContentPane(newContentPane);
		  frame.pack();
		  frame.setVisible(true);
		}
	
	 public static void main(String args[]){
		  javax.swing.SwingUtilities.invokeLater(new Runnable() {
		    public void run() {
		          createAndShowGUI();
		        }
		   });
		}

}

qualquer duvida post ai

Criado 27 de maio de 2006
Ultima resposta 27 de mai. de 2006
Respostas 1
Participantes 2