2012-07-31 58 views
2

這裏是我的代碼,我想居中的JTextPane在JScrollPane的:如何將組件集中到JScrollPane中?

JFrame frame = new JFrame("Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    final JTextPane editor = new JTextPane(); 
    editor.setEditorKit(this); 
    JScrollPane scroll = new JScrollPane(editor); 
    //scroll.setAlignmentX(JScrollPane.CENTER_ALIGNMENT); <<<didnt change anything, so //'ed it 

    frame.getContentPane().add(scroll); 

    frame.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()); 
+2

你是什麼意思'在JScrollPane中居中一個組件?在您的示例中,'JScrollPane'被拉伸以填充'JFrame'的contentPane,'JTextPane'被拉伸以填充'JScrollPane'。我不明白這方面的「中心」是什麼意思。 – Xeon 2012-07-31 13:10:12

回答

4

的問題可能是你的佈局框架的第一個所有你所設置的幀大小,並設置對齊

2

通過@ Mr.Cool +1

  • 的JFrame添加到答案已經在默認情況下實現的BorderLayout,然後代碼行frame.getContentPane().add(scroll);填補所有可用空間爲JFrame的

  • 你有兩個選擇:

    一)添加另一個JComponents(可能是空JPanels),以區域NORTHSOUTHWESTEAST休息,然後JScrollPane可以調整大小與JFrame

    B)使用GridBagLayoutJFrame(沒有任何GridBagConstant),然後JScrollPane不能調整大小與JFrame