Login: password:
Forgot your password?
ButtonManager > ButtonPanel > in depth
ButtonPanel
package buttonmanager;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JPanel;

class ButtonPanel extends JPanel{
    LinkedList buttonList;
    int nextButtonLabel=1;
    final int BUTTONWIDTH=60;
    final int BUTTONHEIGHT=20;
    Random rg=new Random();
    public ButtonPanel() {
        this.setLayout(null);
        this.setBackground(Color.GREEN);
        buttonList=new LinkedList();
    }
    private Point generateRandomPoint(){
        int x=rg.nextInt(this.getWidth()-BUTTONWIDTH);
        int y=rg.nextInt(this.getHeight()-BUTTONHEIGHT);
        return new Point(x,y);
    }
    public void addButton(){
        JButton b=new JButton(" "+(nextButtonLabel++)+" ");
        b.setLocation(generateRandomPoint());
        b.setSize(BUTTONWIDTH,BUTTONHEIGHT);
        this.add(b);
        this.repaint();
        buttonList.add(b);
    }
    private Component selectElement(){
        int numElem=buttonList.size();
        if (numElem>0) {
            Component o=(Component)buttonList.get(rg.nextInt(numElem));
            return o;
        } else return null;
    }
    public void removeButton(){
        Component c=selectElement();
        if (c!=null) {
            buttonList.remove(c);
            this.remove((Component)c);
            this.repaint();
        }
    }
    void moveButton() {
        Component c=selectElement();
        if (c!=null) {
            Point p=generateRandomPoint();
            c.setLocation(p);
            this.repaint();
        }
    }

}

ButtonManager > in depth


powered by segue
segue_logo