我正在为一个学校项目制作一个贷款计算器,一旦按下按钮txtMonthlyPayment
,我就无法弄清楚如何将txtTotalPayment
和ComputePayment
的背景色更改为黄色。另外,如果可以的话,您如何更改在文本字段中输入的文本的字体大小。
感谢堆! ;)
import java.awt.*; //enables java GUI
import java.awt.event.*;//enables the user to respond, such as using the mouse and keyboard
import javax.swing.*;//more awt and swing imports to be associated with components / objects
import javax.swing.border.TitledBorder; //title of the JFrame window
import java.awt.event.ActionListener; // used to set actions for certain components / objects
import java.awt.Color; // used to alter colour of components / objects
import java.awt.Font; // used to change font size / style
public class LoanCalculator extends JFrame {
//create text fields for interest rate, years, loan amount, monthly pmt and total pmt
private JTextField txtAnnualInterestRate = new JTextField();//text fields that appear, their names
private JTextField txtNumberOfYears = new JTextField();
private JTextField txtLoanAmount = new JTextField();
private JTextField txtMonthlyPayment = new JTextField();//define the control here, then add to the panel, below
private JTextField txtTotalPayment = new JTextField();
//create a compute payment button
private JButton jbtComputeLoan = new JButton("Compute Payment"); //text appears on the button
public LoanCalculator()
{
//craft a panel to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(5, 2));//dimensions specified
p1.add(new JLabel("Annual Interest Rate"));
p1.add(txtAnnualInterestRate);
p1.add(new JLabel("Number of Years"));
p1.add(txtNumberOfYears);
p1.add(new JLabel("Loan Amount"));
p1.add(txtLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(txtMonthlyPayment);
p1.add(new JLabel("Total Payment"));
p1.add(txtTotalPayment);
p1.setBorder(new TitledBorder("Enter loan amount, interest rate, and years"));
//create another panel, to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtComputeLoan); //remember defining this in the private section, earlier?
//add your panels to the frame
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
//register a listener, who will 'listen' to updates to the frame, and interactions with it
jbtComputeLoan.addActionListener(new ButtonListener());
}//end constructor
/**
handle the compute payment buttons events
*/
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e )
{
//get values from the text fields
double interest = Double.parseDouble(txtAnnualInterestRate.getText());
int year = Integer.parseInt(txtNumberOfYears.getText());
double loanAmount = Double.parseDouble(txtLoanAmount.getText());
Loan loan = new Loan(interest, year, loanAmount);//see page 376 for source for loan object
//display the monthly total payment and monthly payment
txtMonthlyPayment.setText(String.format("%.2f", loan.getMonthlyPayment()));
txtTotalPayment.setText(String.format("%.2f", loan.getTotalPayment()));
}//end action performed
}
/** craft a loan class, to store loan details */
public class Loan
{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** default constructor */
public Loan()
{
this(2.5, 1, 1000); //the default takes effect when other constructors are not used
}
/** construct a loan with a specified annual interest rate, number of years, and loan amount
* @param annualInterestRate
* @param numberOfYears
* @param loanAmount */
public Loan(double annualInterestRate, int numberOfYears, double loanAmount)
{
this.annualInterestRate = annualInterestRate;//the users determine / specify the interest rate
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** return the annual interest rate
* @return */
public double getAnnualInterestRate()
{
return annualInterestRate;
}
//set the annual interest rate
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;//from the incoming data to the function
}
//return the number of years
public int getNumberOfYears()
{
return numberOfYears;//private variable, which needs a get/set couplet
}
public void setNumberOfYears(int numberOfYears)
{
this.numberOfYears = numberOfYears;
}
public double getLoanAmount()
{
return loanAmount;
}
public double getMonthlyPayment()
{
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1-(1 / Math.pow(1+monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment()
{
double totalPayment = getMonthlyPayment() * numberOfYears *12;
return totalPayment;
}
public java.util.Date getLoanDate()
{
return loanDate;//conveys a private variable, loan date, to any accessing class, who wants to know
}
}//end of loan class
public static void main(String[] args) {
LoanCalculator frame = new LoanCalculator();
frame.pack();
frame.setTitle("Loan Calculator");
frame.setLocationRelativeTo(null); //forces the new window to appear in the center
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//what happens when you touch the X in the above left corner
frame.setVisible(true);//so it appears
}
}
答案 0 :(得分:0)
您需要在actionPerformed
中进行此操作。
public void actionPerformed(ActionEvent e )
{
//get values from the text fields
double interest = Double.parseDouble(txtAnnualInterestRate.getText());
int year = Integer.parseInt(txtNumberOfYears.getText());
double loanAmount = Double.parseDouble(txtLoanAmount.getText());
Loan loan = new Loan(interest, year, loanAmount);//see page 376 for source for loan object
//display the monthly total payment and monthly payment
txtMonthlyPayment.setText(String.format("%.2f", loan.getMonthlyPayment()));
txtMonthlyPayment.setBackground(Color.YELLOW);
txtTotalPayment.setText(String.format("%.2f", loan.getTotalPayment()));
txtTotalPayment.setBackground(Color.YELLOW);
}//end action performed
第二个问题,您可以使用java.awt.Font
设置字体。例如,
txtMonthlyPayment.setFont(new Font("font name",font_style(int),size(int));