- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
所以我正在为一个类(class)做一个项目,因为我想学习这个叫做 Java 的可爱东西。好吧,无论如何,我正在尝试使用一些方法在我的 JPanel 的 TextArea 中打印出来。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI_Amortization_Calculator extends JFrame {
private JPanel contentPane;
private JTextField textLoanAmount;
private JTextField textYears;
private JTextField textInterestRate;
TextArea calculation;
/**
* @wbp.nonvisual location=71,9
*/
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI_Amortization_Calculator frame = new GUI_Amortization_Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI_Amortization_Calculator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 600);
getContentPane().setLayout(null);
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 11, 614, 34);
getContentPane().add(panel_2);
JLabel IntroLabel = new JLabel("Introduction to Java Class GUI Amortization Mortgage Calculator by Beth Pizana");
IntroLabel.setForeground(Color.MAGENTA);
IntroLabel.setFont(new Font("Arial Black", Font.BOLD, 12));
panel_2.add(IntroLabel);
JPanel panel = new JPanel();
panel.setBounds(10, 56, 198, 495);
getContentPane().add(panel);
JLabel loanAmountLabel = new JLabel("Enter your loan amount:");
loanAmountLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(loanAmountLabel);
textLoanAmount = new JTextField();
textLoanAmount.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textLoanAmount);
textLoanAmount.setColumns(15);
String txtLoanAmount = textLoanAmount.getText();
JLabel yearsLabel = new JLabel("Enter the years of your loan:");
yearsLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(yearsLabel);
textYears = new JTextField();
textYears.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textYears);
textYears.setColumns(15);
String txtYears = textYears.getText();
JLabel interestRateLavel = new JLabel("Enter the interest rate of your loan:");
interestRateLavel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(interestRateLavel);
textInterestRate = new JTextField();
textInterestRate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textInterestRate);
textInterestRate.setColumns(15);
String txtInterestRate = textInterestRate.getText();
JButton calculate = new JButton("Calculate");
calculate.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Double loanAmount = Double.parseDouble(txtLoanAmount);
int years = Integer.parseInt(txtYears);
Double interestRate = Double.parseDouble(txtInterestRate);
String calc = calculation.getText(calcAmortization(loanAmount, years, interestRate));
textarea.append(calc);
}
});
calculate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(calculate);
JButton reset = new JButton("Reset");
reset.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
textLoanAmount.setText("");
textYears.setText("");
textInterestRate.setText("");
}
});
reset.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(reset);
TextArea calculation = new TextArea();
calculation.setColumns(6);
calculation.setBounds(228, 51, 380, 500);
getContentPane().add(calculation);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(591, 56, 17, 477);
getContentPane().add(scrollBar);
JScrollBar scrollBar_1 = new JScrollBar();
scrollBar_1.setOrientation(JScrollBar.HORIZONTAL);
scrollBar_1.setBounds(231, 534, 363, 17);
getContentPane().add(scrollBar_1);
}
public static void calcAmortization(double loanAmount, int numYears, double interestRate){
double newBalance;
//Calculate the monthly interest rate
double monthlyInterestRate = (interestRate / 12)/100;
//Calculate the number of months
int totalMonths = numYears * 12;
double monthlyPayment, interestPayment, principalPayment;
int count;
//Calculate the monthly payment
monthlyPayment = loanAmount * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, (double)totalMonths)/(Math.pow(1 + monthlyInterestRate, (double)totalMonths)-1);
printTableHeader();
for (count = 1; count < totalMonths; count++){
interestPayment = loanAmount * monthlyInterestRate;
principalPayment = monthlyPayment - interestPayment;
newBalance = loanAmount - principalPayment;
printSchedule(count, loanAmount, monthlyPayment, interestPayment, principalPayment, newBalance);
loanAmount = newBalance;
}
}
public static void printSchedule(int count, double loanAmount, double monthlyPayment, double interestPayment, double principalPayment, double newBalance){
System.out.format("%-8d$%,-12.2f$%,-10.2f$%,-10.2f$%,-10.2f$%,-12.2f\n",count,loanAmount,monthlyPayment,interestPayment,principalPayment,newBalance);
}
public static void printTableHeader(){
int count;
System.out.println("\nAmortization Schedule for Borrower");
for(count=0;count<62;count++) System.out.print("-");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s"," ","Old","Monthly","Interest","Principal","New","Balance");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s\n\n","Month","Balance","Payment","Paid","Paid","Balance");
}
}
我已经发现问题出在下面几行,并且我尝试了多种不同的方法来使其工作。我有点不知所措。我已经阅读了很多有关发送到 textArea 的内容,但很难找到带有方法的内容。请帮助或至少让我朝着正确的方向前进。非常感谢。这是问题区域:
String calc = calculation.getText(calcAmortization(loanAmount, years, interestRate));
textarea.append(calc);
更新我通过扩展你们给我的想法让它工作,我还添加了一个滚动 Pane 。滚动条没有显示,所以如果有人可以给我一些关于为什么滚动 Pane 不能正常工作的建议。
除滚动 Pane 外的新工作代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
@SuppressWarnings("serial")
public class GUI_Amortization_Calculator extends JFrame {
private JPanel contentPane;
private JTextField textLoanAmount;
private JTextField textYears;
private JTextField textInterestRate;
//private JTextArea calculation;
private PrintStream standardOut;
/**
* @wbp.nonvisual location=71,9
*/
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI_Amortization_Calculator frame = new GUI_Amortization_Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI_Amortization_Calculator() {
class SpecialOutput extends OutputStream {
private JTextArea calculation;
public SpecialOutput(JTextArea calculation) {
this.calculation = calculation;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
calculation.append(String.valueOf((char)b));
// scrolls the text area to the end of data
calculation.setCaretPosition(calculation.getDocument().getLength());
};
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 950, 650);
getContentPane().setLayout(null);
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 11, 614, 34);
getContentPane().add(panel_2);
JLabel IntroLabel = new JLabel("Introduction to Java Class GUI Amortization Mortgage Calculator by Beth Pizana");
IntroLabel.setForeground(Color.MAGENTA);
IntroLabel.setFont(new Font("Arial Black", Font.BOLD, 12));
panel_2.add(IntroLabel);
JPanel panel = new JPanel();
panel.setBounds(10, 56, 198, 545);
getContentPane().add(panel);
JLabel loanAmountLabel = new JLabel("Enter your loan amount:");
loanAmountLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(loanAmountLabel);
JTextField textLoanAmount = new JTextField();
textLoanAmount.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textLoanAmount);
textLoanAmount.setColumns(15);
//String txtLoanAmount = textLoanAmount.getText();
JLabel yearsLabel = new JLabel("Enter the years of your loan:");
yearsLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(yearsLabel);
JTextField textYears = new JTextField();
textYears.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textYears);
textYears.setColumns(15);
//String txtYears = textYears.getText();
JLabel interestRateLavel = new JLabel("Enter the interest rate of your loan:");
interestRateLavel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(interestRateLavel);
JTextField textInterestRate = new JTextField();
textInterestRate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textInterestRate);
textInterestRate.setColumns(15);
//String txtInterestRate = textInterestRate.getText();
JButton calculate = new JButton("Calculate");
calculate.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
double loanAmount = Double.parseDouble(textLoanAmount.getText());
int years = Integer.parseInt(textYears.getText());
double interestRate = Double.parseDouble(textInterestRate.getText());
calcAmortization(loanAmount, years, interestRate);
//String calc = calculation.getText();
//calculation.append(calc);
}
});
calculate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(calculate);
JButton reset = new JButton("Reset");
reset.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
textLoanAmount.setText(null);
textYears.setText(null);
textInterestRate.setText(null);
}
});
reset.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(reset);
JTextArea calculation = new JTextArea();
calculation.setBounds(228, 51, 680, 550);
PrintStream printStream = new PrintStream(new SpecialOutput(calculation));
getContentPane().add(calculation);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(228, 51, 680, 550);
getContentPane().add(scrollPane);
standardOut = System.out;
System.setOut(printStream);
System.setErr(printStream);
}
public static void calcAmortization(double loanAmount, int numYears, double interestRate){
double newBalance;
//Calculate the monthly interest rate
double monthlyInterestRate = (interestRate / 12)/100;
//Calculate the number of months
int totalMonths = numYears * 12;
double monthlyPayment, interestPayment, principalPayment;
int count;
//Calculate the monthly payment
monthlyPayment = loanAmount * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, (double)totalMonths)/(Math.pow(1 + monthlyInterestRate, (double)totalMonths)-1);
printTableHeader();
for (count = 1; count < totalMonths; count++){
interestPayment = loanAmount * monthlyInterestRate;
principalPayment = monthlyPayment - interestPayment;
newBalance = loanAmount - principalPayment;
printSchedule(count, loanAmount, monthlyPayment, interestPayment, principalPayment, newBalance);
loanAmount = newBalance;
}
}
public static void printSchedule(int count, double loanAmount, double monthlyPayment, double interestPayment, double principalPayment, double newBalance){
System.out.format("%-8d$%,-12.2f$%,-10.2f$%,-10.2f$%,-10.2f$%,-12.2f\n",count,loanAmount,monthlyPayment,interestPayment,principalPayment,newBalance);
}
public static void printTableHeader(){
int count;
System.out.println("\nAmortization Schedule for Borrower");
for(count=0;count<62;count++) System.out.print("-");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s"," ","Old","Monthly","Interest","Principal","New","Balance");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s\n\n","Month","Balance","Payment","Paid","Paid","Balance");
}
}
最佳答案
您的代码中有几处错误:
calculation
,因为您在代码中创建了一个新的局部变量TextArea calculation = new TextArea();
String txtLoanAmount = textLoanAmount.getText();
等局部变量没用! txtLoanAmount
不是对 textLoanAmount
内容的永久引用。它只是此刻文本字段内容的快照(即:“”,因为您刚刚创建了文本字段)。calcAmortization()
方法是正确的,但它打印到标准输出,而不是计算文本区域,也不是您可以附加到文本区域的字符串。所以我只是将标准输出重定向到您的文本区域,以尽可能少地重写代码。null
布局和 setBound()
,不如使用真正的布局,例如 GridBagLayout
(那,我没有修复)。.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class GUI_Amortization_Calculator extends JFrame {
private JPanel contentPane;
private JTextField textLoanAmount;
private JTextField textYears;
private JTextField textInterestRate;
private JTextArea calculation;
/**
* @wbp.nonvisual location=71,9
*/
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI_Amortization_Calculator frame = new GUI_Amortization_Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI_Amortization_Calculator() {
System.setOut(new PrintStream(new OutputStream(){
@Override
public void write(int b) throws IOException {
// redirects data to the text area
calculation.append(String.valueOf((char)b));
// scrolls the text area to the end of data
calculation.setCaretPosition(calculation.getDocument().getLength());
}
}));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 600);
getContentPane().setLayout(null);
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 11, 614, 34);
getContentPane().add(panel_2);
JLabel IntroLabel = new JLabel("Introduction to Java Class GUI Amortization Mortgage Calculator by Beth Pizana");
IntroLabel.setForeground(Color.MAGENTA);
IntroLabel.setFont(new Font("Arial Black", Font.BOLD, 12));
panel_2.add(IntroLabel);
JPanel panel = new JPanel();
panel.setBounds(10, 56, 198, 495);
getContentPane().add(panel);
JLabel loanAmountLabel = new JLabel("Enter your loan amount:");
loanAmountLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(loanAmountLabel);
textLoanAmount = new JTextField();
textLoanAmount.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textLoanAmount);
textLoanAmount.setColumns(15);
//String txtLoanAmount = textLoanAmount.getText();
JLabel yearsLabel = new JLabel("Enter the years of your loan:");
yearsLabel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(yearsLabel);
textYears = new JTextField();
textYears.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textYears);
textYears.setColumns(15);
//String txtYears = textYears.getText();
JLabel interestRateLavel = new JLabel("Enter the interest rate of your loan:");
interestRateLavel.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(interestRateLavel);
textInterestRate = new JTextField();
textInterestRate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(textInterestRate);
textInterestRate.setColumns(15);
//String txtInterestRate = textInterestRate.getText();
JButton calculate = new JButton("Calculate");
calculate.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Double loanAmount = Double.parseDouble(textLoanAmount.getText());
int years = Integer.parseInt(textYears.getText());
Double interestRate = Double.parseDouble(textInterestRate.getText());
//String calc = calculation.getText();
calcAmortization(loanAmount, years, interestRate);
//textarea.append(calc);
}
});
calculate.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(calculate);
JButton reset = new JButton("Reset");
reset.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
textLoanAmount.setText("");
textYears.setText("");
textInterestRate.setText("");
}
});
reset.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(reset);
calculation = new JTextArea();
calculation.setColumns(6);
JScrollPane p = new JScrollPane(calculation);
p.setBounds(228, 51, 380, 500);
getContentPane().add(p);
}
public static void calcAmortization(double loanAmount, int numYears, double interestRate){
double newBalance;
//Calculate the monthly interest rate
double monthlyInterestRate = (interestRate / 12)/100;
//Calculate the number of months
int totalMonths = numYears * 12;
double monthlyPayment, interestPayment, principalPayment;
int count;
//Calculate the monthly payment
monthlyPayment = loanAmount * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, (double)totalMonths)/(Math.pow(1 + monthlyInterestRate, (double)totalMonths)-1);
printTableHeader();
for (count = 1; count < totalMonths; count++){
interestPayment = loanAmount * monthlyInterestRate;
principalPayment = monthlyPayment - interestPayment;
newBalance = loanAmount - principalPayment;
printSchedule(count, loanAmount, monthlyPayment, interestPayment, principalPayment, newBalance);
loanAmount = newBalance;
}
}
public static void printSchedule(int count, double loanAmount, double monthlyPayment, double interestPayment, double principalPayment, double newBalance){
System.out.format("%-8d$%,-12.2f$%,-10.2f$%,-10.2f$%,-10.2f$%,-12.2f\n",count,loanAmount,monthlyPayment,interestPayment,principalPayment,newBalance);
}
public static void printTableHeader(){
int count;
System.out.println("\nAmortization Schedule for Borrower");
for(count=0;count<62;count++) System.out.print("-");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s"," ","Old","Monthly","Interest","Principal","New","Balance");
System.out.format("\n%-10s%-11s%-12s%-11s%-11s%-12s\n\n","Month","Balance","Payment","Paid","Paid","Balance");
}
}
注意这一点,它使用 System.out.print()
重定向您打印的所有内容:
System.setOut(new PrintStream(new OutputStream(){
@Override
public void write(int b) throws IOException {
// redirects data to the text area
calculation.append(String.valueOf((char)b));
// scrolls the text area to the end of data
calculation.setCaretPosition(calculation.getDocument().getLength());
}
}));
关于java - 获取打印到 JPanel 中的 TextArea 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393863/
我知道如何在简单的html文本框中删除文本,但是html textareas似乎要复杂得多。而不是value属性,而是将文本放置在之间: . 这就是为什么我在制作onFocus和onBlur
这个问题在这里已经有了答案: text from one textarea should get copy to another textarea and original textarea sh
我实现了一个 在 Elm 中,选项卡缩进和取消缩进,而不是将焦点转移到另一个 HTML 元素。效果很好,除了取消缩进有时会导致选择消失!如果我选择第 5 个字符到第 12 个字符,我按 shift-t
是否可以更改 TextArea 的字体?我需要一个固定宽度的字体来显示一些原始数据,这些数据被格式化为在控制台的列中显示。 最佳答案 您可以使用 CSS 来实现,只需为您的应用程序创建一个主题并添加以
换行符或 pharagraph 在 textarea 输出中不起作用?例如,我在 textarea 中为 pharagraph 使用 enter 但在输出中不起作用?我怎样才能做到这一点? $("#s
这个问题在这里已经有了答案: How to calculate the pixel width of a String in JavaFX? (4 个回答) 6年前关闭。 如何计算 TextArea
我正在尝试使用两个不同的 textarea s 显示相同的代码,但有一个母版和另一个副本。在副本textarea ,我想强调更改,类似于 GitHub 在我们进行一些更改时提供的内容。 我想知道这是否
我需要允许用户突出显示文本(用鼠标选择一个范围),然后我想让他们能够通过下拉右键菜单将某些设置应用于该文本。 我知道后半部分。但是如何获取从 JavafX 的文本区域中选择的文本字符串? 另外,我可以
我使用 TinyMCE。 如果我在我的 textarea(编辑器)中插入另一个 texarea,tinymce 认为这个的结束标记是相关的,所以他关闭了编辑器。所有代码都可以在编辑器之外找到... 你
假设我有两个文本区域... 文本区域 1 文本区域 2 Hi There. 我希望能够将我在文本区域一中输入的文本添加到文本区域二中的文本之后。例如:如果我写“我的名字是乔”。在文本区域一中,它将同
我通过点击一个按钮来实现这个 Bootstrap Modal。但是在点击按钮时,模态出现在屏幕上,除了文本区域字段之外的每个字段都正确显示,按钮代码和文件上传代码在它下面,正在进入文本区域,为什么会这
现在我在获取值时遇到了问题 ~ 我正在执行一个功能,当我单击一个按钮时,它会显示一个模式,并且在其中有一个文本区域。将所有内容写入其中后,单击“确定”按钮,它将使用您编写的文本创建一个新的文本区域。你
我有一个呈现文本区域字段的 HTML 模板和 CSS,但是当单击该字段时,光标从文本区域的中间开始,而不是我期望的从左上角开始。 这不会出现在 IE 中,但会出现在 Chrome 和 FF 中。我还得
我有一个在页面上运行的 TinyMCE 实例,它使我能够编辑现有帖子。我查询数据库,并填充变量 $content与存储的文本。然后我有以下 HTML: Content: " > TinyMCE 显示为
每当有人在 textarea 中按 Enter 键时,我都会调用一个函数.现在我想禁用 new line或 break按下回车键时。 所以new line按下 shift+enter 时应该可以工作。
我有一个带有一些 TextArea 元素的 GUI 来显示信息。由于 GUI 将对 keyevents 使用react,因此我在场景元素中添加了一个 EventHandler。因为我不想为每个文本区域
我想用 jquery 在文本区域中插入图像。 (我知道 img 标签无法插入到文本区域中)。我正在使用tinymce jquery: tinyMCE.init({ mod
为什么这行不通?这里的input类型是text: var name = $("input[name='Event[name]']").serializeArray(); name = name[0].
我正在研究一种解决方案,当您在 TextArea 中键入文本时,它应该通知用户还剩下多少字符需要输入。比如你只能输入200之类的。这很容易制作,但我希望计数器显示在 TextArea 的背景中而不是单
“文本区域类型属性”有什么用? W3foos says this: The type property returns which type of form element a text area i
我是一名优秀的程序员,十分优秀!