java Swing:如何使一个组件左对齐,另一组件相对于父组件居中

java Swing: how to keep one component left aligned with another component centered with respect to parent

假设我在 JPanel 中有两个组件 A 和 B。我希望组件 A 保持左对齐,而组件 B 尽量保持在面板中间。我模拟了以下演示(对质量感到抱歉,我是用油漆制作的):

我现在正在做的是在 JPanel 上使用 GridBagLayout,并在保持 B 居中的同时保持 A 左对齐,但 B 在第二列居中,因此它在 space 之后居中A 被放置,而不是相对于整个面板居中。

我不能为此使用任何第 3 方库。有没有办法使用纯 Swing 来做到这一点?

编辑:

Firefly 的答案是正确的(如标记的那样),但我创建了一个 SSCCE 来显示我的原始问题(第一行)、Hovercraft Full Of Eels 的尝试解决方案(第二行)和 Firefly 的正确解决方案(第三行)。我想 post 它不会有什么坏处:

package Whosebug;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager2;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class WhosebugTest extends JFrame
{

   public WhosebugTest()
   {
      super("Stack Overflow Test");
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);

      JPanel testPanel = new JPanel(new GridLayout(3, 1));

      // set up grid bag layout example
      JPanel gridBagPanel = new JPanel(new GridBagLayout());
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridy = 0;
      gridBagConstraints.anchor = GridBagConstraints.LINE_START;
      gridBagPanel.add(getA(), gridBagConstraints);
      gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.gridy = 0;
      gridBagConstraints.anchor = GridBagConstraints.CENTER;
      gridBagConstraints.weightx = 1.0;
      gridBagPanel.add(getB(), gridBagConstraints);
      testPanel.add(gridBagPanel);

      // set up border layout panel 
      JPanel borderPanel = new JPanel(new BorderLayout());
      borderPanel.add(getA(), BorderLayout.LINE_START);
      JPanel flowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
      borderPanel.add(flowPanel, BorderLayout.CENTER);
      flowPanel.add(getB());
      testPanel.add(borderPanel);

      // set up sly493 layout panel
      JPanel sly493LayoutPanel = new JPanel(new Sly493LayoutManager());
      sly493LayoutPanel.add(getA(), Sly493LayoutManager.LEFT);
      sly493LayoutPanel.add(getB(), Sly493LayoutManager.CENTERED);
      testPanel.add(sly493LayoutPanel);

      // set up panel to act as the midpoint marker
      JPanel midpointMarkerPanel = new JPanel()
      {
         @Override
         public void paintComponent(Graphics g)
         {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;

            g2.setColor(Color.BLACK);

            int w = getWidth();
            int h = getHeight();

            int x = w / 2;
            g2.drawLine(x, 0, x, h);
            g2.drawLine(x, 0, x - (h / 5), (h / 5));
            g2.drawLine(x, 0, x + (h / 5), (h / 5));
         }
      };
      midpointMarkerPanel.setPreferredSize(new Dimension(1, 50));

      // setup up content pane
      JPanel contentPane = new JPanel(new BorderLayout());
      contentPane.add(testPanel, BorderLayout.NORTH);
      contentPane.add(midpointMarkerPanel, BorderLayout.CENTER);
      this.setContentPane(contentPane);

      pack();
   }

   private JPanel getA()
   {
      JPanel aPanel = new JPanel(new BorderLayout());
      JLabel aLabel = new JLabel("A", JLabel.CENTER);
      aLabel.setFont(aLabel.getFont().deriveFont(Font.BOLD, 36));
      aPanel.add(aLabel, BorderLayout.CENTER);
      aPanel.setBackground(Color.RED);
      aPanel.setPreferredSize(new Dimension(50, 50));
      return aPanel;
   }

   private JPanel getB()
   {
      JPanel bPanel = new JPanel();
      JLabel bLabel = new JLabel("B", JLabel.CENTER);
      bLabel.setForeground(Color.WHITE);
      bLabel.setFont(bLabel.getFont().deriveFont(Font.BOLD, 36));
      bPanel.add(bLabel, BorderLayout.CENTER);
      bPanel.setBackground(Color.BLUE);
      bPanel.setPreferredSize(new Dimension(50, 50));
      return bPanel;
   }

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

         @Override
         public void run()
         {
            new WhosebugTest().setVisible(true);
         }
      });
   }

   private static class Sly493LayoutManager implements LayoutManager2
   {

      public static final Integer LEFT = 0;

      public static final Integer CENTERED = 1;

      private Component leftComponent;

      private Component centeredComponent;

      @Override
      public void addLayoutComponent(String name, Component comp)
      {
      }

      @Override
      public void removeLayoutComponent(Component comp)
      {
         if (leftComponent == comp)
         {
            leftComponent = null;
         }
         else if (centeredComponent == comp)
         {
            centeredComponent = null;
         }
      }

      @Override
      public Dimension preferredLayoutSize(Container parent)
      {
         Dimension d = new Dimension();
         for (Component c : parent.getComponents())
         {
            //wide enough to stack the left and center components horizontally without overlap
            d.width += c.getPreferredSize().width;
            //tall enough to fit the tallest component
            d.height = Math.max(d.height, c.getPreferredSize().height);
         }
         return d;
      }

      @Override
      public Dimension minimumLayoutSize(Container parent)
      {
         return preferredLayoutSize(parent);
      }

      @Override
      public void layoutContainer(Container parent)
      {
         //in this method we will:
         //1) position the left component on the left edge of the parent and center it vertically
         //2) position the center component in the center of the parent (as long as it would not overlap
         //the left component) and center it vertically

         int leftComponentWidth = leftComponent.getPreferredSize().width;
         int leftComponentHeight = leftComponent.getPreferredSize().height;
         int centeredComponentWidth = centeredComponent.getPreferredSize().width;
         int centeredComponentHeight = centeredComponent.getPreferredSize().height;

         leftComponent.setBounds(0, (parent.getHeight() - leftComponentHeight) / 2, leftComponentWidth, leftComponentHeight);
         int leftComponentRightEdge = leftComponent.getX() + leftComponent.getWidth();
         int centerComponentLeftEdge = (parent.getWidth() - centeredComponentWidth) / 2;
         int centerComponentTopEdge = (parent.getHeight() - centeredComponentHeight) / 2;

         if (leftComponentRightEdge >= centerComponentLeftEdge)
         {
            //Center component will "do its best" to remain in the center
            //but it will not do so if it would cause it to overlap the left component
            centerComponentLeftEdge = leftComponentRightEdge;
         }

         centeredComponent.setBounds(centerComponentLeftEdge,
                                     centerComponentTopEdge,
                                     centeredComponentWidth,
                                     centeredComponentHeight);
      }

      @Override
      public void addLayoutComponent(Component comp, Object constraints)
      {
         if (LEFT.equals(constraints))
         {
            if (leftComponent != null)
            {
               throw new IllegalStateException("A left component has already been assigned to this layout.");
            }
            leftComponent = comp;
         }
         else if (CENTERED.equals(constraints))
         {
            if (centeredComponent != null)
            {
               throw new IllegalStateException("A centered component has already been assigned to this layout.");
            }
            centeredComponent = comp;
         }
         else
         {
            throw new IllegalStateException("Unexpected constraints '" + constraints + "'.");
         }
      }

      @Override
      public Dimension maximumLayoutSize(Container target)
      {
         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
      }

      @Override
      public float getLayoutAlignmentX(Container target)
      {
         return 0;
      }

      @Override
      public float getLayoutAlignmentY(Container target)
      {
         return 0;
      }

      @Override
      public void invalidateLayout(Container target)
      {

      }
   }
}
  • 整体容器使用BorderLayout
  • 将 A 添加到其 BorderLayout.LINE_START 位置。
  • 添加另一个 FlowLayout JPanel BorderLayout.CENTER。该面板将容纳 B.
  • 将B 添加到上面的JPanel。由于 FlowLayout 默认为 FlowLayout.CENTER,B 应在此 JPanel 中居中。

