我遇到以下代码的问题,我已经详细说明了我遇到的问题以及详细的问题,但以防这里的问题就在眼前。当我单击Show Table按钮时,它不会在表格中显示我的计算,但是如果我在If语句中的某些位置添加了一个writeline(“get working”),它就可以完美地运行。我只是不确定我需要做什么才能在桌子上显示计算。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/*
* Developer:
* Date: 2/8/2016
* Purpose:Enter the Loan amount and number of years. Display on the table the Interest, Monthly Payment and total payment.
* in the number of years from a text field, and it should display the
* monthly and total payment
*/
public class Exercise17_13 extends JFrame {
// add Panes, Panels and Labels
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel1 = new JPanel();
JTextArea jta = new JTextArea();
TitledBorder titledBorder;
JLabel jlblNumberYears = new JLabel("Number of Years");
JLabel jlbLoanAmount = new JLabel("Loan Amount");
JTextField jtfNumberYears = new JTextField(3);
JTextField jtfLoanAmount = new JTextField(8);
ButtonGroup btg = new ButtonGroup();
JButton jbtShowTable = new JButton();
public Exercise17_13() {
//sets a Border around the program
titledBorder = new TitledBorder("");
this.setSize(new Dimension(400, 300));
jPanel1.setBorder(titledBorder);
titledBorder.setTitle("");
//add the actionListeners
jtfNumberYears.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtfNumberYears_actionPerformed(e);
}
});
//set the ShowTable button to receive ActionListener
jbtShowTable.setText("Show Table");
jbtShowTable.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jbtShowTable_actionPerformed(e);
}
});
//set LoanAmount to receive ActionListener
jtfLoanAmount.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtfLoanAmount_actionPerformed(e);
}
});
//set each Panel to center using null
add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jta, null);
add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jlbLoanAmount, null);
jPanel1.add(jtfLoanAmount, null);
jPanel1.add(jlblNumberYears, null);
jPanel1.add(jtfNumberYears, null);
jPanel1.add(jbtShowTable, null);
btg.add(jbtShowTable);
}
//set the default frame of the program
public static void main(String[] args) {
Exercise17_13 frame = new Exercise17_13();
frame.setTitle("Exercise 17_13");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//sets LoanAmount to an editable action field
void jtfLoanAmount_actionPerformed(ActionEvent e) {
jtfLoanAmount.setEditable(true);
}
//sets number of years to an editable action field
void jtfNumberYears_actionPerformed(ActionEvent e) {
jtfNumberYears.setEditable(true);
}
//sets the show table button action performed variables
void jbtShowTable_actionPerformed(ActionEvent e) {
double number;
double denomination;
double x;
double y;
/* this part of the program is supposed to display the loanAmount and loanYears calculation
* but it currently does not display anything on the table. If I comment out the starting
* if (jbtShowTable.isSelected()) then it displays the System.out.Println("working") just
* fine on the table but not the calculations. I am not sure where I went wrong with
* displaying the results on the table.
*/
//this does calculations for the LoanAmount and Years and
// if (jbtShowTable.isSelected()) {
//System.out.println("working");
if (e.getSource() == jtfLoanAmount) {
double totalAmount = Double.parseDouble(jtfLoanAmount.getText().trim());
jtfLoanAmount.setEditable(true);
jtfNumberYears.setEditable(true);
if (e.getSource() == jtfNumberYears) { //getSource returns the object on which the event initally occured
double numOfYears = Double.parseDouble(jtfNumberYears.getText().trim());
jtfLoanAmount.setEditable(false);
jtfNumberYears.setEditable(false);
for (double intRate = 5; intRate <= 8; intRate = Math.round((intRate + .125) * 1000.0) / 1000.0) {
number = intRate / 100.0 * totalAmount / 12;
x = -(12 * numOfYears);
y = (intRate / 100.0 / 12) + 1.0;
denomination = 1.0 - Math.pow(y, x);
jta.setText(new Double(intRate + " " + Math.round((number / denomination) * 100.0) / 100.0 + " "
+ Math.round(((number / denomination) * 12 * numOfYears) * 100.0) / 100.0).toString());
}
}
}
}
}
答案 0 :(得分:0)
好吧,它有一些问题:
if (e.getSource() == ...)
陈述的内容是什么?只有在按下按钮时才会调用该方法,无需检查事件的来源。此外,当源是两个不同的东西时,你只是执行真正的魔法。马上。Double
解析String
。什么是甚至试图实现?append
(每行后可能附加换行符)效果更好。jbtShowTable.isSelected
:我不确定为什么认为这是必要的,因为您所使用的方法是由按钮注册的ActionListener
调用的。如果调用此方法,则某人必须按下按钮。 :)我做了这些更改后,数字开始出现在“表格”中。如果它们是正确的我不知道,但至少你现在可以看到它们。 :)