2012-11-26 5 views
4

내가하려는 것은 프레임 내에 5 개의 별도 JPanel을 구성하는 것입니다. 결과물은 다음과 같습니다. 맨 위에 한 개의 패널이 있습니다. 위쪽 패널 바로 아래에 공간을 수직으로 분할 한 두 개의 패널과 나머지 공간을 수평으로 나누는 또 다른 두 개의 패널.JFrame에서 여러 JPanel을 구성하는 좋은 방법은 무엇입니까?

enter image description here

나는 위의 설명처럼 패널을 구성하는 방법을 알아낼 수 없어 난 그냥 적절한 구문을 모르기 때문에 그것이라고 생각합니다. 그래서 어떤 도움이나 조언을 크게 여기에 내가 지금까지 가지고있는 코드입니다 감사합니다.

import java.lang.String.*; 
import java.lang.Exception.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Display extends JFrame implements ActionListener{ 
// instance variables 
private static final int FRAME_WIDTH = 400; 
private static final int FRAME_HEIGHT = 350; 

private static final int FRAME_X_ORIGIN = 150; 
private static final int FRAME_Y_ORIGIN = 150; 

private static final int BUTTON_WIDTH = 90; 
private static final int BUTTON_HEIGHT = 30; 

private JButton readFile; 
private JButton exit; 
private JButton stats; 
private JButton blank; 

private JCheckBox avgHomeworkScore; 
private JCheckBox avgTestScore; 
private JCheckBox sdHomeworkScore; 
private JCheckBox sdTestScore; 

private JRadioButton buttonOne; 
private JRadioButton buttonTwo; 
private JRadioButton buttonThree; 
private JRadioButton buttonFour; 

private JPanel header; 
private JPanel statistics; 
private JPanel courses; 
private JPanel display; 
private JPanel action; 

public static void main(String[] args){ 
    Display frame = new Display(); 
    frame.setVisible(true); 
} 


public Display(){ 

    Container contentPane; 


    //Set the frame properties 
    setSize   (FRAME_WIDTH, FRAME_HEIGHT); 
    setResizable (false); 
    setTitle  ("CSCE155A Course Offerings Viewer"); 
    setLocation  (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 

    contentPane = getContentPane(); 
    contentPane.setLayout(new GridLayout()); 
    contentPane.setBackground(Color.white); 

    //header 





    //Create and Place the Buttons on the frame 
    readFile = new JButton("Read File"); 
    readFile.setBounds(4, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    exit = new JButton("Exit"); 
    exit.setBounds(100, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    stats = new JButton("Stats"); 
    stats.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    blank = new JButton("Clear"); 
    blank.setBounds(290, 285, BUTTON_WIDTH, BUTTON_HEIGHT); 


    action = new JPanel(new FlowLayout()); 
    action.setBackground(Color.blue); 
    action.add(readFile); 
    action.add(exit); 
    action.add(stats); 
    action.add(blank); 
    contentPane.add(action); 

    //Register this frame as an Action listener of the buttons 
    readFile.addActionListener(this); 
    exit.addActionListener(this); 
    stats.addActionListener(this); 
    blank.addActionListener(this); 

    //Create and Place the checkboxes on the frame 
    avgHomeworkScore = new JCheckBox(); 
    avgHomeworkScore.setMnemonic(KeyEvent.VK_H); 
    contentPane.add(avgHomeworkScore); 
    avgHomeworkScore.setSelected(true); 

    avgTestScore = new JCheckBox(); 
    avgTestScore.setMnemonic(KeyEvent.VK_T); 
    avgTestScore.setSelected(true); 

    sdHomeworkScore = new JCheckBox(); 
    sdHomeworkScore.setMnemonic(KeyEvent.VK_S); 
    sdHomeworkScore.setSelected(true); 

    sdTestScore = new JCheckBox(); 
    sdTestScore.setMnemonic(KeyEvent.VK_D); 
    sdTestScore.setSelected(true); 

    statistics = new JPanel(new GridLayout(0,1)); 
    contentPane.add(statistics); 
    statistics.add(avgHomeworkScore); 
    statistics.add(avgTestScore); 
    statistics.add(sdHomeworkScore); 
    statistics.add(sdTestScore); 

    avgHomeworkScore.addActionListener(this); 
    avgTestScore.addActionListener(this); 
    sdHomeworkScore.addActionListener(this); 
    sdTestScore.addActionListener(this); 



    //create the radio buttons 
    buttonOne = new JRadioButton(); 
    buttonOne.setMnemonic(KeyEvent.VK_1); 
    buttonOne.setSelected(true); 

    buttonTwo = new JRadioButton(); 
    buttonTwo.setMnemonic(KeyEvent.VK_2); 

    buttonThree = new JRadioButton(); 
    buttonThree.setMnemonic(KeyEvent.VK_3); 

    buttonFour = new JRadioButton(); 
    buttonFour.setMnemonic(KeyEvent.VK_4); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(buttonOne); 
    group.add(buttonTwo); 
    group.add(buttonThree); 
    group.add(buttonFour); 

    buttonOne.addActionListener(this); 
    buttonTwo.addActionListener(this); 
    buttonThree.addActionListener(this); 
    buttonFour.addActionListener(this); 

    courses = new JPanel(new GridLayout(0,1)); 
    courses.setBackground(Color.blue); 
    courses.add(buttonOne); 
    courses.add(buttonTwo); 
    courses.add(buttonThree); 
    courses.add(buttonFour); 
    contentPane.add(courses); 

    //Exit program when the viewer is closed 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 
+1

아마도 ASCII 아트를 사용하여 패널 배치의 정확성을 보여줄 수 있습니다. – David

답변

7

레이아웃 관리자를 사용하십시오. 패널 및 기타 구성 요소의 범위, 크기 및 위치를 절대로 설정하지 마십시오. 이것이 레이아웃 관리자의 임무입니다. 레이아웃 관리자는 the Swing tutorial (다른 모든 것, BTW)에 자세히 설명되어 있습니다.

메인 패널에는 BorderLayout을, 하단 패널에는 BorderLayout을 사용할 수 있습니다.

+0

원래 GridLayout으로 구성하려했지만 작동하지 않았습니다. 나는 그것이 위에 보이기로되어있는 것을 올렸다. 관리자가 그것을 성취 할 수있게 할 것인가? – user1854263

+0

북쪽에는 CSC ... 레이블이있는 BorderLayout을 사용할 수 있으며, 가운데에는 선택 패널을, 동쪽에는 코스 제공을, 남쪽에는 다른 두 패널을 포함하는 패널을 사용할 수 있습니다. 이 남쪽 패널 자체는 중앙의 통계 및 남쪽의 버튼과 함께 BorderLayout을 사용합니다. GridBagLayout, GroupLayout 등과 같은 다른 가능성도 있습니다. –

관련 문제