【问题标题】:java Swing: how to keep one component left aligned with another component centered with respect to parentjava Swing:如何使一个组件与另一个相对于父级居中的组件左对齐
【发布时间】:2015-09-08 00:29:19
【问题描述】:

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

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

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

编辑:

Firefly 的答案是正确的(如标记),但我创建了一个 SSCCE,显示了我最初的问题(第一行)、Hovercraft Full Of Eels 的尝试解决方案(第二行)和 Firefly 的正确解决方案(第三行)。我认为发布它不会有什么坏处:

package stackoverflow;

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 StackOverflowTest extends JFrame
{

   public StackOverflowTest()
   {
      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 StackOverflowTest().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)
      {

      }
   }
}

【问题讨论】:

  • 使用两种独立的布局,一种用于A,一种用于B,并将它们放入您的JPanel
  • @MuratK.: A 和 B 是添加到容器中的组件,因此更改 A 和 B 的布局将没有任何用处。您必须更改包含 组件容器 的布局,而不是组件的布局。

标签: java swing layout jpanel layout-manager


【解决方案1】:

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

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

以下可执行示例可能满足您的需求。该实现快速而肮脏,绝不是一个好的通用布局管理器的示例,但它似乎满足您的要求并且表现得就像您的图纸让我认为应该的那样。我将您的“B 尽最大努力保持在面板中间”的要求解释为意味着 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) {

        }       
    }
}

【讨论】:

  • 很遗憾,没有自定义布局管理器就无法完成,但这是完美的。
  • 很高兴它对您有用。同意悲伤...自定义布局管理器很少是最佳选择。很想看到基于内置管理器的实现,但我什么都没有:(
【解决方案2】:

这是一个非常快速和肮脏的方式,类似于 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 );
               }
            } );
         }
      } );
   }
}

【讨论】:

    【解决方案3】:
    • 让整个容器使用 BorderLayout
    • 将 A 添加到其 BorderLayout.LINE_START 位置。
    • 添加另一个 FlowLayout JPanel BorderLayout.CENTER。此面板将举行 B。
    • 将 B 添加到上面的 JPanel。由于 FlowLayout 默认为 FlowLayout.CENTER,所以 B 应该在这个 JPanel 中居中。

    【讨论】:

    • 这不起作用。相反,结果与我目前使用 GridBagLayout 的结果相同。详细说明:这导致 B 相对于 FlowLayout 面板居中。我希望 B 尝试相对于原始面板保持居中(按照您的指示,这将是 BorderLayout 面板)。当我说“尝试保持居中”时,我的意思是 B 将尽可能居中(相对于整个容器),但不会与 A 重叠。
    • 这就是我试图在我的图片中展示的内容:当面板很小时,A 和 B 触摸并且 B 没有居中,因为左对齐的 A 占用了 B 所需的空间要居中,所以 B 只是尽可能靠近中心,这意味着接触 A。但是,随着面板变大,A 会移出中心(b/c 它是左对齐),并且 B 相对于中心变得居中到整个面板。
    【解决方案4】:

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

    1. 将两行放在单独的 JPanel 中
    2. 使用 GridBagLayout
    3. “居中”项实际上是左侧项,但它是右对齐的。

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

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

    【讨论】:

      【解决方案5】:

      盒子布局

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        • 2020-07-06
        • 2012-12-16
        • 2012-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多