2012-04-16 29 views
20

如何添加滾動條到我的文本區域。我已經嘗試過這個代碼,但它不工作。Java:將滾動添加到文本區域

  middlePanel=new JPanel(); 
     middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area")); 

     // create the middle panel components 

     display = new JTextArea(16, 58); 
     display.setEditable(false); // set textArea non-editable 
     scroll = new JScrollPane(display); 
     scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

     //Add Textarea in to middle panel 
     middlePanel.add(scroll); 
     middlePanel.add(display); 

感謝

+0

當文本到達區域限制,然後會發生什麼? – talnicolas 2012-04-16 15:37:20

+0

數據即將消失。 – Ravi 2012-04-16 15:40:33

+3

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-04-16 17:19:37

回答

47

添加到JTextArea中JScrollPane的位置後:

scroll = new JScrollPane(display); 

你不需要重新添加到其他容器中像你這樣的:

middlePanel.add(display); 

剛刪除最後一行代碼,它會正常工作。就像這樣:

middlePanel=new JPanel(); 
    middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area")); 

    // create the middle panel components 

    display = new JTextArea(16, 58); 
    display.setEditable(false); // set textArea non-editable 
    scroll = new JScrollPane(display); 
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

    //Add Textarea in to middle panel 
    middlePanel.add(scroll); 

JScrollPane的只是另一個容器其在需要時周圍放置你的組件滾動條和也有自己的佈局。所有你需要做的,當你想換什麼成卷軸只是將它傳遞到JScrollPane的構造函數:

new JScrollPane(myComponent) 

或視圖設置是這樣的:

JScrollPane pane = new JScrollPane(); 
pane.getViewport().setView (myComponent); 

附加:

這裏是完全可行的例子,因爲你仍然沒有得到它的工作:

public static void main (String[] args) 
{ 
    JPanel middlePanel = new JPanel(); 
    middlePanel.setBorder (new TitledBorder (new EtchedBorder(), "Display Area")); 

    // create the middle panel components 

    JTextArea display = new JTextArea (16, 58); 
    display.setEditable (false); // set textArea non-editable 
    JScrollPane scroll = new JScrollPane (display); 
    scroll.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

    //Add Textarea in to middle panel 
    middlePanel.add (scroll); 

    // My code 
    JFrame frame = new JFrame(); 
    frame.add (middlePanel); 
    frame.pack(); 
    frame.setLocationRelativeTo (null); 
    frame.setVisible (true); 
} 

這裏是你會得到什麼: enter image description here

+0

還沒有工作! – Ravi 2012-04-16 15:48:14

+0

添加了您應該使用的代碼。也許你正試圖把它添加到其他地方?如果你這樣做,將JTextArea從滾動窗格中取出並放在新的位置。 – 2012-04-16 15:50:56

+0

我沒有在其他地方使用。 – Ravi 2012-04-16 15:52:29

4

我天真的假設是,滾動窗格的大小將自動確定...

實際工作對我來說唯一的解決辦法是明確seeting界限JScrollPane的

import javax.swing.*; 

public class MyFrame extends JFrame { 

    public MyFrame() 
    { 
     setBounds(100, 100, 491, 310); 
     getContentPane().setLayout(null); 

     JTextArea textField = new JTextArea(); 
     textField.setEditable(false); 

     String str = ""; 
     for (int i = 0; i < 50; ++i) 
      str += "Some text\n"; 
     textField.setText(str); 

     JScrollPane scroll = new JScrollPane(textField); 
     scroll.setBounds(10, 11, 455, 249);      // <-- THIS 

     getContentPane().add(scroll); 
     setLocationRelativeTo (null); 
    } 
} 

也許這將幫助一些未來的訪客:)

+0

只需要小小的提示 - 滾動窗格的大小和位置就會自動確定,但前提是您要爲要添加的容器提供任何佈局。現在在你的例子中,你爲內容窗格設置了'null'佈局:'getContentPane()。setLayout(null);' - 這就是你需要手動指定邊界的原因。否則,如果您設置了任何佈局 - 例如'new BorderLayout()',它會自動爲您的滾動窗格計算和設置邊界。 – 2016-03-01 14:52:29

+0

從Java 8開始起作用。 – ATorras 2017-07-15 23:32:56

2

嘗試將這兩行添加到您的代碼中。我希望它會起作用。它的工作對我來說:)

display.setLineWrap(true); 
display.setWrapStyleWord(true); 

輸出的圖像顯示在下面

enter image description here