2014-02-22 85 views
0

请您告诉我为什么滚动不起作用。它是可见的,但它不起作用,在这里你可以看到这段代码。缺失的部分是什么?JScrollPane滚动可见但不起作用

// GUI elements 
private JTextField textSend = new JTextField(20); 
private JTextArea textArea = new JTextArea(5, 20); 

private JScrollPane scroll = new JScrollPane(textArea); 

private JButton buttonConnect = new JButton("Connect"); 
private JButton buttonSend = new JButton("Send"); 
private JButton buttonDisconnect = new JButton("Disconnect"); 
private JButton buttonQuit = new JButton("Quit"); 

private JPanel leftPanel = new JPanel(); 
private JPanel rightPanel = new JPanel(); 

private JLabel empty = new JLabel(""); 

ChessHeroChatClient() { 
    setTitle("ChessHero Chat Client"); 
    setLocationRelativeTo(null); 
    setSize(500, 500); 
    setResizable(false); 

    leftPanel.setLayout(new BorderLayout()); 
    rightPanel.setLayout(new GridLayout(6, 1)); 

    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

    leftPanel.add(textSend, BorderLayout.NORTH); 
    leftPanel.add(textArea, BorderLayout.WEST); 
    leftPanel.add(scroll, BorderLayout.EAST); 

    add(leftPanel, BorderLayout.CENTER); 
    add(rightPanel, BorderLayout.EAST); 

    textArea.setEditable(false); 
+0

很难说没有看到实际的课程或你的意思是不工作。截图怎么样?或更多细节。 –

回答

4
private JTextArea textArea = new JTextArea(5, 20); 
private JScrollPane scroll = new JScrollPane(textArea); 

创建滚动面板用,这是很好的textarea的。

//leftPanel.add(textArea, BorderLayout.WEST); // this is wrong 
leftPanel.add(scroll, BorderLayout.EAST); 

但是,您将textArea添加到WEST,并滚动到EAST,这是错误的。 Swing组件只能有一个父对象,所以只需保留textArea并将scrollPane添加到EAST或WEST。

+0

非常感谢,现在它工作。 –