如何使用网格包布局向框架添加三个标签?

时间:2012-03-20 17:52:33

标签: java gridbaglayout

尝试使用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();
}
}

2 个答案:

答案 0 :(得分:2)

你承诺的错误:

更改

myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

然后调用buildLabel()方法,以便初始化JLabel

最后,当你用大写的 C

编写addcomponentsToFrame();时,你正在写addComponentsToFrame();
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();
    }
}

答案 1 :(得分:1)

您需要先创建GridBagLayoutDemo的实例。这样的事情会起作用。

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