2013-08-29 63 views
4

在JTextArea中最多打印363行時,TextArea將在第43行(邊界)處截斷。我已經把JTextArea放入一個JScrollPane中,並將ScrollPolicy設置爲ScrollPaneConstants.VERTICAL_SCROLLBAR_​​ALWAYS ....如何在寫入超出幀邊界的內容時使JTextArea可滾動?在JFrame邊界處截斷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); 
} 
+1

將'amortScroll'直接添加到'amortFrame' – MadProgrammer

+2

下面是一個建議:不要使用'setText',而是使用'append'。 – tbodt

回答

3

你總是應該調用frame.setVisible(真)之前調用frame.pack()frame.setSize(...)。通常最好使用frame.pack()讓Swing組件按照其首選大小繪製。

所以接下來你實際上需要給文本區域一個關於它的首選大小應該是什麼的建議。這是通過做做:

JTextArea amortText = new JTextArea(25, 70); 

最後用一個更好的Swing組件將是一個JTable,因爲它支持表格數據。 JTextArea不應該用於格式化數據。

+0

謝謝!顯然相對較新的Swing ... JTable完美的作品! – dpfrakes