我正在为学校制定一个代码,你可以选择一种贷款类型,然后它会花掉你所欠的金额,并告诉你需要多长时间来偿还贷款。我使用了一些基本的JTextField来允许用户输入,然后使用.getText()来检索信息。但是,当我使用该字段时,它会在我的控制台中返回nullnull。我一直试图解决这个问题并且已经改变了很多代码,而且我对编码很新,所以可能有点难以理解。如果有人可以帮我解决为什么它会返回null,我会感到沮丧吗?
CODE HERE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class Loan1 extends JFrame implements ActionListener
{
String paymentTotal, total;
double a;
double b;
double g;
String Total;
FlowLayout flowLayout = new FlowLayout();
private JButton bl = new JButton("Business Loan");
private JButton pl = new JButton("Personal Loan");
private JButton cl = new JButton("Create a Loan");
private JButton submit = new JButton("Submit");
TextField textField = new TextField(20);
JLabel thing = new JLabel("How long were you looking to payoff the loan?");
JLabel thing2 = new JLabel("you will have to pay " + g + " $ a month");
public Loan1(){
setLayout(flowLayout);
add("Business Loan",bl);
add("Personal Loan",pl);
add("Create a Loan",cl);
bl.addActionListener(this);
pl.addActionListener(this);
cl.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bl || e.getSource() == pl
|| e.getSource() == cl)
payment();
if (e.getSource() == submit) {
Submit();
}
repaint();
//b = new Double(Total);
System.out.print(Total);
System.out.print(paymentTotal);
add(thing);
TextField textField = new TextField(20);
String text = textField.getText();
add(textField);
remove(bl);
remove(cl);
remove(pl);
if(text.equals("1")){
a = 1;
//g = paymentTotal/a *12
add(thing2);
}
if(text.equals("2")){
a = 2;
//g = paymentTotal/a *12
add(thing2);
}
if(text.equals("3")){
a = 3;
//g = paymentTotal/a *12
add(thing2);
}
if(text.equals("4")){
a = 1;
//g = paymentTotal/a *12
add(thing2);
}
if(text.equals("5")){
a = 1;
//g = paymentTotal/a *12
add(thing2);
}
}
public static void main(String[] args)
{
Loan1 JBL = new Loan1();
JBL.setSize(250,250);
JBL.setVisible(true);
}
public void payment() {
JLabel thing1 = new JLabel("How much money
were you looking to get a loan for?");
add(thing1);
add(textField);
add(submit);
}
public void Submit (){
String Total = textField.getText();
paymentTotal = Total;
System.out.print(Total);
remove(textField);
}
}
答案 0 :(得分:0)
首先:在您的方法付款()中,您忘了:
ID | Name | Friend | Enemy | Alien | Monster |
35 | John | Friend | -blank- | -blank- | Monster |
42 | Eric | -blank- | -blank- | -blank- | -blank- |
为什么你没有为每个按钮指定自己的actionPerformed?