Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put 4 JLabel at corners of a JFrame

As the title said, I'm trying to put 4 different JLabels at each of the corners of a JFrame. I want them to stay there forever even if I try to resize the JFrame

I've tried using a layout manager but I just can't get it right.

    ImageIcon icon;
    JLabel labelNW = new JLabel();
    JLabel labelNE = new JLabel();
    JLabel labelSW = new JLabel();
    JLabel labelSE = new JLabel();
    URL buttonURL = InputOutputTest.class.getResource("images/square_dot.gif");
    if(buttonURL != null){
        icon = new ImageIcon(buttonURL);
        labelNW.setIcon(icon);
        labelNE.setIcon(icon);
        labelSW.setIcon(icon);
        labelSE.setIcon(icon);
    }
    window.add(labelNW, BorderLayout.NORTH);
    //window.add(labelNE, BorderLayout.EAST);
    //window.add(labelSW, BorderLayout.WEST);
    window.add(labelSE, BorderLayout.SOUTH);

This code takes care of the north and south of the left side. I'm probably approaching this wrong though.

I also tried GridLayout (2,2) but they weren't at the corners and there's a huge gap on the right side.

like image 461
kir Avatar asked Dec 08 '22 13:12

kir


2 Answers

You will want to nest JPanels each using its own layout. In fact you could do this by nesting JPanels that all use BorderLayout.

Going to check if GridBagLayout can do it in one shot.... hang on...

Yep GridBagLayout does it too:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class GridBagExample {
   public static void main(String[] args) {
      JPanel mainPanel = new JPanel(new GridBagLayout());

      GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
            GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(
                  0, 0, 0, 0), 0, 0);
      mainPanel.add(new JLabel("Left Upper"), gbc);

      gbc.gridx = 1;
      gbc.gridy = 0;
      gbc.anchor = GridBagConstraints.NORTHEAST;
      mainPanel.add(new JLabel("Right Upper"), gbc);

      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.SOUTHWEST;
      mainPanel.add(new JLabel("Left Lower"), gbc);

      gbc.gridx = 1;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.SOUTHEAST;
      mainPanel.add(new JLabel("Right Lower"), gbc);

      JFrame frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.setLocationRelativeTo(null);
      frame.pack();
      frame.setVisible(true);
   }
}

Edit
Now for the BorderLayout example:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class BorderLayoutExample {
   public static void main(String[] args) {
      JPanel northPanel = new JPanel(new BorderLayout());      
      northPanel.add(new JLabel("North East"), BorderLayout.EAST);
      northPanel.add(new JLabel("North West"), BorderLayout.WEST);

      JPanel southPanel = new JPanel(new BorderLayout());
      southPanel.add(new JLabel("South East"), BorderLayout.EAST);
      southPanel.add(new JLabel("South West"), BorderLayout.WEST);


      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(northPanel, BorderLayout.NORTH);
      mainPanel.add(southPanel, BorderLayout.SOUTH);

      JFrame frame = new JFrame("BorderLayout Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }
}
like image 57
Hovercraft Full Of Eels Avatar answered Dec 27 '22 18:12

Hovercraft Full Of Eels


I find that only GroupLayout gives me the control I need for precisely laid out components. This should do the trick. You need to make sure the gap in between has a very large Maximum value (i.e. Short.MAX_VALUE), but you can set the minimum and preferred sizes to whatever you want.

public class LabelFrame extends JFrame {
    public LabelFrame() {
        JPanel contentPane = new JPanel();
        JLabel labelNW = new JLabel();
        JLabel labelNE = new JLabel();
        JLabel labelSW = new JLabel();
        JLabel labelSE = new JLabel();
        URL buttonURL = InputOutputTest.class.getResource("images/square_dot.gif");
        if(buttonURL != null){
            icon = new ImageIcon(buttonURL);
            labelNW.setIcon(icon);
            labelNE.setIcon(icon);
            labelSW.setIcon(icon);
            labelSE.setIcon(icon);
        }
        GroupLayout layout = new GroupLayout(contentPane);
        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                        .addComponent(labelNW)
                        .addComponent(labelSW))
                .addGap(20,50,Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                        .addComponent(labelNE)
                        .addComponent(labelSE))
        );
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                        .addComponent(labelNW)
                        .addComponent(labelNE))
                .addGap(20,50,Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                        .addComponent(labelSW)
                        .addComponent(labelSE))
        );
        contentPane.setLayout(layout);
        setContentPane(contentPane);
        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                LabelFrame frame = new LabelFrame();
                frame.setVisible(true);
            }
        });
    }

}
like image 39
durron597 Avatar answered Dec 27 '22 18:12

durron597