按下按钮后,如何更改JTextField的背景颜色?

问题描述 投票:0回答:1

即时通讯为学校项目制作贷款计算器,我无法弄清楚如何按下按钮txtMonthlyPayment后将txtTotalPaymentComputePayment的背景颜色更改为黄色。如果可以的话,作为奖励,如何更改在文本字段中输入的文本的字体大小。

谢谢堆! ;)

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
 }
}
java swing user-interface jframe jtextfield
1个回答
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));
© www.soinside.com 2019 - 2024. All rights reserved.