Как добавить три метки в рамку с помощью Grid Bag Layout? - PullRequest
0 голосов
/ 20 марта 2012

Попытка использовать 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 ]

2 голосов
/ 20 марта 2012

Ошибки, которые вы совершили:

Изменить

myFrame.getDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

до

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Затем вызовите ваш метод buildLabel(), чтобы ваши JLabel могли быть инициализированы.

И, наконец, вы пишете 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();
    }
}
1 голос
/ 20 марта 2012

Вам нужно сначала создать экземпляр GridBagLayoutDemo. Нечто подобное будет работать.

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