2013-09-16 78 views
0

我想添加一個滾動條到我的JTextarea,但滾動條不顯示我的Jtoolbar 任何人都可以告訴我這段代碼有什麼問題。所以我可以修復它。我一直在尋找eeverywhere但滾動窗格還是老樣子不露面JScrollPane不顯示到JTextarea

 public PlayerGui() { 
    // create main windows 
    super("Liste"); 

    JTextArea editors = new JTextArea(); 
    editors.setLineWrap(true); 
    editors.setWrapStyleWord(true); 
    // scroll bar 
    JScrollPane scroll = new JScrollPane(editors); 
    setEditor(editors); 


    // create center panel 
    JPanel cent = new JPanel(); 

    //create Panel for the to 
    JPanel north = new JPanel(); 
    setNorthpanel(north); 
    // create tool bar 
    JToolBar toolbar = new JToolBar(); 
    toolbar.add(scroll); 

    // set center panel and add preffered layout and backgrounds and size 
    setCenter(cent); 
    getCenter().setLayout(new BorderLayout()); 
    // add scroll bar and toolbar 
    add(scroll, BorderLayout.EAST); 
    add(toolbar, BorderLayout.SOUTH); 
    //getCenter().setBackground(Color.black); 
    Dimension size = new Dimension(getCenter().getPreferredSize()); 
    getEditor().setPreferredSize(size); 
    getCenter().getPreferredSize(); 
    getCenter().setBorder(new CompoundBorder(new EmptyBorder(10,10 ,10,10),new  EtchedBorder(Color.BLACK, Color.black))); 
    //add text editor to the center panel 
    getCenter().add(getEditor(),BorderLayout.CENTER); 
    //set layout of the frame 
    setLayout(new BorderLayout()); 
    menubar1 = new JMenuBar(); 
    //create menu list from a string arrays 
    for(int i=0; i<list.length; i++){ 
     JMenu menus = new JMenu(list[i]); 
     menubar1.add(menus); 
    } 
+1

發佈證明你的問題的[SSCCE](http://sscce.org/)。我們不知道setEditor(...)方法的作用。由於它使用文本區域,可能這是問題的原因。 – camickr

+1

'getCenter()。getPreferredSize();'嗯..你期望達到什麼目的? –

回答

0

您正在使用的佈局管理器不正確。請找到您正在工作的修改後的代碼。

public class TestFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TestFrame frame = new TestFrame(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 


    public TestFrame() { 
     // create main windows 
     super("Liste"); 
     // scroll bar 
     JScrollPane scroll = new JScrollPane(); 
     //setEditor(editors); 


     // create center panel 
     JPanel cent = new JPanel(); 

     //create Panel for the to 
     JPanel north = new JPanel(); 
     getContentPane().setLayout(new BorderLayout(0, 0)); 
     //toolbar.add(scroll); 

     // set center panel and add preffered layout and backgrounds and size 
     //setCenter(cent); 
     //getCenter().setLayout(new BorderLayout()); 
     // add scroll bar and toolbar 
     getContentPane().add(scroll); 

     JTextArea textArea = new JTextArea(); 
     scroll.setViewportView(textArea); 

     JToolBar toolBar = new JToolBar(); 
     getContentPane().add(toolBar, BorderLayout.NORTH); 
     JMenuBar menubar1 = new JMenuBar(); 
     //create menu list from a string arrays 
//  for(int i=0; i<list.length; i++){ 
//   JMenu menus = new JMenu(list[i]); 
//   menubar1.add(menus); 
//  } 
    } 
} 

希望這會對您有所幫助。 :-)