2013-05-05 48 views
1

我在互聯網上的stackoverflow &上搜索,但它沒有工作。滾動條不出現。請幫助我,如果它有效,我會很樂意爲您的答案投票。下面是代碼:(在此先感謝!)JScrollPane沒有出現在JTextPane上

package com.james.client; 

import javax.swing.*; 
import javax.swing.UIManager.LookAndFeelInfo; 

public class Main extends JFrame{ 

private static final long serialVersionUID = 1L; 

public static void main(String [] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
{ 
    //Set program to nimbus 
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
     if ("Nimbus".equals(info.getName())) { 
      UIManager.setLookAndFeel(info.getClassName()); 
      break; 
    } 
    } 
    //Window stuff 
    JFrame window = new JFrame("MinecraftProgrammer++"); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setSize(1000, 600); 
    window.setResizable(false); 

    JPanel content = new JPanel(); 
    content.setLayout(null); 

    JMenuBar nav = new JMenuBar(); 
    JMenu file = new JMenu("File"); 
    JMenu newfile = new JMenu("New"); 
    JMenuItem Class = new JMenuItem("Class"); 
    JMenuItem Package = new JMenuItem("Package"); 
    JMenuItem Other = new JMenuItem("Other"); 

    newfile.add(Class); 
    newfile.add(Package); 
    newfile.add(Other); 
    file.add(newfile); 
    nav.add(file); 

    JTextPane code = new JTextPane(); 
    JScrollPane codescroll = new JScrollPane(code); 
    codescroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    codescroll.setBounds(0, 0, 994, 547); 
    code.setAutoscrolls(true); 
    code.setBounds(0, 0, 994, 547); 

    content.add(codescroll); 
    content.add(code); 
    window.setJMenuBar(nav); 
    //No more code after this line 
    window.add(content); 
    window.setVisible(true); 
} 
} 

回答

3

刪除此行:

content.add(code); 

你已經添加的JTextPane到滾動。您不需要再將JTextPane添加到您的JPanel。

+0

完美的作品!我做了多麼糟糕的錯誤。謝謝。 – 2013-05-05 00:05:38

+0

@JamesL。沒問題。在開始使用Swing時自己犯了很多錯誤。 – 2013-05-05 00:06:21