Which layout can do this?

前端 未结 6 1496
广开言路
广开言路 2020-12-28 15:26

I\'m trying to layout some JLabels in my application as shown in this example:

\"enter

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 16:09

    I am using Windows forms, as I don't have an Java tools installed, but the idea is the same, you will just have to imagine that you are adding the JLabel instead of Buttons and that this is a JFrame or JWindow instead of a .NET Form.

    The code should be something like this, if we assume a 800 x 800 pixel area to layout stuff on

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

提交回复
热议问题