简体   繁体   English

如何使用“网格袋布局”在框架中添加三个标签?

[英]How do I add three labels to a frame using Grid Bag Layout?

Trying to use GridBagLayout. 尝试使用GridBagLayout。

I have the method called buildLabel. 我有一个叫做buildLabel的方法。 This creates three label. 这将创建三个标签。 Another method called addComponentsToFrame. 另一个方法称为addComponentsToFrame。 This builds the frame and created a panel. 这将构建框架并创建一个面板。 It also adds the three labels to the panel. 还将三个标签添加到面板上。 Now I want to display what I have done. 现在,我想展示我所做的。 How do I do display the frame. 如何显示框架。 Here is my code! 这是我的代码!

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

Mistakes you had committed : 您犯的错误:

Change 更改

myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

to

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Then call your buildLabel() method, so that your JLabel s can be initialized. 然后调用您的buildLabel()方法,以便可以初始化JLabel

And lastly, you are writing addcomponentsToFrame(); 最后,您正在编写addcomponentsToFrame(); when you should be writing addComponentsToFrame(); 什么时候应该编写addComponentsToFrame(); with capitalized C 大写的C

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

You need to create an instance of GridBagLayoutDemo first. 您需要首先创建一个GridBagLayoutDemo实例。 Something like this will work. 这样的事情会起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM