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();
}
}