2010-08-21 4 views
1

다른 jPanel에서 차례로 호출되는 JPanel에 Scrollpane을 추가하고 싶습니다.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에 배치 할 수 있습니다. –

답변

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에 추가되었지만 jpanel에 scrollerpane이 필요하다면 scrollerpane이 프레임이 아닌 다른 jpanel에 추가됩니다. – Lalchand

+0

다른 jpanel이 jframe에 추가 되었습니까? – YoK

+0

@lalchand 이제 코드가 변경되었습니다. 스크롤 패널이 다른 JPanel에 추가되었습니다. – YoK

3

JScrollPane의 모든 JComponent의 유도체, 데코레이터처럼, 당신은 그것으로 모든 구성 요소를 장식하실 수 있습니다.

스크롤 창에 구성 요소를 넣을 수 있습니다.

JScrollPane pane = new JScrollPane(component); 

그리고 단지 래핑 된 구성 요소 대신에 스크롤 영역을 추가하십시오.