如果我没有正确理解您的需求,您希望 B 相对于整个父级居中,而不是在 A 定位后剩下的 space 中居中。这使这个问题变得有趣,并且在测试了其他建议的答案之后,我认为它们不能满足该要求。

我无法想出一种方法来组合内置布局管理器来实现这一点。所以,我破解了 LayoutManager2 的自定义实现。

以下可执行示例可能会满足您的需要。该实现快速而肮脏,绝不是一个好的通用布局管理器的示例,但它似乎满足您的要求并且表现得像您的图纸让我认为应该的那样。我将您的要求 "B does its best to stay in the middle of the panel" 解释为 B 应尽量相对于整个面板保持居中,但不以重叠 A 为代价。

package com.example;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager2;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example  {

    public Example() {

        JPanel a = new JPanel();
        a.setBackground(Color.RED);
        a.setPreferredSize(new Dimension(128, 128));

        JPanel b = new JPanel();
        b.setBackground(Color.BLUE);
        b.setPreferredSize(new Dimension(128, 128));

        JPanel panel = new JPanel(new Sly493LayoutManager());
        panel.add(a, Sly493LayoutManager.LEFT);
        panel.add(b, Sly493LayoutManager.CENTERED);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

    private static class Sly493LayoutManager implements LayoutManager2 {

        public static final Integer LEFT = 0;

        public static final Integer CENTERED = 1;

        private Component leftComponent;

        private Component centeredComponent;

        @Override
        public void addLayoutComponent(String name, Component comp) { }

        @Override
        public void removeLayoutComponent(Component comp) { 
            if (leftComponent == comp) {
                leftComponent = null;
            } else if (centeredComponent == comp) {
                centeredComponent = null;
            }           
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            Dimension d = new Dimension();
            for (Component c : parent.getComponents()) {
                //wide enough to stack the left and center components horizontally without overlap
                d.width += c.getPreferredSize().width;
                //tall enough to fit the tallest component
                d.height = Math.max(d.height, c.getPreferredSize().height);
            }
            return d;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return preferredLayoutSize(parent);
        }

        @Override
        public void layoutContainer(Container parent) {     
            //in this method we will:
            //1) position the left component on the left edge of the parent and center it vertically
            //2) position the center component in the center of the parent (as long as it would not overlap
            //the left component) and center it vertically

            int leftComponentWidth = leftComponent.getPreferredSize().width;
            int leftComponentHeight = leftComponent.getPreferredSize().height;
            int centeredComponentWidth = centeredComponent.getPreferredSize().width;
            int centeredComponentHeight = centeredComponent.getPreferredSize().height;

            leftComponent.setBounds(0, (parent.getHeight() - leftComponentHeight) / 2, leftComponentWidth, leftComponentHeight);
            int leftComponentRightEdge = leftComponent.getX() + leftComponent.getWidth();
            int centerComponentLeftEdge = (parent.getWidth() - centeredComponentWidth) / 2;
            int centerComponentTopEdge = (parent.getHeight() - centeredComponentHeight) / 2;        

            if (leftComponentRightEdge >= centerComponentLeftEdge) {
                //Center component will "do its best" to remain in the center
                //but it will not do so if it would cause it to overlap the left component
                centerComponentLeftEdge = leftComponentRightEdge;
            }

            centeredComponent.setBounds(centerComponentLeftEdge, 
                    centerComponentTopEdge, 
                    centeredComponentWidth, 
                    centeredComponentHeight);
        }

        @Override
        public void addLayoutComponent(Component comp, Object constraints) {
            if (LEFT.equals(constraints)) {
                if (leftComponent != null) {
                    throw new IllegalStateException("A left component has already been assigned to this layout.");
                }
                leftComponent = comp;
            } else if (CENTERED.equals(constraints)) {
                if (centeredComponent != null) {
                    throw new IllegalStateException("A centered component has already been assigned to this layout.");
                }
                centeredComponent = comp;
            } else {
                throw new IllegalStateException("Unexpected constraints '" + constraints + "'.");
            }
        }

        @Override
        public Dimension maximumLayoutSize(Container target) {
            return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }

        @Override
        public float getLayoutAlignmentX(Container target) {
            return 0;
        }

        @Override
        public float getLayoutAlignmentY(Container target) {
            return 0;
        }

        @Override
        public void invalidateLayout(Container target) {

        }       
    }
}

这里有一个非常快速和肮脏的方法,类似于 Firefly 的答案 - 只需创建一个具有 null Layout 的 JPanel,并将两个子面板放在它的 paintComponent 方法中:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Example extends JPanel {

   protected JPanel a;
   protected JPanel b;
   int              size = 200;

   public Example() {
      setLayout( null );

      a = new JPanel();
      a.setBackground( Color.red );
      a.setPreferredSize( new Dimension( size, size ) );

      b = new JPanel();
      b.setBackground( Color.blue );
      b.setPreferredSize( new Dimension( size, size ) );

      add( a );
      add( b );

      setPreferredSize( new Dimension( 4 * size, size ) );
   }

   @Override
   public void paintComponent( final Graphics g ) {
      super.paintComponent( g );
      a.setBounds( 0, 0, size, size );

      int w = getWidth();

      int x = (w - size) / 2;
      if ( x < size ) {
         x = size;
      }
      b.setBounds( x, 0, size, size );

   }

   public static void main( String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         @Override
         public void run() {
            // Create and set up the window.
            JFrame jf = new JFrame();
            jf.setName( "Example" );
            Example item = new Example();
            jf.add( item );
            // Display the window.
            jf.pack();
            jf.setVisible( true );
            jf.addWindowListener( new WindowAdapter() {
               @Override
               public void windowClosing( WindowEvent arg0 ) {
                  System.exit( 0 );
               }
            } );
         }
      } );
   }
}

