English 中文(简体)
2. 调整标签、文本现场和纽特州规模
原标题:Adjust the label, text-field & button size

我写了Swaing的<代码>摊销/代码>,但看不好。 如何调整标签、文本现场和营地的规模?

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class AmortizationLoanSchedule extends JApplet implements ActionListener {

    private JLabel LoanAmount = new JLabel("Loan Amount");
    private JLabel NumberOfYears = new JLabel("Number Of Years");
    private JLabel InterestRate = new JLabel("Interest Rate (Annual)");

    private JTextField jtfLoanAmount = new JTextField(10);
    private JTextField jtfNumberOfYears = new JTextField(10);
    private JTextField jtfInterestRate = new JTextField(10);

    // Calculate button is also needed
    private JButton jbtCalculate = new JButton("Amortize Loan");

    // ...and a text area where the results will be displayed
    private JTextArea jtaResults = new JTextArea();

    public void init() {
        try {
            // Panel p1 will hold the input
            JPanel p1 = new JPanel();
            p1.setLayout(new GridLayout(3, 3));
            p1.add(LoanAmount);
            p1.add(jtfLoanAmount);
            p1.add(NumberOfYears);
            p1.add(jtfNumberOfYears);
            p1.add(InterestRate);
            p1.add(jtfInterestRate);

            // Panel p2 will hold panel p1 and the calculate button
            JPanel p2 = new JPanel();
            p2.setLayout(new BorderLayout());
            p2
                    .setBorder(new TitledBorder(
                            "Enter loan amount, Number of years and annual interest rate"));
            p2.add(p1, BorderLayout.BEFORE_FIRST_LINE);
            p2.add(jbtCalculate, BorderLayout.AFTER_LAST_LINE);

            // Action listener for the button
            jbtCalculate.addActionListener(this);

            // Make the text area scrollable and uneditable
            JScrollPane scrollPane = new JScrollPane(jtaResults);
            jtaResults.setRows(12);
            jtaResults.setEditable(false);

            // Place the two panels to the applet
            getContentPane().add(p2, BorderLayout.NORTH);
            getContentPane().add(scrollPane, BorderLayout.SOUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jbtCalculate)
            calculateLoan();
        else
            System.out.println("you will never see this text!");

    }

    public void calculateLoan() {
        if ((jtfNumberOfYears.getText().equals(""))
                || (jtfLoanAmount.getText().equals(""))
                || (jtfInterestRate.getText().equals(""))) {
            JOptionPane.showMessageDialog(null, "All fields are mandatory",
                    null, 1);
        } else {
            int numberOfYears = Integer.parseInt(jtfNumberOfYears.getText());
            double loanAmount = Double.parseDouble(jtfLoanAmount.getText());
            double annualInterestRate = (Double.parseDouble(jtfInterestRate
                    .getText())) / 100;

            double monthlyInterestRate = annualInterestRate / 12;
            double numberOfMonths = numberOfYears * 12;
            double monthlyPayment = loanAmount
                    * (monthlyInterestRate / (1 - Math.pow(
                            1 + monthlyInterestRate, -numberOfMonths)));
            double totalPayment = monthlyPayment * numberOfMonths;
            double balance = loanAmount;
            double interest;
            double principal;

            jtaResults.append("Payment#	" + "Interest	" + "Principal	"
                    + "Balance

");

            for (int i = 0; i < numberOfYears * 12; i++) {
                interest = (int) (monthlyInterestRate * balance * 100) / 100.0;
                principal = (int) ((monthlyPayment - interest) * 100) / 100.0;
                balance = (int) ((balance - principal) * 100) / 100.0;

                jtaResults.append(i + 1 + "	" + interest + "	" + principal
                        + "	" + balance + "
");
            }

            jtaResults.append("

Monthly Payment: $"
                    + (int) (monthlyPayment * 100) / 100.0 + "
");
            jtaResults.append("Total Payment: $" + (int) (totalPayment * 100)
                    / 100.0 + "

");
        }
    }
}

“AmortizationLoanStorule”/

Update

这是必要的。

“entergraph

最佳回答

“AmortizationLayout”/

import java.awt.*;
import javax.swing.*;

import javax.swing.border.TitledBorder;

class AmortizationLayout {

    AmortizationLayout() {
        JPanel gui = new JPanel(new BorderLayout(2,2));

        JPanel labelFields = new JPanel(new BorderLayout(2,2));
        labelFields.setBorder(new TitledBorder("BorderLayout"));

        JPanel labels = new JPanel(new GridLayout(0,1,1,1));
        labels.setBorder(new TitledBorder("GridLayout"));
        JPanel fields = new JPanel(new GridLayout(0,1,1,1));
        fields.setBorder(new TitledBorder("GridLayout"));

        for (int ii=1; ii<4; ii++) {
            labels.add(new JLabel("Label " + ii));
            // if these were of different size, it would be necessary to
            // constrain them using another panel
            fields.add(new JTextField(10));
        }

        labelFields.add(labels, BorderLayout.CENTER);
        labelFields.add(fields, BorderLayout.EAST);

        JPanel guiCenter = new JPanel(new BorderLayout(2,2));
        guiCenter.setBorder(new TitledBorder("BorderLayout"));
        JPanel buttonConstrain = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonConstrain.setBorder(new TitledBorder("FlowLayout"));
        buttonConstrain.add( new JButton("Click Me") );
        guiCenter.add( buttonConstrain, BorderLayout.NORTH );

        guiCenter.add(new JScrollPane(new JTextArea(5,30)));

        gui.add(labelFields, BorderLayout.NORTH);
        gui.add(guiCenter, BorderLayout.CENTER);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) throws Exception {
        //Create the GUI on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new AmortizationLayout();
            }
        });
    }
}
问题回答

(1) 错误

2) to avoiding input of non_Digits chars use DocumentFilter f或 plain JTextField





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签