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

2 回答 2

2

你犯的错误:

改变

myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

然后调用你的buildLabel()方法,这样你JLabel的 s 就可以被初始化了。

最后,addcomponentsToFrame();当你应该addComponentsToFrame();用大写字母写作时,你正在写作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();
    }
}
于 2012-03-20T18:08:57.163 回答
1

您需要先创建一个 GridBagLayoutDemo 的实例。像这样的东西会起作用。

public static void main(String args[]) {
    GridBagLayoutDemo demo = new GridBagLayoutDemo();
    demo.buildLabel();
    demo.addComponentsToFrame();
}
于 2012-03-20T18:03:52.893 回答