2014-03-24 6 views
0

좋아, 이제 프로그래밍 연습에 약간의 문제가 있습니다.테두리 및 흐름 레이아웃

다음과 같은 요구 사항을 충족하는 프로그램을 작성합니다 (FlowLayout의 관리자를 사용하여) :

  • 프레임을 만들고 에 레이아웃 설정을 FlowLayout의
  • 운동 텍스트는 다음과 같이 간다

  • 두 개의 패널을 만들어 프레임에 추가하십시오.
  • 각 패널에는 세 개의 버튼이 있습니다. 패널 등등 FlowLayout의 버튼은 "버튼 1"이름을 지정해야합니다

, "버튼 2"를 사용합니다. (나는 원래 코드를 완성했다.)

내 패널을 남쪽으로 이동하고 다른 패널을 가운데로 옮기는 동안 코드를 바꿔야했지만 시도했지만 제대로 나오지 않는 것 같습니다. 버튼은 위아래에 있습니다.

원본 코드 (을 FlowLayout의) 다음 의 BorderLayout에서

import javax.swing.*; 
import java.awt.*; 

public class lab5_1 extends JFrame { 

    public lab5_1() { 
     setLayout(new FlowLayout()); 

     // Create two panels 
     JPanel panel1 = new JPanel(); 
     JPanel panel2 = new JPanel(); 


     // Add three buttons to each panel 
     panel1.add(new JButton(" Button 1 ")); 
     panel1.add(new JButton(" Button 2 ")); 
     panel1.add(new JButton(" Button 3 ")); 
     panel2.add(new JButton(" Button 4 ")); 
     panel2.add(new JButton(" Button 5 ")); 
     panel2.add(new JButton(" Button 6 ")); 


     // Add panels to frame 
     add(panel1); 
     add(panel2); 

    } 

    public static void main(String[] args) { 
     lab5_1 frame = new lab5_1(); 
     frame.setTitle(" Exercise 12_1 "); 
     frame.setSize(600,75); 
     frame.setLocationRelativeTo(null); // center frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

내 시도 :

public class lab5_2 extends JFrame { 

    public lab5_2() { 
    setLayout(new BorderLayout()); 

    // Create two panels 
     JPanel panel1 = new JPanel(); 
     JPanel panel2 = new JPanel(); 


     // Add three buttons to each panel 
     panel1.add(new JButton(" Button 1 ")); 
     panel1.add(new JButton(" Button 2 ")); 
     panel1.add(new JButton(" Button 3 ")); 
     panel2.add(new JButton(" Button 4 ")); 
     panel2.add(new JButton(" Button 5 ")); 
     panel2.add(new JButton(" Button 6 ")); 

     //Add Panel to frame 
     add(panel1, BorderLayout.CENTER); 
     add(panel2, BorderLayout.SOUTH); 
    } 


    public static void main(String[] args) { 
     lab5_2 frame = new lab5_2(); 
     frame.setTitle(" Exercise 12_2 "); 
     frame.setSize(200, 200); 
     frame.setLocationRelativeTo(null); // center frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 
+1

IMO 'NORTH' 또는'WEST' 또는 'EAST' 영역을 사용하고 있지 않기 때문에'CENTER' 영역은'SOUTH' 다음에 남아있는 공간을 채 웁니다 (적어도 [ Oracle 설명서 - BorderLayout] (http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html)). 그렇지 않은 경우 사용되지 않은 지역에 대해 빈 공간 *을 보았을 것입니다. – pasty

+0

@pasty 그래서 내 코드는 정확하지만 나머지는 채워지지 않으므로 게재 위치가 꺼져 있습니까? – Ryan

+0

페이스트리에 동의합니다. 중앙 부분은 사용 가능한 모든 공간을 채우기 위해 확장됩니다. 어쩌면 당신이 무엇을 기대했는지에 대한 그림을 보여줄 수 있습니다. – csmckelvey

답변

1

중심 영역은 가능한 한 사용 가능한 공간을 많이 가져옵니다. 다른 영역은 사용 가능한 모든 공간을 채우기 위해 필요한만큼만 확장됩니다.

관련 문제