Dúvida no código

3 respostas
Donizeti

Galera to com um problema no código abaixo,
na linha 23 ele gera o seguinte erro:
"non-static variable this cannot be referenced from a static context. "
se alguém puder me ajudar agradeço desde já.wlu

package calculadora;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 *
 * @author Usuario
 */
public class Calculator {

    /**
     * @param args the command line argumentsk
     */
    public static void main(String[] args) {
        // TODO code application logic here
       CalculatorFrame frame = new CalculatorFrame();  
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
       
    }
    class CalculatorFrame extends JFrame
    {
        public CalculatorFrame()
        { 
            setTitle("Calculadora");
            CalculatorPanel panel= new CalculatorPanel();
            add(panel);
            pack();
           
        }   
    }
    class CalculatorPanel extends JPanel{
        public CalculatorPanel(){
            setLayout(new BorderLayout());
            result=0;
            lastCommand = "=";
            start=true;
            
            display = new JButton("0");
            display.setEnabled(false);
            add(display, BorderLayout.NORTH);
            
            ActionListener insert = new InserAction();
            ActionListener command = new CommandAction();
            
            panel = new JPanel();
            panel.setLayout(new GridLayout(4, 4));
            addButton("7", insert);
            addButton("8", insert);
            addButton("9", insert);
            addButton("/", command);
            
            addButton("4", insert);
            addButton("5", insert);
            addButton("6", insert);
            addButton("*", command);
            
            addButton("1", insert);
            addButton("2", insert);
            addButton("3", insert);
            addButton("-", command);
        
            addButton("0", insert);
            addButton(".", insert);
            addButton("=", command);
            addButton("+", command);
            
            add(panel, BorderLayout.CENTER);
        }
    }
    private void addButton(String label , ActionListener listener)
    {
        JButton button= new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    }    
    private class InserAction implements ActionListener{
            public void actionPerformed (ActionEvent event){
                String input = event.getActionCommand();
                if(start)
                {
                    display.setText(" ");
                    start=false;
                }
                display.setText(display.getText()+ input);
            }
    }
    private class CommandAction implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String command = event.getActionCommand();
            if(start)
            {
                if (command.equals("-"))
                {
                    display.setText(command);
                    start=false;
                }  else
                 lastCommand = command;
                       
            }else
            {
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start=true;
            }
        }
    }
      public void calculate (double x)
      {
       if(lastCommand.equals("+"))result+= x;
       else if(lastCommand.equals("+"))result+= x;
       else if(lastCommand.equals("+"))result+= x;
       else if(lastCommand.equals("+"))result+= x;
       else if(lastCommand.equals("+"))result+= x;
            display.setText(""+result);
      }
                    
    
      private JButton display;
      private JPanel panel;
      private double result;
      private String lastCommand;
      private boolean start;    
}

3 Respostas

Alkamavo

estas a aceber a uma variavel nao estatica....tens de definir a classe que instancia esta variavel com estatica

package calculadora;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 13. * 14. *
 * 
 * @author Usuario 15.
 */
public class Calculator {

	/**
	 * 19. *
	 * 
	 * @param args
	 *            the command line argumentsk 20.
	 */
	public static void main(String[] args) {
		// TODO code application logic here
		CalculatorFrame frame = new CalculatorFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

	static class CalculatorFrame extends JFrame {
		public CalculatorFrame() {
			setTitle("Calculadora");
			CalculatorPanel panel = new CalculatorPanel();
			add(panel);
			pack();

		}
	}

	static class CalculatorPanel extends JPanel {
		public CalculatorPanel() {
			setLayout(new BorderLayout());
			result = 0;
			lastCommand = "=";
			start = true;

			display = new JButton("0");
			display.setEnabled(false);
			add(display, BorderLayout.NORTH);

			ActionListener insert = new InserAction();
			ActionListener command = new CommandAction();

			panel = new JPanel();
			panel.setLayout(new GridLayout(4, 4));
			addButton("7", insert);
			addButton("8", insert);
			addButton("9", insert);
			addButton("/", command);

			addButton("4", insert);
			addButton("5", insert);
			addButton("6", insert);
			addButton("*", command);

			addButton("1", insert);
			addButton("2", insert);
			addButton("3", insert);
			addButton("-", command);

			addButton("0", insert);
			addButton(".", insert);
			addButton("=", command);
			addButton("+", command);

			add(panel, BorderLayout.CENTER);
		}
	}

	private static void addButton(String label, ActionListener listener) {
		JButton button = new JButton(label);
		button.addActionListener(listener);
		panel.add(button);
	}

	private static class InserAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String input = event.getActionCommand();
			if (start) {
				display.setText(" ");
				start = false;
			}
			display.setText(display.getText() + input);
		}
	}

	private static  class CommandAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String command = event.getActionCommand();
			if (start) {
				if (command.equals("-")) {
					display.setText(command);
					start = false;
				} else
					lastCommand = command;

			} else {
				calculate(Double.parseDouble(display.getText()));
				lastCommand = command;
				start = true;
			}
		}
	}

	public static void calculate(double x) {
		if (lastCommand.equals("+"))
			result += x;
		else if (lastCommand.equals("+"))
			result += x;
		else if (lastCommand.equals("+"))
			result += x;
		else if (lastCommand.equals("+"))
			result += x;
		else if (lastCommand.equals("+"))
			result += x;
		display.setText("" + result);
	}

	private static JButton display;
	private static JPanel panel;
	private static double result;
	private static String lastCommand;
	private static boolean start;
}

fiz algumas alteraçoes..
ai esta

Donizeti

Wleu Alkamavo agora compilo legal…

blackfalcon

Acabei de postar em outro topico e posto aqui…

http://www.javafree.org/javabb/viewtopic.jbb?t=6941&Cap-2--Modificadores

Muito legal isso, da um bom conceito sobre os Modificadores em Java, que ate antes de ler isso tinha bastante duvidas, agora sumiu bastante :slight_smile:

Abraços

Criado 21 de junho de 2008
Ultima resposta 21 de jun. de 2008
Respostas 3
Participantes 3