gpt4 book ai didi

java - JTextArea 在 JFrame 边界截断

转载 作者:搜寻专家 更新时间:2023-11-01 03:07:09 25 4
gpt4 key购买 nike

当在 JTextArea 中打印多达 363 行时,TextArea 将在大约第 43 行(框架边界)处截断。我已经将 JTextArea 放入 JScrollPane,并将 ScrollPolicy 设置为 ScrollPaneConstants.VERTICAL_SCROLLBAR_​​ALWAYS...。如何在写入超出框架边界的内容时使 JTextArea 可滚动?

public static double principalAmt, intRate, balance, monthlyIntRate, monthlyPayment, monthlyIntPayment, monthlyPrincipalPayment;
public static int termLength, months;

static Box infoBox = Box.createVerticalBox();
static Box graphBox = Box.createVerticalBox();

public static void main(String[] args) {
new AmortizationTable();
}

public AmortizationTable() {

// User input
principalAmt = 150000;
termLength = 15;
intRate = 0.05;

// Calculated values
balance = principalAmt;
monthlyIntRate = intRate / 1200;
months = termLength * 12;
monthlyPayment = principalAmt * (monthlyIntRate *
Math.pow(1 + monthlyIntRate, months) / (Math.pow(1 + monthlyIntRate, months) - 1));

JFrame amortFrame = new JFrame();
JPanel amortPanel = new JPanel();
Box amortBox = Box.createVerticalBox();
JTextArea amortText = new JTextArea();
amortText.setEditable(false);

String[] amortArray = new String[months];

JLabel amortTitle1 = new JLabel("---------------------------------------------------"
+ "-------------------------------------------");
JLabel amortTitle2 = new JLabel("Payment\tMonthly Payment\tInterest Paid\t\tPrincipal Paid\t\tBalance");
JLabel amortTitle3 = new JLabel("-------------------------------------------------"
+ "---------------------------------------------");

amortText.setText(amortText.getText() + amortTitle1.getText() + "\n");
amortText.setText(amortText.getText() + amortTitle2.getText() + "\n");
amortText.setText(amortText.getText() + amortTitle3.getText() + "\n");

for(int i=0; i<months; i++) {

monthlyIntPayment = balance * monthlyIntRate;
monthlyPrincipalPayment = monthlyPayment - monthlyIntPayment;
balance -= monthlyPrincipalPayment;
if(balance<0)
balance = -balance;

amortArray[i] = ("" + (i+1) + "\t" +
new DecimalFormat("$0.00").format(monthlyPayment) + "\t\t" +
new DecimalFormat("$0.00").format(monthlyIntPayment) + "\t\t" +
new DecimalFormat("$0.00").format(monthlyPrincipalPayment) + "\t\t" +
new DecimalFormat("$0.00").format(balance) + "\t\n");

amortText.setText(amortText.getText() + amortArray[i]);

}

JScrollPane amortScroll = new JScrollPane(amortText);
amortScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
amortBox.add(amortScroll);
amortPanel.add(amortBox);
amortFrame.add(amortPanel);
amortFrame.setVisible(true);
}

最佳答案

在调用 frame.setVisible(true) 之前,您应该始终调用 frame.pack()frame.setSize(...)。通常,最好使用 frame.pack() 让 Swing 组件以其首选尺寸绘制。

所以接下来您实际上需要给文本区域一个关于它的首选大小应该是什么的建议。这是通过做:

JTextArea amortText = new JTextArea(25, 70);

最后一个更好用的 Swing 组件是 JTable,因为它支持表格数据。 JTextArea 不应用于格式化数据。

关于java - JTextArea 在 JFrame 边界截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18501809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com