2017-03-02 66 views
0

我試圖把滾動條放入JPanel中。如何在面板中添加ScrollPane

我把我的代碼放在public static void main中。

public static void main(String[] args) { 
    JFrame frame = new JFrame("Outil Replacement"); 
    frame.setSize(930, 610); 
    frame.setPreferredSize(new Dimension(930,400)); 

    JScrollPane scrollPane = new JScrollPane(); 

    scrollPane.setWheelScrollingEnabled(true); 

    frame.add(scrollPane); 
    frame.setContentPane(new Replacement().panelMain); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setVisible(true); 
} 

它什麼也沒做。

只是一個窗體視圖。

有人有解決方案嗎?

回答

2

你而設定的目標JPanelJScrollPane口的視圖然後添加JScrollPane,就像這樣:

JScrollPane scrollPane = new JScrollPane(new Replacement().panelMain); 

scrollPane.setWheelScrollingEnabled(true); 

frame.setContentPane(scrollPane); 

參見:JScrollPane(java.awt.Component)

和:How to Use Scroll Panes

+0

就是這樣!謝謝。只是爲了一個小錯誤..一個糟糕的「.setcontent();」 –