哪种布局可以做到这一点?

22

我正在尝试在我的应用程序中布置一些JLabel,如此示例所示:

enter image description here

我总是希望这个JLabel位于中间,并且其他JLabel的数量是可变的,可以从1到30。我尝试了使用网格布局,选择了一定数量的列/行并在空白处设置了一些空JLabel,但是我无法得到好的结果,并且找不到如何使用MigLayout完成它,是否有人有好的布局解决方案或任何其他解决方案。

PS:我不想显示圆圈,只是为了显示JLabel是按照圆形排列的。


好问题,而且图片描述得非常清楚(并且字节数小)。+1 - Andrew Thompson
1
谢谢,我觉得在这里连续30天后,我开始有点适应了 :) - imanis_tn
6个回答

19

您不需要专门支持此功能的布局管理器。您可以使用一些相当简单的三角函数计算x、y位置,然后使用常规布局,如SpringLayout

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class CircleLayout {

  /**
   * Calculate x,y positions of n labels positioned in
   * a circle around a central point. Assumes AWT coordinate
   * system where origin (0,0) is top left.
   * @param args
   */
  public static void main(String[] args) {
    int n = 6;  //Number of labels
    int radius = 100;
    Point centre = new Point(200,200);

    double angle = Math.toRadians(360/n);
    List<Point> points = new ArrayList<Point>();
    points.add(centre);

    //Add points
    for (int i=0; i<n; i++) {
      double theta = i*angle;
      int dx = (int)(radius * Math.sin(theta));
      int dy = (int)(-radius * Math.cos(theta));
      Point p = new Point(centre.x + dx, centre.y + dy);
      points.add(p);
    }

    draw(points);
  }

  private static void draw(List<Point> points) {
    JFrame frame = new JFrame("Labels in a circle");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();;
    SpringLayout layout = new SpringLayout();

    int count = 0;
    for (Point point : points) {
      JLabel label = new JLabel("Point " + count);
      panel.add(label);
      count++;
      layout.putConstraint(SpringLayout.WEST, label, point.x, SpringLayout.WEST, panel);
      layout.putConstraint(SpringLayout.NORTH, label, point.y, SpringLayout.NORTH, panel);
    }

    panel.setLayout(layout);

    frame.add(panel);
    frame.setVisible(true);

  }
}

数学 截图


14

我认为你做得很好。+1 - Andrew Thompson
在开始创建自定义布局管理器之前,请确保没有现有的布局管理器符合您的要求。 - imanis_tn
创建自己的布局管理器可能看起来很具有挑战性,但实际上它相当简单! - Jakub Zaverka

13

JH实验室有一个ClockLayout

这是一个为特定目的创建的非常愚蠢的布局。它只是按顺时针方向从顶部开始将其组件排列在一个圆圈中。


布局很棒,但是这里没有中心元素,所以我必须实现我的自定义时钟布局。+1 - imanis_tn

5

我喜欢@Baqueta和@sacha的想法:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CircleLayoutTest {
  public JComponent makeUI() {
    JPanel panel = new JPanel() {
      @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Insets i = getInsets();
        g.translate(i.left, i.top);
        g.setColor(Color.RED);
        int w = getWidth() - i.left - i.right;
        int h = getHeight() - i.top - i.bottom;
        g.drawOval(0, 0, w, h);
        g.translate(-i.left, -i.top);
      }
    };
    panel.setLayout(new FlowLayout() {
      @Override public void layoutContainer(Container target) {
        synchronized(target.getTreeLock()) {
          int nmembers  = target.getComponentCount();
          if(nmembers<=0) return;
          Insets i = target.getInsets();
          double cx = .5 * target.getWidth();
          double cy = .5 * target.getHeight();
          Component m = target.getComponent(0);
          Dimension d = m.getPreferredSize();
          m.setSize(d.width, d.height);
          m.setLocation((int)(cx+.5-.5*d.width),(int)(cy+.5-.5*d.height));
          if(nmembers-1<=0) return;
          double rw = .5 * (target.getWidth() - i.left - i.right);
          double rh = .5 * (target.getHeight() - i.top - i.bottom);
          double x = 0, y = 0, r = 0;
          double radian = 2.0 * Math.PI / (nmembers-1);
          for(int j=1; j<nmembers; j++) {
            m = target.getComponent(j);
            if(m.isVisible()) {
              d = m.getPreferredSize();
              m.setSize(d.width, d.height);
              x = cx + rw * Math.cos(r) - .5 * d.width;
              y = cy + rh * Math.sin(r) - .5 * d.height;
              m.setLocation((int)(x+.5), (int)(y+.5));
              r += radian;
            }
          }
        }
      }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(initPanel(panel));
    return p;
  }
  private static JComponent initPanel(JComponent p) {
    p.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
    for(int i=0; i<6; i++) {
      p.add(new JLabel("No."+i));
    }
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new CircleLayoutTest().makeUI());
    f.setSize(320 ,320);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

我喜欢你的大部分代码,加一分。很棒,只有你的代码可以在所有方向上调整大小,非常感谢。 - mKorbel
我喜欢它平滑地调整大小的事实。+1 - Andrew Thompson

3

我正在使用Windows表单,因为我没有Java工具安装,但是思路是相同的,你只需要想象你添加的是JLabel而不是按钮,并且这是一个JFrame或JWindow而不是.NET表格。

如果我们假设有一个800 x 800像素的区域来布置东西,那么代码应该是这样的:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    void Form1_Load(object sender, EventArgs e)
    {
        int numberItems = 18;
        int centreX = 400;
        int centreY = 400;


        double offset = Math.PI;
        double step = Math.PI * 2 / numberItems;


        Button b = null;
        for (int i = 0; i < numberItems; i++)
        {
            b = new Button();
            b.Width = 30;
            b.Height = 30;
            SetPosition(b, 370, offset, i, step);
            this.Controls.Add(b);
        }

        b = new Button();
        b.Width = 30;
        b.Height = 30;
        b.Location = new Point(centreX, centreY);
        this.Controls.Add(b);
    }


    private void SetPosition(Button button, int legLength, double offset, double posOffSet, double step)
    {

        int x = (int)(legLength + Math.Sin(offset + posOffSet * step) * legLength);
        int y = (int)(legLength + Math.Cos(offset + posOffSet * step) * legLength);

        button.Location = new Point(x, y);
    }
}

谢谢,但我想要一个使用布局的解决方案,因为不建议使用绝对定位。 - imanis_tn
哦,好的,那么你不能创建自己的布局管理器吗?它可以根据面板的实际宽度计算出点数。只需要一个因子类型的东西。 - sacha barber

2
  1. MigLayout可以通过“pos x y [x2] [y2]”作为组件约束来进行绝对定位。 MigLayout确实是管理所有布局的布局管理器。 在其主页上查看WebStart演示,它很好地展示了绝对定位。 您仍然需要像使用自定义布局管理器一样计算组件的位置。

  2. 此外,您可以只是关闭布局

  3. 如果您想变得更有创意,可以看看JHotDraw


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接