[英]JTextArea truncating at JFrame bounds
When printing up to 363 lines in a JTextArea, the TextArea will truncate at about line 43 (frame bounds). 在JTextArea中最多打印363行时,TextArea将在第43行(帧边界)处截断。 I already put the JTextArea into a JScrollPane, and set the ScrollPolicy to ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS.... How do I make the JTextArea scrollable while writing content beyond the frame bounds? 我已经将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);
}
You should always invoke frame.pack()
or frame.setSize(...)
before invoking frame.setVisible(true). 在调用frame.setVisible(true)之前,应始终调用frame.pack()
或frame.setSize(...)
)。 Generally it is better to use frame.pack() to let the Swing components be painted at their preferred sizes. 通常最好使用frame.pack()让Swing组件以其首选大小进行绘制。
So next you actually need to give the text area a suggestion as to what its preferred size should be. 接下来你实际上需要给文本区域一个关于它的首选大小应该是什么的建议。 This is done by doing: 这是通过以下方式完成的:
JTextArea amortText = new JTextArea(25, 70);
Finally a better Swing component to use would be a JTable
since it support tabular data. 最后,要使用的更好的Swing组件是JTable
因为它支持表格数据。 A JTextArea should not be used for formatted data. JTextArea不应用于格式化数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.