如何在输入字段中添加固定值/符号

时间:2015-03-12 04:45:35

标签: java

http://i.stack.imgur.com/84fUa.png

我想在相应的输入字段中添加一个符号,如上面的链接(屏幕截图)所示,并使符号在JTextField内部锁定,当用户键入某个内容时,该值不得与符号重叠。

例如,当用户想要键入年利率时,文本光标应出现在%符号的前面。

我需要添加哪些代码才能执行上述请求。

这是我的代码:

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoanCalculator extends JApplet {

    // Create text fields for interest rate, years
    // loan amount, monthly payment, and total payment
    private JTextField jtfAnnualInterestRate = new JTextField();
    private JTextField jtfNumberOfYears = new JTextField();
    private JTextField jtfLoanAmount = new JTextField();
    private JTextField jtfMonthlyPayment = new JTextField();
    private JTextField jtfTotalPayment = new JTextField();

    // Create a Compute Payment button
    private JButton jbtComputeLoan = new JButton("Compute Payment");

    public static void main(String[] args) {

        JFrame frame = new JFrame("Loan Payment Calculator");
        LoanCalculator applet = new LoanCalculator();
        frame.add(applet, BorderLayout.CENTER);
        applet.init();

        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(320, 230);
        frame.setVisible(true);
    }

    public void init() {

        jtfMonthlyPayment.setEditable(false);
        jtfTotalPayment.setEditable(false);

        // Right align text fields
        jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
        jtfNumberOfYears.setHorizontalAlignment(JTextField.RIGHT);
        jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);
        jtfMonthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
        jtfTotalPayment.setHorizontalAlignment(JTextField.RIGHT);

        // Panel p1 to hold labels and text fields
        JPanel p1 = new JPanel(new GridLayout(5, 2));
        p1.add(new JLabel("Annual Interest Rate : ", SwingConstants.RIGHT));
        p1.add(jtfAnnualInterestRate);
        p1.add(new JLabel("Number of Years : ", SwingConstants.RIGHT));
        p1.add(jtfNumberOfYears);
        p1.add(new JLabel("Loan Amount : ", SwingConstants.RIGHT));
        p1.add(jtfLoanAmount);
        p1.add(new JLabel("Monthly Payment : ", SwingConstants.RIGHT));
        p1.add(jtfMonthlyPayment);
        p1.add(new JLabel("Total Payment : ", SwingConstants.RIGHT));
        p1.add(jtfTotalPayment);
        p1.setBorder(new
                TitledBorder("Enter interest rate, year and loan amount"));

        // Panel p2 to hold the button
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p2.add(jbtComputeLoan);

        // Add the panels to the frame
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);

        // Register listener
        jbtComputeLoan.addActionListener(new ButtonListener());
    }

    /**
     * Handle the Compute Payment button
     */
    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Get values from text fields
            double interest =
                    Double.parseDouble(jtfAnnualInterestRate.getText());
            int year =
                    Integer.parseInt(jtfNumberOfYears.getText());
            double loanAmount =
                    Double.parseDouble(jtfLoanAmount.getText());

            // Create a loan object
            Loan loan = new Loan(interest, year, loanAmount);

            // Display monthly payment and total payment
            jtfMonthlyPayment.setText(String.format("%.2f",
                    loan.getMonthlyPayment()));
            jtfTotalPayment.setText(String.format("%.2f",
                    loan.getTotalPayment()));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如何设置文本字段的提示?看看这个:xswingx