我正在尝试编程“检查余额”按钮以显示正确的余额 在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();
}
}
答案 0 :(得分:2)
您基本上需要调用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(),
答案 1 :(得分:1)
你正在调用Checks.getBalance(),这样做,Checks需要无需实例化即可访问,意味着它应该是一个静态类,getBalance()函数也应该是静态的。
您没有类Checks的对象。在实例化之前,您将不会拥有任何值。因此,创建它的对象,然后访问getBalance()函数。
为什么你在课堂上有课?将它保持在外面以确保重新使用是不是更容易?