尽管在Java中调整了jLabel的大小,如何使jLabel保持与窗口窗体的角点相连? [英] How to make jLabels stay attached to the corners of a window form, despite resizing the form in java?

查看:76
本文介绍了尽管在Java中调整了jLabel的大小,如何使jLabel保持与窗口窗体的角点相连?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java程序中有4个jLabel,尽管用户调整了窗口的大小,但我仍将其放置在4个角中,我希望它们保持在那里.我已经为标签编写了代码,但是似乎无法弄清楚如何将它们粘在每个角落.

I have 4 jLabels in my java program, which i placed in 4 corners I want them to stay there despite user resizing the window. I have written the code for the labels, but cannot seem to figure out how to keep them glued to each corner.

这是我的jLabels代码

here is my code for the jLabels

    JLabel label_1 = new JLabel("");
    label_1.setEnabled(false);
    label_1.setBounds(0, 0, 19, 19);
    contentPane.add(label_1);

    JLabel label_2 = new JLabel("");
    label_2.setEnabled(false);
    label_2.setBounds(0, 242, 19, 19);
    contentPane.add(label_2);

    JLabel label_3 = new JLabel("");
    label_3.setEnabled(false);
    label_3.setBounds(549, 242, 19, 19);
    contentPane.add(label_3);

    JLabel label_4 = new JLabel("");
    label_4.setEnabled(false);
    label_4.setBounds(549, 0, 19, 19);
    contentPane.add(label_4);

谢谢

推荐答案

  1. 不要使用空布局
  2. 不要使用setBounds(...)
  3. 请使用适当的布局管理器.阅读布局管理器教程以获取所有详细信息. li>
  1. Don't use null layouts
  2. Don't use setBounds(...)
  3. Do use proper layout managers. Read the Layout Manager Tutorials for all the gory details.

请注意,通过使用空布局和setBounds,您会将应用程序的布局字符串化为非常僵硬,难以调试,增强和修改,并且还创建了一个在盒子上看起来不错的GUI,但是在使用不同操作系统甚至在屏幕分辨率稍有不同的同一操作系统上的其他盒子上,可能看起来效果也不佳.

Note that by using a null layout and setBounds, you ham-string your application's layout to being very rigid, very difficult to debug, enhance, and modify, and you also create a GUI that might look good on your box, but likely will not look good on another box using a different OS, or even the same OS with a slightly different screen resolution.

例如,使用GridBagLayout:

For example, using a GridBagLayout:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import static java.awt.GridBagConstraints.*;

public class LabelLayout extends JPanel {
   private static final int[] ANCHORS = {NORTHWEST, SOUTHWEST, NORTHEAST, SOUTHEAST};

   public LabelLayout() {
      setLayout(new GridBagLayout());
      for (int i = 0; i < ANCHORS.length; i++) {
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.gridx = i / 2;
         gbc.gridy = i % 2;
         gbc.gridheight = 1;
         gbc.gridwidth = 1;
         gbc.weightx = 1.0;
         gbc.weighty = 1.0;
         gbc.anchor = ANCHORS[i];
         add(new JLabel("Label " + (i + 1)), gbc);
      }
   }


   private static void createAndShowGui() {
      JFrame frame = new JFrame("Labels");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new LabelLayout());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

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

其他一些注意事项:

  • 我尝试避免使用GridBagLayouts,因为它们是较复杂的布局之一,但是对于您的问题,它们很好且简单地工作.
  • 您还可以通过使用嵌套的JPanel来解决您的问题,每个嵌套的JPanel都使用一个更简单的布局(例如BorderLayout).

演示程序,迭代编号2显示了两个GUI,一个使用GridBagLayout,另一个使用嵌套的JPanels,每个使用BorderLayout:

Demo program, iteration number 2 that shows two GUI's, one using GridBagLayout and the other using nested JPanels, each using BorderLayout:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

import static java.awt.GridBagConstraints.*;

public class LabelLayout {
   private static final int[] ANCHORS = { NORTHWEST, NORTHEAST, SOUTHWEST,
         SOUTHEAST };
   private JPanel gridBagPanel = new JPanel(new GridBagLayout());
   private JPanel borderPanel = new JPanel(new BorderLayout());

   public LabelLayout() {
      for (int i = 0; i < ANCHORS.length; i++) {
         GridBagConstraints gbc = new GridBagConstraints();
         gbc.gridx = i % 2;
         gbc.gridy = i / 2;
         gbc.gridheight = 1;
         gbc.gridwidth = 1;
         gbc.weightx = 1.0;
         gbc.weighty = 1.0;
         gbc.anchor = ANCHORS[i];
         gridBagPanel.add(new JLabel("Label " + (i + 1)), gbc);
      }

      JPanel northPanel = new JPanel(new BorderLayout());
      JPanel southPanel = new JPanel(new BorderLayout());

      northPanel.add(new JLabel("Label 1"), BorderLayout.WEST);
      northPanel.add(new JLabel("Label 2"), BorderLayout.EAST);
      southPanel.add(new JLabel("Label 3"), BorderLayout.WEST);
      southPanel.add(new JLabel("Label 4"), BorderLayout.EAST);

      borderPanel.add(northPanel, BorderLayout.NORTH);
      borderPanel.add(southPanel, BorderLayout.SOUTH);
   }

   public JPanel getGridBagPanel() {
      return gridBagPanel;
   }

   public JPanel getBorderPanel() {
      return borderPanel;
   }

   private static void createAndShowGui() {
      LabelLayout labelLayout = new LabelLayout();

      JFrame frame = new JFrame("Label GridBagLayout");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(labelLayout.getGridBagPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);

      frame = new JFrame("Label BorderLayout");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(labelLayout.getBorderPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);

   }

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

这篇关于尽管在Java中调整了jLabel的大小,如何使jLabel保持与窗口窗体的角点相连?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