2012-03-20 3 views
-1

GridBagLayout을 사용하려고합니다.그리드 백 레이아웃을 사용하여 프레임에 세 개의 라벨을 어떻게 추가합니까?

나는 buildLabel이라는 메서드를 가지고있다. 이렇게하면 세 개의 레이블이 만들어집니다. addComponentsToFrame이라는 또 다른 메서드입니다. 이렇게하면 프레임이 만들어져 패널이 만들어집니다. 또한 세 개의 레이블을 패널에 추가합니다. 이제 나는 내가 한 일을 보여주고 싶다. 프레임을 어떻게 표시합니까? 여기 내 코드가있다!

@author eeua9b 

public class GridBagLayoutDemo extends JFrame { 

private JLabel label1; 
private JLabel label2; 
private JLabel label3; 
private JFrame myFrame; 
private JPanel p; 

// build the Labels 
private void buildLabel() { 

    label1 = new JLabel("Tables"); 
    label2 = new JLabel("Reports"); 
    label3 = new JLabel("Forms"); 

} 

/** 
* build the frame 
*add the labels to panel 
*add the panel to the frame. 
* set the gridBagLayout 
*/ 
private void addComponentsToFrame() { 
    myFrame = new JFrame("My Frame"); 
    myFrame.setSize(600, 400); 

    //this is underlined in red. 
    myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    p = new JPanel(new GridBagLayout()); 

    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.insets = new Insets(15, 15, 15, 15); 

    p.add(label1, gbc); 
    p.add(label2, gbc); 
    p.add(label3, gbc); 

    myFrame.add(p); 

    myFrame.setVisible(true); 
} 

public static void main(String args[]) { 
    //show the frame. this is underlined in red. 
    addcomponentsToFrame(); 
} 
} 

답변

1

먼저 GridBagLayoutDemo의 인스턴스를 만들어야합니다. 이 같은 것이 효과가 있습니다.

public static void main(String args[]) { 
    GridBagLayoutDemo demo = new GridBagLayoutDemo(); 
    demo.buildLabel(); 
    demo.addComponentsToFrame(); 
} 
2

실수 당신이 저지른 : 당신의 JLabel의 초기화 할 수 있도록

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

변경

myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

그런 다음 buildLabel() 메서드를 호출합니다. 당신이

import java.awt.*; 
import javax.swing.*; 
public class GridBagLayoutDemo extends JFrame 
{ 
    private JLabel label1; 
    private JLabel label2; 
    private JLabel label3; 
    private JFrame myFrame; 
    private JPanel p; 

    // build the Labels 
    private void buildLabel() 
    { 
     label1 = new JLabel("Tables"); 
     label2 = new JLabel("Reports"); 
     label3 = new JLabel("Forms"); 
    } 

    /** 
    * build the frame 
    *add the labels to panel 
    *add the panel to the frame. 
    * set the gridBagLayout 
    */ 
    private void addComponentsToFrame() 
    { 
     myFrame = new JFrame("My Frame"); 
     myFrame.setSize(600, 400); 

     //this is underlined in red. 
     myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     p = new JPanel(new GridBagLayout()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(15, 15, 15, 15); 
     // Add these lines to take these JLabels to the TOP. 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.weightx = 1.0; 
     gbc.weighty = 0.1; 

     p.add(label1, gbc); 
     p.add(label2, gbc); 
     p.add(label3, gbc); 

     myFrame.add(p); 

     myFrame.setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
     //show the frame. this is underlined in red. 
     GridBagLayoutDemo gbld = new GridBagLayoutDemo(); 
    gbld.buildLabel(); 
    gbld.addComponentsToFrame(); 
    } 
} 
C 대문자 addComponentsToFrame();를 작성해야하는 경우

그리고 마지막으로, 당신은 addcomponentsToFrame();을 작성하는

관련 문제