Java JTextArea在JFrame边界处截断
在JTextArea中打印多达363行时,TextArea将在第43行(帧边界)处截断。我已经将JTextArea放入了JScrollPane,并将ScrollPolicy设置为ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS。。。。在写入超出帧边界的内容时,如何使JTextArea可滚动Java JTextArea在JFrame边界处截断,java,swing,jscrollpane,jtextarea,Java,Swing,Jscrollpane,Jtextarea,在JTextArea中打印多达363行时,TextArea将在第43行(帧边界)处截断。我已经将JTextArea放入了JScrollPane,并将ScrollPolicy设置为ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS。。。。在写入超出帧边界的内容时,如何使JTextArea可滚动 public static double principalAmt, intRate, balance, monthlyIntRate, monthlyPay
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);
}
public static double principalat,intRate,balance,monthlynitate,monthlyPayment,monthlyntpayment,monthlyprincipalment;
公共静态期限长度,月;
静态框infoBox=Box.createVerticalBox();
静态框graphBox=Box.createVerticalBox();
公共静态void main(字符串[]args){
新摊销表();
}
公共摊销表(){
//用户输入
principalAmt=150000;
终端长度=15;
intRate=0.05;
//计算值
平衡=主平衡;
月硝酸盐=intRate/1200;
月份=期限长度*12;
月付款=本金*(月付款)*
Math.pow(1+月,月)/(Math.pow(1+月,月)-1));
JFrame amortFrame=新JFrame();
JPanel amortPanel=新的JPanel();
Box amortBox=Box.createVerticalBox();
JTextArea amortText=新的JTextArea();
amortText.setEditable(false);
字符串[]amortArray=新字符串[月份];
JLabel amortitle1=新的JLabel(“--------------------------------------------------------------”
+ "-------------------------------------------");
JLabel amortTitle2=新JLabel(“付款\t每月付款\t最新付款\t\t主要付款\t\t余额”);
JLabel amortitle3=新的JLabel(“--------------------------------------------------------------”
+ "---------------------------------------------");
amortText.setText(amortText.getText()+amortTitle1.getText()+“\n”);
amortText.setText(amortText.getText()+amortTitle2.getText()+“\n”);
amortText.setText(amortText.getText()+amortTitle3.getText()+“\n”);
对于(int i=0;i,在调用frame.setVisible(true)之前,您应该始终调用frame.pack()
或frame.setSize(…)
。通常,最好使用frame.pack(),以便将Swing组件绘制为其首选尺寸
因此,接下来您实际上需要给文本区域一个建议,以确定其首选大小。这可以通过执行以下操作来完成:
JTextArea amortText = new JTextArea(25, 70);
最后一个更好的Swing组件是JTable
,因为它支持表格数据。JTextArea不应该用于格式化数据。将amortScroll
直接添加到amortFrame
这里有一个建议:不要使用setText
,而是使用append
。谢谢!显然是相对较新的哦,Swing…JTable工作得非常完美!