2013-08-28 2 views
2

다음 소스 코드가 있습니다 ... 누군가 jsprollpane을 jframe에 추가하는 방법에 대한 조언을 해 줄 수 있습니까? jframe에 추가하는 데 여러 번 시도했지만 아무런 진전이 없었습니다. 그것은 심지어 보이지 않습니다. 내 응용 프로그램의jframe에 jscrollpane을 추가하는 방법은 무엇입니까?

public class Form3 { 
     JFrame jframe = new JFrame("Etiket print."); 
     JPanel panel1 = new JPanel(); 
     JPanel panel2 = new JPanel(); 
     JPanel panel3 = new JPanel(); 
     JPanel panel4 = new JPanel(); 
     JScrollPane scrollFrame = new JScrollPane(panel2); 
     Color myBlue1Color = new Color(168, 175, 247); 
     Color myBlue2Color = new Color(139, 146, 255); 



      public Form3(){ 
       jframe.setMinimumSize(new Dimension(1280, 1000)); 
       panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS)); 
       panel2.setAutoscrolls(true); 
       jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       //---------------------------------Header 
       panel1 = createSquareJPanel(Color.YELLOW, 600,200); 
       panel3 = createSquareJPanel(Color.GREEN, 400,200); 
       panel4 = createSquareJPanel(Color.white, 280,200); 
       JPanel container = new JPanel(); 
       JPanel container1 = new JPanel(); 
       JPanel container2 = new JPanel(); 
       container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); 
       container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS)); 
       container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS)); 


       container1.add(panel1); 
       container2.add(container1); 
       container2.add(panel3); 
       container2.add(panel4); 
       container.add(container2); 
       container.add(panel2); 

       { 

        for (int i=0; i<25; i++){ 

         JPanel harnessPanel= new JPanel(); 
         harnessPanel.setMinimumSize(new Dimension(1280, 70)); 
         harnessPanel.setMaximumSize(new Dimension(1280, 70)); 
         harnessPanel.setPreferredSize(new Dimension(1280, 70)); 
         if(i%2==0) { 
          harnessPanel.setBackground(myBlue1Color); 
         } 
         else { 
          harnessPanel.setBackground(myBlue2Color); 
         } 
         panel2.add(harnessPanel); 

         harnessPanel.repaint(); 
         harnessPanel.validate(); 
        } 
        panel2.repaint(); 
        panel2.validate(); 
       } 
       jframe.add(scrollFrame); 
       jframe.add(container); 

       jframe.pack(); 
       jframe.setLocationRelativeTo(null); 
       jframe.setVisible(true); 

      } 

      private JPanel createSquareJPanel(Color color, int size1, int size2) 
      { 
       JPanel tempPanel = new JPanel(); 
       tempPanel.setBackground(color); 
       tempPanel.setMinimumSize(new Dimension(size1, size2)); 
       tempPanel.setMaximumSize(new Dimension(size1, size2)); 
       tempPanel.setPreferredSize(new Dimension(size1, size2)); 
       return tempPanel; 
      } 

      public static void main (String args[]){ 
       SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         Form3 myF=new Form3(); 

        } 
       }); 
      }; 
     } 

사진 : enter image description here

실제 상태는 : enter image description here

+0

가능한 중복 (http://stackoverflow.com/questions/18381857/scrollable-panel-in-swing) - 그것은 질문을 반복하는 데 도움이되지 않습니다, 당신은 열심히 답변을 이해하려고 노력 ... – kleopatra

답변

5

JFrame은 기본적으로 BorderLayout 사용합니다. 기본 위치 (지정하지 않은 경우)는 CENTER입니다.

BorderLayout은 하나의 구성 요소가 5 개의 사용 가능한 위치 중 하나만 차지할 수있게합니다. 당신이 그래서

은 ...

jframe.add(scrollFrame); 
jframe.add(container); 

이 중심 위치로 scrollFrame을 추가하고 효율적으로 당신이 실제로 제거되지 않습니다 (container를 추가 할 때를 제거하지만 결과는 대한 그냥 같은).

위치 제한을 제공하십시오. 예를 들어 ...

jframe.add(scrollFrame); 
jframe.add(container, BorderLayout.SOUTH); 

은 자세한 내용은 How to use BorderLayout를 참조

[스윙 스크롤 패널]의
+0

나는 이것을 이렇게 변경하려고했습니다 : jframe.add (scrollFrame, BorderLayout.EAST); jframe.add (container, BorderLayout.WEST); 이제 동쪽에있는 얇은 구성 요소 (cca 0.5mm 크기)를 볼 수 있지만 여전히 스크롤링에 사용할 수 없기 때문에 무엇이 잘못 될 수 있는지 알고 있습니까? – Michael

+0

뷰포트는 preferredSize 또는 구현 된 경우 preferredScrollabelSize를 사용합니다. – MadProgrammer

관련 문제