2017-04-11 67 views
0

我該如何製作它,以便我的JTextArea是可滾動的。我想這樣做,keysDisplay有一個滾動條,可用於滾動文本區域。我在jFramePrint()中跳過了一些未分析的代碼。使JTextArea在Swing中滾動

public class ApplicationViewer extends JFrame { 

    private JTabbedPane tabs = new JTabbedPane(); 
    private JTextArea keyGenDisplay = new JTextArea(); 
    private JTextArea keysDisplay = new JTextArea(); 

    private JPanel controlPanel = new JPanel(); 
    private JButton addNumber = new JButton("Add Number"); 
    private JButton addLetter = new JButton("Add Letter"); 
    private JButton addHyphen = new JButton("Add Hyphen"); 
    private JButton calculateButton = new JButton("Calculate Key"); 

    private JTextField amountField = new JTextField("", 6); 
    private JLabel amountLabel = new JLabel(" Amount of Keys : "); 
    private JScrollPane scroll = new JScrollPane(keysDisplay); 

    public void jFramePrint() { 

     this.setLayout(new BorderLayout()); 
     this.add(controlPanel, BorderLayout.SOUTH); 


     controlPanel.add(addNumber); 
     controlPanel.add(addLetter); 
     controlPanel.add(addHyphen); 
     controlPanel.add(amountLabel); 
     controlPanel.add(amountField); 
     controlPanel.add(calculateButton); 

     this.add(scroll); 

     this.setSize(1400, 900); 
     this.setTitle("Key Generator"); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

     keyGenDisplay.append("Key Format: "); 
     keysDisplay.append("Keys Here: "); 

     tabs.add("Key Generator", keyGenDisplay); 
     tabs.add("Keys", keysDisplay); 

     this.add(tabs); 
     this.setVisible(true); 

    } 
} 
+0

看看這個帖子:http://stackoverflow.com/questions/1052473/scrollbars-in-jtextarea –

+0

當你發佈代碼時使用正確的格式,你有1,2,3個空格如果你希望我們花時間幫忙,那麼你至少可以做的是發佈可讀代碼! – camickr

回答

1
private JTextArea keysDisplay = new JTextArea(); 

你應該使用類似的第一次:

private JTextArea keysDisplay = new JTextArea(5, 20); 

這將使文本區域,計算出自己的首選大小。添加到滾動窗格後,滾動條將正常工作,並且超過5行的文本被添加到文本區域。

this.add(scroll); 

... 

this.add(tabs); 

您的幀正在使用BorderLayout。當你不使用約束,則「中心是默認使用。

不能multile組件添加到BorderLayout的同一區域,因此僅顯示添加的最後一個組件。

指定對標籤組件有不同的限制