2010-08-21 45 views
1

我想將Scrollpane添加到由另一個jpanel調用的JPanel中?JPanel的滾動窗格?

public class test { 

    JFrame jframe; 
    JPanel jpanel; 
    JScrollPane js= null; 
    public void createFrame() 
    { 
     jframe=new JFrame("Thick client Wallboard"); 
     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     jframe.setSize(dim.width,dim.height); 
     jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jframe.setUndecorated(true); 
     jframe.setResizable(false);   
     jframe.setLocation(0, 0); 
     getJContentPane(); 
     jframe.setContentPane(jpanel); 
     jframe.setVisible(true); 
    } 
    public void getJContentPane() 
    { 

     jpanel = new JPanel(); 
     jpanel.setBackground(new Color(0x505050)); 
     jpanel.setLayout(null); 
     jpanel.setFont(new Font("verdana",Font.BOLD,12)); 
     jpanel.setBorder(new LineBorder(Color.BLACK,2)); 
     getpanel(); 
     jpanel.add(js); 
     jpanel.setBackground(Color.LIGHT_GRAY); 
     jpanel.setVisible(true); 


    } 
    public void getpanel() 
    { 

     JPanel mainPanel=new JPanel(); 
     mainPanel.setBounds(50,50,920,650); 
     mainPanel.setBackground(Color.WHITE); 
     mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS)); 

     for(int i = 0; i < 30; i++) { 
      mainPanel.add(new JButton("Button " + i)); 
     } 
     js=new JScrollPane(mainPanel); 

    } 


    public static void main(String[] args) { 

     test t=new test(); 
     t.createFrame(); 
    } 

} 

我曾經做過這樣的,但它是不工作...

+0

請再具體些。您可以將任何組件放入JScrollPane中。 – 2010-08-21 13:16:00

回答

3

你可以這樣做:

public static void main(String[] args) { 
    Frame frame = new JFrame(); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 

    for(int i = 0; i < 20; i++) { 
     panel.add(new JButton("Button " + i)); 
    } 

    //Creating JScrollPane with JPanel 
    JScrollPane scrollPane = new JScrollPane(panel); 
    JPanel otherPanel = new JPanel(); 
    otherPanel.setLayout(new BorderLayout()); 
    //Adding scrollPane to panel 
    otherPanel.add(scrollPane); 
    frame.add(otherPanel); 

    frame.setSize(200,200); 
    frame.setVisible(true);  

} 

同時檢查: http://www.coderanch.com/t/342486/GUI/java/Adding-ScrollPane-JPanel

+0

您爲jpanel添加scrollerpane,然後將scrollerpane添加到jframe,但是我需要scrollerpane用於jpanel,然後將scrollerpane添加到另一個jpanel而不是框架,請幫我解決這個問題 – Lalchand 2010-08-21 14:08:48

+0

因此,另一個jpanel添加到jframe中? – YoK 2010-08-21 14:19:17

+0

@lalchand更改後的代碼現在將scrollpane添加到其他JPanel。 – YoK 2010-08-21 14:25:44

3

JScrollPane的是,像所有的JComponent衍生物,裝飾器,你可以用它裝飾的任何部件。

您可以將任何組件放在滾動窗格中。

JScrollPane pane = new JScrollPane(component); 

只需添加滾動窗格而不是封裝的組件。