简体   繁体   中英

Method cannot be resolved or is not a field

I have 3 classes; a start class, a kassa class and a kassapaneel class. The kassapaneel is my panel, it is the graphical side if I am correct. In my kassa class I have a method called getSubtotaal, it's extended to my kassapaneel but it doesn't seem to recognize it. Same goes for berekenBTW. And the totaal variable too.

My kassa class:

package Opdrachten;

import javax.swing.*;

public class Kassa extends JPanel{
private double subtotaal;
private final double PERCENTAGE_BTW = 19.0;

public void telOp(double bedrag) {
    subtotaal += bedrag;
}

public double getSubtotaal() {
    return subtotaal;
}

public void reset() {
    subtotaal = 0;
}

public double berekenBTW() {
    return subtotaal - berekenSubtotaalExBTW();
}

public double berekenSubtotaalExBTW() {
    return subtotaal / (1 + PERCENTAGE_BTW / 100);
}
}

My kassapaneel (panel):

package Opdrachten;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

public class Kassapaneel extends Kassa {
private JTextField invoerVak;
  private JTextField subtotaalVak, BTWVak, exBTWVak, totaalVak;
  private JLabel invoerLabel, subtotaalLabel, BTWLabel, exBTWLabel, totaalLabel;
  private JButton totaalKnop, resetKnop;
  private Kassa kassa;



  public Kassapaneel() 
  {
    setLayout( null ); 
    //maak kassa
    kassa = new Kassa();


    //maak knop
    totaalKnop = new JButton("Totaal");
    totaalKnop.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Double subtotaal = kassa.getSubtotaal();
            Double btw = kassa.berekenBTW();
            Double exBtw = subtotaal - btw;

            subtotaalVak.setText(String.valueOf(subtotaal));
            BTWVak.setText(String.valueOf(btw));
            exBTWVak.setText(String.valueOf(exBtw));
        }
    });

    resetKnop = new JButton("Reset");
    resetKnop.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            kassa.reset();
        }
    });

    // Maak de tekstvakken
    invoerVak = new JTextField( 10 );
    invoerVak.setHorizontalAlignment( JTextField.RIGHT );
    invoerVak.addActionListener( new KnopHandler1() );

    subtotaalVak = new JTextField( 10 );
    subtotaalVak.setHorizontalAlignment( JTextField.RIGHT );
    subtotaalVak.addActionListener( new KnopHandler1() );
    subtotaalVak.setBackground( Color.LIGHT_GRAY );


    BTWVak = new JTextField( 10 );
    BTWVak.setHorizontalAlignment( JTextField.RIGHT );
    BTWVak.addActionListener( new KnopHandler1() );
    BTWVak.setBackground( Color.LIGHT_GRAY );


    exBTWVak = new JTextField( 10 );
    exBTWVak.setHorizontalAlignment( JTextField.RIGHT );
    exBTWVak.addActionListener( new KnopHandler1() );
    exBTWVak.setBackground( Color.LIGHT_GRAY );

    totaalVak = new JTextField( 10 );
    totaalVak.setHorizontalAlignment( JTextField.RIGHT );
    totaalVak.addActionListener( new KnopHandler1() );
    totaalVak.setBackground( Color.GREEN );




    // Maak de labels
    invoerLabel = new JLabel( "voer bedrag in" );
    subtotaalLabel = new JLabel("Subtotaal");
    BTWLabel = new JLabel ("BTW");
    exBTWLabel = new JLabel ("Totaal ex BTW");
    totaalLabel = new JLabel ("Totaal");


    // Bepaal van alle componenten de plaats en afmeting

    invoerVak.setBounds ( 100,50,120,20 );
    subtotaalVak.setBounds ( 100,80,120,20 );
    BTWVak.setBounds ( 100,110,120,20 );
    exBTWVak.setBounds ( 100,140,120,20 );
    totaalVak.setBounds ( 100,170,120,20 );

    totaalKnop.setBounds ( 230,50,100,20 );
    resetKnop.setBounds ( 230,80,100,20 );

    invoerLabel.setBounds ( 10,50,120,20 );
    subtotaalLabel.setBounds ( 10,80,120,20 );
    BTWLabel.setBounds ( 10,110,120,20 );
    exBTWLabel.setBounds ( 10,140,120,20 );
    totaalLabel.setBounds ( 10,170,120,20 );



    // Voeg de componenten toe aan het paneel
    add (invoerVak);
    add (subtotaalVak);
    add (BTWVak);
    add (exBTWVak);
    add (totaalVak);
    add (totaalKnop);
    add (resetKnop);
    add (invoerLabel);
    add (subtotaalLabel);
    add (BTWLabel);
    add (exBTWLabel);
    add (totaalLabel);

  }


  class KnopHandler1 implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
        String invoer = invoerVak.getText();
        double invoerBedrag = Double.parseDouble (invoer);

        //hiermee stop je het bedrag in het kassasysteem.
        kassa.telOp (invoerBedrag);
        kassa.berekenBTW();
        kassa.berekenSubtotaalExBTW();

        kassa.totaal = (kassa.getSubtotaal + kassa.berekenBTW);            
    }
  }

  class KnopHandler2 implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
        kassa.reset();

    }
  }

}

