Domanda

I am using gridbag layout for the flexible layout of my project but really it is now becoming headache for me ,,after a hard try of nearly 5 hours i am not able to do the correct positionin according to my requirement ,,plz figure it out for me :-

public static void main(String[] args) {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setFont(f);
        frame.setTitle("Natural Language Processor");

        frame.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

            // Setting the label for the nlp            
            l = new JLabel("Natural Language Processor");
            l.setFont(f);
            c.weightx = 0.5;
            c.gridx = 0;
            c.gridy = 0;
            c.ipady = 20;
            c.fill = GridBagConstraints.NONE;
            c.anchor = GridBagConstraints.PAGE_START;
            frame.add(l, c);

            l1 = new JLabel("English");
            l.setFont(f1);
            c.weightx = 0.0;
            c.weighty = 1.0;
            c.gridy = 1;
            c.gridx = 0;

            //c.ipady = 20;
            c.fill = GridBagConstraints.NONE;
            c.anchor = GridBagConstraints.PAGE_START;

            frame.add(l1, c);

output of code

In the pic above I want the label "English" to be below the "Natural" of "natural Language processor" label instead of centering it.

È stato utile?

Soluzione

First of all don't forget to run your Swing code in the EDT by invoking all this code using SwingUtilities.invokeLater(Runnable r).

There are several ways to do this, which one is the best depends on what else you'll put on the UI. This is one of the shortest ways to do it:

WAY 1

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TitleTestFrame extends JFrame
{

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TitleTestFrame();
            }
        });
    }

    public TitleTestFrame()
    {
        super("frame title");

        JLabel titleLabel = new JLabel("This is a centered title.");
        JLabel subtitleLabel = new JLabel("Subtitle");

        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.0; //all components in the row with weightx = 0.0 keeps the components clump together in the center.
                           //This way the labels will be in the center of the panel.

        c.anchor = GridBagConstraints.FIRST_LINE_START; //FIRST_LINE makes the title and subtitle stay at the top of the frame.
                          //LINE_START make them be glued to the beginning of its column. However because the weightx = 0.0 the column is centered.


        mainPanel.add(titleLabel,  c);
        c.gridy = 1;
        c.weighty = 1.0;  // this weight must be put in the components under the titles so they don't get far from each other.
        mainPanel.add(subtitleLabel, c);


        add(mainPanel);

        setSize(new Dimension(400,300));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
}

Result:

simple code

I don't like this way a lot because it uses weightx = 0.0 to keep the labels centered, if you needed to put more GUI elements at the sides you'll need to put the labels in the center column with weightx 0.0 and the elements at each side with weightx >0.0.

For example, replacing the code above with the following we have:

WAY 2

JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.0;
c.anchor = GridBagConstraints.FIRST_LINE_START;

mainPanel.add(titleLabel,  c);
c.gridy = 1;
mainPanel.add(subtitleLabel, c);

JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.red);
colorPanel.setOpaque(true);

JPanel colorPanel2 = new JPanel();
colorPanel2.setBackground(Color.gray);
colorPanel2.setOpaque(true);

c.weighty = 1.0;
c.gridy = 0;
c.gridx = 0;
c.weightx = 1.0;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
mainPanel.add(colorPanel, c);
c.gridx = 2;
mainPanel.add(colorPanel2, c);

add(mainPanel);

Result:

way 2

EDIT

The key to doing good layouts with gridbag layout is understanding how are you creating the grid. Then you just have to play with the weight and the anchor to place the components in each cell.

This is my 2x3 grid, on the SECOND WAY (without the colors).

enter image description here

This is your 2x1 grid

enter image description here

With your grid you can't align the beginning of the labels, with my grid you can.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top