简体   繁体   English

将4 JLabel放在JFrame的角落

[英]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. 正如标题所说,我试图在JFrame的每个角落放置4个不同的JLabel。 I want them to stay there forever even if I try to resize the JFrame 我希望他们永远呆在那里,即使我试图调整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. 我也试过GridLayout(2,2),但他们并没有在角落里,右侧有很大的差距。

You will want to nest JPanels each using its own layout. 您将希望使用自己的布局嵌套 JPanel。 In fact you could do this by nesting JPanels that all use BorderLayout. 事实上,你可以通过嵌套所有使用BorderLayout的JPanel来做到这一点。

Going to check if GridBagLayout can do it in one shot.... hang on... 要检查GridBagLayout是否可以一次性完成....挂起......

Yep GridBagLayout does it too: 是的GridBagLayout也是这样做的:

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: 现在为BorderLayout示例:

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

I find that only GroupLayout gives me the control I need for precisely laid out components. 我发现只有GroupLayout为我精确布局的组件提供控制。 This should do the trick. 这应该可以解决问题。 You need to make sure the gap in between has a very large Maximum value (ie Short.MAX_VALUE ), but you can set the minimum and preferred sizes to whatever you want. 您需要确保其间的间隙具有非常大的最大值(即Short.MAX_VALUE ),但您可以将最小和首选大小设置为您想要的任何值。

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

}

I've tried using a layout manager.. 我尝试过使用布局管理器..

Layout managers are wonderful at what they do, but are perhaps the wrong tool for this job. 布局经理对他们的工作非常了解,但也许是这项工作的错误工具。 Consider using a custom border instead. 请考虑使用自定义边框。 Here is an example . 这是一个例子

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

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