尽管在java中调整表单大小,如何使jLabel保持附加到窗口的角落?

时间:2014-01-30 17:27:02

标签: java swing layout jlabel layout-manager

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

这是我的jLabel代码

    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 个答案:

答案 0 :(得分:3)

  1. 不要使用空布局
  2. 请勿使用setBounds(...)
  3. 使用适当的布局管理器。阅读Layout Manager Tutorials了解所有血腥细节。
  4. 请注意,通过使用null布局和setBounds,您可以将应用程序的布局变为非常严格,非常难以调试,增强和修改,并且您还可以创建一个可能在您的盒子上看起来很好的GUI,但是在使用不同操作系统的另一个盒子上,或者甚至是具有略微不同的屏幕分辨率的相同操作系统,可能看起来不太好。

    例如,使用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,另一个使用嵌套的JPanel,每个使用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();
             }
          });
       }
    }