繁体   English   中英

方法无法解析或不是字段

[英]Method cannot be resolved or is not a field

我有 3 个班级; 一个起始类,一个 kassa 类和一个 kassapaneel 类。 kassapaneel 是我的面板,如果我是对的,它就是图形方面。 在我的 kassa 类中,我有一个名为 getSubtotaal 的方法,它扩展到我的 kassapaneel 但它似乎无法识别它。 berekenBTW 也是如此。 还有总变量。

我的卡萨课:

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);
}
}

我的 kassapaneel(面板):

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();

    }
  }

}

以下是解决您的一些问题的尝试:

我删除了 Kasaa 对象,因为Kassapaneel extends Kasaa 这意味着您可以访问 Kasaa 中的 protected+ 方法和字段。 我相信您遇到的问题是在KnopHandler1 ActionListener 中。 您试图调用不存在的字段,它们不是方法,因为它们末尾没有括号。

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);
}

}

要详细说明注释//<--- Issue here ,您正在调用kassa.getSubtotaal 我将其更改为getSubtotaal() 前者表明Kasaa类中有一个字段——例如: public double getSubtotal = 0; 这不存在,它是不是一个方法,所以你应该一直呼吁kassa.getSubtotaal()()结尾。 除此之外,您还试图设置一个变量kasaa.total ,该变量与Kasaa类中的此方法或变量没有相似之处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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