基于组件的组合物

时间:2009-08-13 07:51:57

标签: java swing

有人可以协助我实施基于组件的项目吗?我设计了两个组件,即计算器和引擎(下面的源代码),它们需要位于任何摆动组件内(JListJTree或任何)。然后应该有能力将两个组件(计算器或引擎)中的任何一个拖动到编辑器窗格中,以便使用连接器进行合成(下面给出代码)。如果合成是正确的,让复合返回到拖动初始组件的调色板。

组件:

public class Engine {

    private String name = "";
    private boolean running = false;
    private int speed;

    public Engine(String name) {
        this.name = name;
    }

    public void start() {
        if (!running) {
            running = true;
            System.out.println("Engine starts.");
        }
        else
            System.out.println("Engine already starts.");
    }

    public void stop() {
        if (running) {
            running = false;
            speed = 0;
            System.out.println("Engine stops.");
        }
        else
            System.out.println("Engine already stops.");
    }

    public void setSpeed(int speed) {
        if (speed >=0)
            this.speed = speed;
    }
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

public class Calc {

    public Calc() {

        }

    public Double squareRoot(Double a_double){
        return Math.sqrt(a_double);
    }

    // pre an_int >= 0
    public synchronized Integer factorial(Integer an_int){
        int fact=1;
        for(int i=1;i<=an_int;i++){
            fact *= i;
        }
        return fact;
    }

    /**
     * 
     * @param an_int
     * @return the squared root of the input integer 
     */
    public Integer getSquareRoot(Integer an_int){
        return (int) Math.sqrt(an_int);
    }


    public Double getSquareRoot(Double aDouble){
        return Math.sqrt(aDouble);
    }

    /**
     * Converts an Integer object into a Double object. 
     * 
     * @param an_int The Integer to be converted.
     * @return The Double object representing the same value.
     *  
     */
    public Double integerToDouble(Integer an_int){
        return new Double(an_int.intValue());
    }

    /**
     * Converts a Double object into an Integer object. Decimal digits are
     * truncated. Useful when passing the output of a method as the input to another.
     * 
     * @param a_double The Double to be converted.
     * @return The Integer object representing the same value.
     *  
     */
    public Integer doubleToInteger(Double a_double){
        return new Integer(a_double.intValue());
    }


    public Integer sum(Integer a, Integer b) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return a+b;
    }

    /**
     * Returns a string representation of the calculator.
     */
    public String toString(){
        return("Calculator computation unit:\n"+super.toString());
    }


    void doNothing(){

    }}

/////////////////////////////////////////////// //////////////////////////////////////////

2 个答案:

答案 0 :(得分:0)

你最好的选择可能是利用一个现有的GUI编辑器,据说Netbeans中的编辑器很好。

然后,您需要将组件添加到组件面板中。有关详细信息,请查看Netbeans在线帮助。

答案 1 :(得分:0)

从NetBeans开始,take a look at this answer,我详细介绍了如何在NetBeans中创建组件。