出于实际目的,我不需要精确居中。我需要正确对齐。我需要两行。右边的文字比中间的文字短得多。所以诀窍是:

  1. 将两行放在单独的 JPanel 中
  2. 使用 GridBagLayout
  3. “居中”的项目确实是左边的项目,但它是 right-aligned。

来自真实应用程序的一些代码({} 块曾经在不同的功能中,但现在不重要):

    JPanel myPanel = new JPanel(new GridBagLayout());
    JLabel centerFirst = new JLabel("first line");
    JLabel rightFirst = new JLabel("...");
    {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        myPanel.add(centerFirst, gridBagConstraints);
    }
    {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5; // you don't really need 0.5 and 0.5, it may be 0.1 and 0.1, two equal values
        myPanel.add(rightFirst, gridBagConstraints);
    }
    JLabel centerSecond = new JLabel("second line");
    JLabel rightSecond = new JLabel("...");
    {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        myPanel.add(centerSecond, gridBagConstraints);
    }
    {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        myPanel.add(rightSecond, gridBagConstraints);
    }

    //...

    // the main frame uses GridBagLayout too
    JFrame frame = new JFrame();
    {
        GridBagLayout gbl = new GridBagLayout();
        //...
        frame.setLayout(gbl);
    }

    // myPanel: full width of the enclosing GridBagLayout
    {
        GridBagLayout gbl = (GridBagLayout) frame.getContentPane().getLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;       // it will occupy columns 0 and 1 of the GridBagLayout
        gbc.gridy = 4;       // and there will be stuff above it
        gbc.gridheight = 1;  // full width, therefore height=1 is enough
        gbc.gridwidth = 2;   // the number of columns in this GridBagLayout
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(myPanel, gbc);
        frame.add(myPanel);
    }

盒子布局

container.add(componentA)
container.add(Box.createHorisontalGlue())
container.add(componentB)
container.add(Box.createHorisontalGlue())