How to make jLabels stay attached to the corners of a window form, despite resizing the form in java?

送分小仙女□ 提交于 2019-12-25 02:43:38

问题


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.

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

Thanks


回答1:


  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.

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.

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

A couple other notes:

  • I try to avoid using GridBagLayouts since they are one of the more complex layouts, but for your problem, they work nicely and simply.
  • Your problem can also be solved by using nested JPanels each using a simpler layout such as a 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();
         }
      });
   }
}


来源:https://stackoverflow.com/questions/21463352/how-to-make-jlabels-stay-attached-to-the-corners-of-a-window-form-despite-resiz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!