2012-04-16 7 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

+0

データが消滅するだけです。 – Ravi

+3

すぐに役立つように、[SSCCE](http://sscce.org/)を投稿してください。 –

答えて

47

ここにJScrollPaneにJTextAreaにを追加した後:

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

+0

使用するコードを追加しました。他にもそれを追加しようとしているのでしょうか?そうすると、JTextAreaがスクロールペインの外に出て新しい場所に配置されます。 –

+0

私は他のどこにも使っていません。 – Ravi

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()'のようなレイアウトを設定すると、自動的にスクロールペインの境界を計算して設定します。 –

+0

Java 8では動作します。 – ATorras

2

これら2行をコードに追加してみてください。私はそれがうまくいくことを望む。それは私のために働いた:)出力の

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

画像はテキストが、その後の領域の上限に達したときに何が起こる

enter image description here

関連する問題