2013-08-17 111 views
2

我想在textarea中創建一個滾動條,但是如果我將JPanel佈局設置爲null,滾動條將不會顯示!JTextArea中的滾動條

我試圖

JScrollPane scrollbar1 = 
    new JScrollPane(
    ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

但由於空佈局沒有工作。

這裏是我當前的代碼:

import javax.swing.*; 

import java.awt.*; 
public class app extends JFrame { 

    public static void main(String[] args) 
    { 
     new app(); 
    } 

    public app() 
    { 
     this.setSize(400,400); 
     this.setLocation(0,0); 
     this.setResizable(false); 
     this.setTitle("Application");   
     JPanel painel = new JPanel(null);   
     // Creating the Input 
     JTextField tf1 = new JTextField("Some random text", 15);    
     tf1.setBounds(5,5,this.getWidth()-120,20); 
     tf1.setColumns(10); 
     tf1.setText("Omg");   
     painel.add(tf1);    
     // Creating the button   
     JButton button1 = new JButton("Send");   
     button1.setBounds(290, 5, 100, 19);   
     painel.add(button1);    
     // Creating the TextArea    
     JTextArea ta1 = new JTextArea(15, 20); 
     JScrollPane scr = new JScrollPane(); 
     ta1.setBounds(5, 35, 385, 330); 
     ta1.setLineWrap(true); 
     ta1.setWrapStyleWord(true);   
     painel.add(ta1); 
     this.add(painel); 
     this.setVisible(true); 
    } 
} 

我要讓它正常工作。如果有人能幫助我,請在下面留言!

+0

正確的方法將不使用'null'佈局。無論如何,您需要將'TextArea'放在滾動窗格中,並在滾動窗格中顯示您想要的組合。 – kiheru

+0

@Kiheru我說我試圖做到這一點,但沒有奏效。 –

+0

它確實有效,但是您需要正確設置滾動窗格的邊界,因爲您嘗試手動創建佈局(是的,我試過了)。 – kiheru

回答

2

我糾正了以下所有的問題是工作代碼。請閱讀有關更改的評論。

import javax.swing.*; 

import java.awt.*; 

public class app extends JFrame { 

    public static void main(String[] args) { 
     new app(); 
    } 

    public app() { 
     this.setSize(400, 400); 
     this.setLocation(0, 0); 
     this.setResizable(false); 
     this.setTitle("Application"); 
     JPanel painel = new JPanel(null); 
     // Creating the Input 
     JTextField tf1 = new JTextField("Some random text", 15); 
     tf1.setBounds(5, 5, this.getWidth() - 120, 20); 
     tf1.setColumns(10); 
     tf1.setText("Omg"); 

     // resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
     painel.add(tf1); 
     // Creating the button 
     JButton button1 = new JButton("Send"); 
     button1.setBounds(290, 5, 100, 19); 
     painel.add(button1); 
     // Creating the TextArea 
     JTextArea ta1 = new JTextArea(15, 20); 
     JScrollPane scr = new JScrollPane(ta1, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
       JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane 
     ta1.setBounds(5, 35, 385, 330); 
     ta1.setLineWrap(true); 
     ta1.setWrapStyleWord(true); 
     scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout 
     painel.add(scr);// Add you scroll pane to container 
     this.add(painel); 
     this.setVisible(true); 
    } 
} 

編輯。請閱讀Java上的oracle教程。並開始使用適當的佈局管理器... 希望它可以幫助

+4

'null'佈局不好的建議;另請參閱[*初始線程*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod

+0

謝謝,那正是我正在尋找的。 –

1

您的JTextArea傳遞給你的JScrollPane構造,然後添加你的JScrollPane對象添加到您Container,而不是僅僅是JTextArea。因此,這將是這個樣子:

JScrollPane scr = new JScrollPane(ta1); 
panel.add(scr); 
+0

請閱讀問題和「試用代碼」。 –

+0

這只是一個基於所提供代碼的觀察。 –

+0

+1 ...........休息是OP的問題 – mKorbel

2

If someone can help me, leave a comment below please!

  • 你爲什麼你要和你的頭牆粉碎請JScrollPane被指定爲動態,可調整大小LayoutManagerAbsoluteLayout可以打破這個它基本屬性

  • 從頂部開始

    1. public class app extends JFrame {

      • public class App { ---> Java命名約定
      • ,而不是任何東西延伸,創造JFrame作爲局部變量
    2. new app(); ---> SE甲骨文教程初始線程

    3. 創建另一個JPanel,放在那裏JTextFieldJButton

    4. 做你的東西覆蓋tf1.setBounds(5,5,this.getWidth()-120,20);

    5. NullLayout不能正常工作,而無需使用到BorderLayoutJPanel painel = new JPanel(null);Insets

    6. 變化BUILT_IN FlowLayout,有把JScrollPaneJTextAreaCENTER area

    7. 你可以放JScrollPaneJTextAreaJFrames CENTER area直接和另一個JPanelJTextFieldJButtonSOUTHNORTHJFrame已經在API

    8. JScrollPane僅示出在殼體JScrollbars實現BorderLayoutDimension小於JComponent放在那裏

    9. 使用JFrame.pack()而不是setSize,此行應該在setVisible之前

+1

+1爲真實,正確的解決方案:-) – kiheru

2

這裏的許多@mKorbels points的一個基本的例子。請注意0​​,FlowLayout()的默認佈局如何使用其組件的首選大小。對f.setSize()的調用是可選的,以強制滾動條出現。

image

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.*; 

public class App { 

    public static void main(String[] args) { 
     new App(); 
    } 

    public App() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame f = new JFrame("Application"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       JPanel panel = new JPanel(); 
       JTextField tf1 = new JTextField("Some random text", 15); 
       tf1.setColumns(10); 
       tf1.setText("Omg"); 
       panel.add(tf1); 
       JButton button1 = new JButton("Send"); 
       panel.add(button1); 
       JTextArea ta = new JTextArea(15, 20); 
       JScrollPane scr = new JScrollPane(ta); 
       scr.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
       ta.setLineWrap(true); 
       ta.setWrapStyleWord(true); 
       f.add(panel, BorderLayout.NORTH); 
       f.add(scr, BorderLayout.CENTER); 
       f.pack(); 
       Dimension d = scr.getPreferredSize(); 
       f.setSize(d.width, d.height); 
       f.setLocationByPlatform(true); 
       f.setVisible(true); 
      } 
     }); 
    } 
}