我正在尝试对“检查余额”按钮进行编程,以显示正确的余额,方法是
使用Checks类中的getBalance()方法。

但是我收到错误消息

TravelCheck.java:130:错误:无法从静态上下文引用非静态方法getBalance()
            JOptionPane.showMessageDialog(null,“您在美国的支票总额为” + Checks.getBalance(),
         ^

有谁知道如何纠正这个???

这是我的项目说明的链接

http://www.oakton.edu/user/2/somplski/c156/manual/csc156mp7.pdf

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TravelCheck extends JFrame
{
    private static final int WIDTH = 440, HEIGHT = 300;
    private JLabel Title, Instruct;
    private JButton pesoB, francB, euroB, usB, checkbalanceB, exitB;
    private ButtonHandler bHandler;

    public TravelCheck()
    {
        Title = new JLabel("Cash Your Traveler's Check ", SwingConstants.CENTER);
    Instruct = new JLabel("Click the button that matches your currency type", SwingConstants.CENTER);

        pesoB = new JButton(TravelString.PESO_S) ;
    bHandler = new ButtonHandler();
    pesoB.addActionListener(bHandler);

    francB = new JButton(TravelString.FRANC_S) ;
    bHandler = new ButtonHandler();
    francB.addActionListener(bHandler);

    euroB = new JButton(TravelString.EURO_S) ;
    bHandler = new ButtonHandler();
    euroB.addActionListener(bHandler);

    usB = new JButton(TravelString.US_S) ;
    bHandler = new ButtonHandler();
    usB.addActionListener(bHandler);

    checkbalanceB = new JButton(TravelString.CHECK_S);
        bHandler = new ButtonHandler();
        checkbalanceB.addActionListener(bHandler);

    exitB = new JButton(TravelString.EXIT_S);
        bHandler = new ButtonHandler();
        exitB.addActionListener(bHandler);

    setTitle("Traveler's Check Machine");

    Container pane = getContentPane();
    pane.setLayout(new GridLayout(8,0));

    pane.add(Title);
    pane.add(Instruct);

    pane.add(pesoB);
    pane.add(francB);
    pane.add(euroB);
    pane.add(usB);
    pane.add(checkbalanceB);
    pane.add(exitB);

    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public class TravelString
    {
    public static final String
    PESO_S = "Mexican peso",
    FRANC_S = "Swiss franc",
    EURO_S = "Euro dollar",
    US_S = "U.S. dollar",
    CHECK_S = "Check Balance",
    EXIT_S = "Exit";
    }

    private void transaction(String CurrencyString)
    {
        JOptionPane.showMessageDialog(null, "The " + CurrencyString + " button was used",
                      "Button Message Demo", JOptionPane.PLAIN_MESSAGE);
    }


    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals(TravelString.EXIT_S))
        System.exit(0);
        else if (e.getActionCommand().equals(TravelString.PESO_S))
        transaction(TravelString.PESO_S);
        else if (e.getActionCommand().equals(TravelString.FRANC_S))
        transaction(TravelString.FRANC_S);
        else if (e.getActionCommand().equals(TravelString.EURO_S))
        transaction(TravelString.EURO_S);
        else if (e.getActionCommand().equals(TravelString.US_S))
        transaction(TravelString.US_S);
        else if (e.getActionCommand().equals(TravelString.CHECK_S))
                JOptionPane.showMessageDialog(null, "The amount of your checks total in US is" + Checks.getBalance(),
                          "Traveler's checks balance", JOptionPane.PLAIN_MESSAGE);
    }
    }

    public class Checks
    {
    private static final double
        PESO = 2.0, FRANC = 3.0, EURO = 4.0;

    private int numberOfChecks;
    private int faceAmount;
    private String country;
    private double balance;

    public Checks (String s)
    {
        numberOfChecks = 0;
        faceAmount = 0;
        balance =  0.0;
        if (s.equals(TravelString.PESO_S) || s.equals(TravelString.FRANC_S) ||
        s.equals(TravelString.EURO_S) || s.equals(TravelString.US_S))
        country = s;
        else
        {
        country = TravelString.US_S;
        System.err.println("### Bad Checks constructor invocation. US Check by default ###");
        }
    }

    public int getNumber()
    {
        return numberOfChecks;
    }

    public String getCountry()
    {
        return country;
    }

    public int getFace()
    {
        return faceAmount;
    }

    public double getBalance()
    {
        return balance;
    }

    public void deposit (int number, int denomination)
    {
        if (number > 0 && denomination > 0)
        {
        if (faceAmount == 0)
        {
        faceAmount = denomination;
        numberOfChecks = number;
        balance += faceAmount * number * conversion();
        }
        else if (faceAmount == denomination)
        {
        numberOfChecks += number;
        balance += faceAmount * number * conversion();
        }
        else
            System.err.println("### Non-existent denomination error in deposit() ###");
        }
        else
        System.err.println("### Non-positive parameter error in deposit() ###");
    }

    public void withdrawal(int number)
    {
        if (number < 0)
        System.err.println("### Negative parameter error in withdrawal() ###");
        else if (balance < number * faceAmount * conversion())
        System.err.println("### Overdraw error in withdrawal() ###");
        else
        {
        numberOfChecks -= number;
        balance -= faceAmount * number * conversion();

        if (numberOfChecks == 0)
            faceAmount = 0;
        }
    }

    private double conversion()
    {
        double retval = 0.0;

        if (country.equals(TravelString.US_S))
        retval = 1.0;
        else if (country.equals(TravelString.PESO_S))
        retval = 1.0/PESO;
        else if (country.equals(TravelString.FRANC_S))
        retval = 1.0/FRANC;
        else if (country.equals(TravelString.EURO_S))
        retval = 1.0/EURO;
        return retval;
    }
    }


    public static void main(String[] args)
    {
    TravelCheck travelcheck = new TravelCheck();
    }

}

最佳答案

本质上,您需要调用Checks对象的getBalance()方法,而不是静态地调用它。将Checks.getBalance()替换为ch.getBalance(),其中ch是已实例化并传递到TravelCheck类的Checks类的对象。

为此,您将必须创建Checks类的对象。由于Checks类位于TravelCheck类中,因此您可以在TravelCheck类的构造函数内创建一个对象(这只是一个示例。您应该确定理想情况下在何处创建对象):

public TravelCheck()
{
    Title = new JLabel("Cash Your Traveler's Check ", SwingConstants.CENTER);
    Instruct = new JLabel("Click the button that matches your currency type", SwingConstants.CENTER);
    Checks ch=new Checks(TravelString.US_S); //Object creation with US currency variable passed in contructor
    ....


然后更改第130行以反映此更改,以使其使用以下对象:

OptionPane.showMessageDialog(null, "The amount of your checks total in US is" + ch.getBalance(),

10-06 13:34