日期:2014-05-20 浏览次数:20621 次
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author xtass
*/
public class CircleLayoutTest {
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
CircleLayoutFrame frame=new CircleLayoutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* A frame that shows buttons arranged along a circle
*/
class CircleLayoutFrame extends JFrame{
public CircleLayoutFrame(){
setTitle("CircleLayoutTest");
setLayout(new CircleLayout());
add(new JButton("Yellow"));
add(new JButton("Blue"));
add(new JButton("Red"));
add(new JButton("Green"));
add(new JButton("Orange"));
add(new JButton("Fuchsia"));
add(new JButton("Indigo"));
pack();
}
/**
* A layout manager that lays out components along a circle
*/
class CircleLayout implements LayoutManager
{
public void addLayoutComponents(String name,Component comp){
}
public void removeLayoutComponents(Component comp){
}
public void setSizes(Container parent){
if(sizesSet) return;
int n=parent.getComponentCount();
preferredWidth=0;
preferredHeight=0;
minWidth=0;
minHeight=0;
maxComponentWidth=0;
maxComponentHeight=0;
//compute the maxium component widths and heights
//and set the preferred size to the sum of the component sizes.
for(int i=0;i<n;i++){
Component c=parent.getComponent(i);
if(c.isVisible()){
Dimension d=c.getPreferredSize();
maxComponentWidth=Math.max(maxComponentWidth,d.width);
maxComponentHeight=Math.max(maxComponentHeight,d.height);
preferredWidth+=d.width;
preferredHeight+=d.height;
}
}
minWidth=preferredWidth/2;
minHeight=preferredHeight/2;
sizesSet=true;
}
public Dimension preferredLayoutSize(Container parent){
setSizes(parent);
Insets insets=parent.getInsets();
int width=preferredWidth+insets.left+insets.right;
int height=preferredHeight+insets.top+insets.bottom;