2012-12-25 4 views
1

하나의 JFrame 폼에 3 개의 패널을 추가하려고합니다. 내가 그들 만이 표시되고있는 세 개의 패널을 추가하고하지만 난 분할 창 아무것도에 패널을 추가하는 경우 표시되는 경우 라인 cp.setLayout(null)을 제거 코드를자바의 분할 창에 jpanels 추가

import javax.swing.*; 
import java.awt.*; 
class paneltest extends JFrame{ 
paneltest() 
{ 
    Container cp=this.getContentPane(); 
    cp.setLayout(null); 
    panel1 p1= new panel1(); 
    panel2 p2= new panel2(); 
    panel3 p3= new panel3(); 
    cp.add(p1); 
    cp.add(p2); 
    cp.add(p3); 
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); 

    p1.setBounds(0,0,screenSize.width/3,screenSize.height); 
    p2.setBounds(screenSize.width/3,0,screenSize.width/3,screenSize.height); 
    p3.setBounds(2*(screenSize.width/3),0,screenSize.width/3,screenSize.height); 

    try{ 

     JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 
     JSplitPane splitPaneRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 
     splitPaneLeft.setLeftComponent(p1); 
     splitPaneLeft.setRightComponent(p2); 
     splitPaneRight.setLeftComponent(splitPaneLeft); 
     splitPaneRight.setRightComponent(p3); 

     JPanel panelSplit = new JPanel(); 
     panelSplit.add(splitPaneRight); 
     cp.add(panelSplit); 
     panelSplit.setVisible(true); 
     } 
    catch(Exception ex) 
    { 

    JOptionPane.showMessageDialog(null,"exception occured"+ex); 

    } 
    } 
    public static void main(String arsg[]) 
    { 

     paneltest frm= new paneltest(); 
     frm.show(); 

     } 

     } 
     class panel1 extends JPanel 
     { 
     panel1() 
      { 
      setLayout(new FlowLayout()); 
      JLabel l1= new JLabel("panel1"); 
      add(l1); 


      }  

      } 

     class panel2 extends JPanel 
     { 
     panel2() 
      { 
      setLayout(new FlowLayout()); 
      JLabel l1= new JLabel("panel2"); 
      add(l1); 

      }  

     } 
     class panel3 extends JPanel 
     { 
     panel3() 
     { 
      setLayout(new FlowLayout()); 
      JLabel l1= new JLabel("panel3"); 
      add(l1); 


     }  

     } 
+0

1)이 코드는 cp.setLayout (널)'에서 내리막 충전을 시작한다; *** '을 사용하여 레이아웃을 *** 2) 코드 블록에 대한 일관되고 논리적 들여 쓰기를 사용하여 . 코드의 들여 쓰기는 사람들이 프로그램 흐름을 이해하도록 돕기위한 것입니다. –

답변

2

다음의 오류를 제안한다. 이렇게하면 초기 문제가 해결됩니다. 그 후

:

  • 들여 쓰기 코드 당신이 바로 후 splitpanes에 추가 할 경우 콘텐츠 창에 패널을 추가하지
  • 존중 자바 명명 규칙
  • . 단일 상위 요소에 구성 요소를 추가 할 수 있습니다. 모두에 추가하는 것이 의미가 없습니다
  • setBounds()을 사용하지 마십시오. 이것이 레이아웃 관리자의 역할입니다.
  • JPanel 및 JFrame을 확장하지 마십시오. 그들을 사용하십시오
  • 존중 Swing's threading policy. !
  • 하지 catch (Exception)
+0

나는 setLayout (null)을 제거하고 컨테이너에 패널을 추가하지 않기 때문에 패널이 표시되지만 크기를 조정할 수는 없습니다. –

+0

'panelSplit' 패널은 FlowLayout (기본값)을 사용하기 때문입니다. BorderLayout을 사용하도록 (듯이)하면, splitpane는 거기에서 사용 가능한 스페이스를 모두 차지합니다. –