Here's an attempt to resolve some of your issues:

I removed the Kasaa object, because the Kassapaneel extends Kasaa . That means you'll have access to the protected+ methods and fields within Kasaa. I believe the issue you're experiencing was within the KnopHandler1 ActionListener. You were attempting to call fields that didn't exist, and they weren't methods because they didn't have the parenthesis at the end.

package Opdrachten;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

public class Kassapaneel extends Kassa {
private JTextField invoerVak;
  private JTextField subtotaalVak, BTWVak, exBTWVak, totaalVak;
  private JLabel invoerLabel, subtotaalLabel, BTWLabel, exBTWLabel, totaalLabel;
  private JButton totaalKnop, resetKnop;    

  public Kassapaneel() 
  {
    setLayout( null ); 

    //maak knop
    totaalKnop = new JButton("Totaal");
    totaalKnop.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Double subtotaal = getSubtotaal();
            Double btw = berekenBTW();
            Double exBtw = subtotaal - btw;

            subtotaalVak.setText(String.valueOf(subtotaal));
            BTWVak.setText(String.valueOf(btw));
            exBTWVak.setText(String.valueOf(exBtw));
        }
    });

    resetKnop = new JButton("Reset");
    resetKnop.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            reset();
        }
    });

    // Maak de tekstvakken
    invoerVak = new JTextField( 10 );
    invoerVak.setHorizontalAlignment( JTextField.RIGHT );
    invoerVak.addActionListener( new KnopHandler1() );

    subtotaalVak = new JTextField( 10 );
    subtotaalVak.setHorizontalAlignment( JTextField.RIGHT );
    subtotaalVak.addActionListener( new KnopHandler1() );
    subtotaalVak.setBackground( Color.LIGHT_GRAY );


    BTWVak = new JTextField( 10 );
    BTWVak.setHorizontalAlignment( JTextField.RIGHT );
    BTWVak.addActionListener( new KnopHandler1() );
    BTWVak.setBackground( Color.LIGHT_GRAY );


    exBTWVak = new JTextField( 10 );
    exBTWVak.setHorizontalAlignment( JTextField.RIGHT );
    exBTWVak.addActionListener( new KnopHandler1() );
    exBTWVak.setBackground( Color.LIGHT_GRAY );

    totaalVak = new JTextField( 10 );
    totaalVak.setHorizontalAlignment( JTextField.RIGHT );
    totaalVak.addActionListener( new KnopHandler1() );
    totaalVak.setBackground( Color.GREEN );




    // Maak de labels
    invoerLabel = new JLabel( "voer bedrag in" );
    subtotaalLabel = new JLabel("Subtotaal");
    BTWLabel = new JLabel ("BTW");
    exBTWLabel = new JLabel ("Totaal ex BTW");
    totaalLabel = new JLabel ("Totaal");


    // Bepaal van alle componenten de plaats en afmeting

    invoerVak.setBounds ( 100,50,120,20 );
    subtotaalVak.setBounds ( 100,80,120,20 );
    BTWVak.setBounds ( 100,110,120,20 );
    exBTWVak.setBounds ( 100,140,120,20 );
    totaalVak.setBounds ( 100,170,120,20 );

    totaalKnop.setBounds ( 230,50,100,20 );
    resetKnop.setBounds ( 230,80,100,20 );

    invoerLabel.setBounds ( 10,50,120,20 );
    subtotaalLabel.setBounds ( 10,80,120,20 );
    BTWLabel.setBounds ( 10,110,120,20 );
    exBTWLabel.setBounds ( 10,140,120,20 );
    totaalLabel.setBounds ( 10,170,120,20 );



    // Voeg de componenten toe aan het paneel
    add (invoerVak);
    add (subtotaalVak);
    add (BTWVak);
    add (exBTWVak);
    add (totaalVak);
    add (totaalKnop);
    add (resetKnop);
    add (invoerLabel);
    add (subtotaalLabel);
    add (BTWLabel);
    add (exBTWLabel);
    add (totaalLabel);

  }


  class KnopHandler1 implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
        String invoer = invoerVak.getText();
        double invoerBedrag = Double.parseDouble (invoer);

        //hiermee stop je het bedrag in het kassasysteem.
        telOp (invoerBedrag);
        berekenBTW();
        berekenSubtotaalExBTW();

        double totaal = (getSubtotaal() + berekenBTW()); //<--- Issue here.
        System.out.println("Totaal == " + totaal);
    }
  }

  class KnopHandler2 implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
        reset();
    }
  }

public static void main(String...banana)
{
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.add(new Kassapaneel());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
}

}

To elaborate on the comment //<--- Issue here , you were calling kassa.getSubtotaal . I changed this to getSubtotaal() . The former suggests that there's a field within the Kasaa class -- Eg: public double getSubtotal = 0; This doesn't exist, it is instead a method, so you should have been calling kassa.getSubtotaal() with the () at the end. In addition to this, you were attempting to set a variable kasaa.total where there is no resemblance of this method or variable within the Kasaa class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM