2014-02-06 107 views
1

我似乎无法看到滚动条显示。我注意到在我的应用程序的底部有一个小框,当我添加了滚动窗格代码时,它的尺寸太小,无法做任何事情,它肯定不会使我需要滚动的文本区域。任何帮助,将不胜感激。JScrollpane滚动条没有显示

mainFrame = new JFrame();  
    menuBar = new JMenuBar(); 
    menu = new JMenu("File"); 
    messageArea = new JTextArea(20, 35); 
    messageEntry = new JTextArea(5, 35); 
    fl = new FlowLayout(); 
    currentMessage = ""; 

    sp = new JScrollPane(messageArea); 
    sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    sp.setSize(100, 100); 

    messageArea.setEditable(false); 
    messageArea.setLineWrap(true); 


    menuBar.add(menu); 

    sendButton = new JButton("Send"); 
    sendButton.addActionListener(this); 
    mainFrame.setPreferredSize(new Dimension(400,500)); 
    mainFrame.setLayout(fl); 
    mainFrame.getContentPane().setBackground(Color.blue); 
    mainFrame.setJMenuBar(menuBar); 
    mainFrame.add(messageArea); 
    mainFrame.add(messageEntry); 
    mainFrame.add(sp); 
    mainFrame.add(sendButton); 
    mainFrame.setResizable(false); 
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    mainFrame.setTitle("Communicator"); 
    mainFrame.pack(); 
    mainFrame.setVisible(true); 

回答

3

你加入messageArea直接mainFrame的代码,这是采摘出来滚动窗格后,留下滚动窗格空。删除mainFrame.add(messageArea);行。您只需要将滚动窗格本身添加到窗口。

+0

哇,我一直与那一小时战斗。谢谢! – user519670