把按钮横排,竖排好实现。
如果把按钮排列成圆呢。。没啥好思路。

解决方案 »

  1.   

    能给点实例代码吗?对SWT不是很熟..谢谢!
      

  2.   

    import java.awt.geom.Point2D;public class GeometryUtil {
        // 绕原点的旋转矩阵,绕任意点旋转,可以先移动到原点,旋转,然后再移回去
        // cosθ -sinθ 0
        // sinθ +conθ 0
        // 0000 +0000 1
        // x = r*cosα, y = r*sinα
        // x' = r*cos(α+θ) = r*cosα*cosθ - r*sinα*sinθ = x*cosθ - y*sinθ
        // y' = r*sin(α+θ) = r*sinα*cosθ + r*cosα*sinθ = x*sinθ + y*cosθ
        // (x, y)绕圆心旋转degree度
        public static Point2D rotate(double x, double y, double degree) {
            return rotate(x, y, 0, 0, degree);
        }    public static Point2D rotate(Point2D p, double degree) {
            return rotate(p.getX(), p.getY(), 0, 0, degree);
        }    public static Point2D rotate(Point2D p, Point2D op, double degree) {
            return rotate(p.getX(), p.getY(), op.getX(), op.getY(), degree);
        }    // (x, y)绕(ox, oy)旋转degree度
        public static Point2D rotate(double x, double y, double ox, double oy, double degree) {
            x -= ox;
            y -= oy;        double cos = Math.cos(Math.toRadians(degree));
            double sin = Math.sin(Math.toRadians(degree));        double temp = x * cos - y * sin;
            y = x * sin + y * cos;
            x = temp;        return new Point2D.Double(x + ox, y + oy);
        }
    }
    import java.awt.geom.Point2D;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;@SuppressWarnings("serial")
    public class CircleButtons extends JPanel {
        
        public CircleButtons() {
            JButton[] buttons = new JButton[10];
            int radius = 100;
            int cx = 150;
            int cy = 150;
            int deltaAngle = 360 / buttons.length;
            
            setLayout(null);
            for (int i = 0; i < buttons.length; ++i) {
                buttons[i] = new JButton("" + i);
                Point2D p = GeometryUtil.rotate(cx + radius, cy, cx, cy, i * deltaAngle); // 计算按钮在圆周上的坐标
                buttons[i].setBounds((int)p.getX(), (int)p.getY(), 50, 50);
                
                add(buttons[i]);
            }
        }    private static void createGuiAndShow() {
            JFrame frame = new JFrame("");
            frame.getContentPane().add(new CircleButtons());        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setAlwaysOnTop(true);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }    public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGuiAndShow();
                }
            });
        }
    }
      

  3.   

    完全看不懂,数学没学好,什么sin cos 的,唉,看来还得弄明白算法 和 公式
      

  4.   

    还有我在我的eclipse上运行了一下,有个错误,就主方法里面的run()方法错误,为什么呢?