【问题标题】:Component based composition基于组件的组合
【发布时间】:2009-08-13 07:51:57
【问题描述】:

谁能帮助我实现一个基于组件的项目?我设计了两个组件,即计算器和引擎(下面的源代码),它们需要位于任何摆动组件(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;
    }
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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(){

    }}

/////////////////////////////////////// ////////////////////////////////////////////p>

【问题讨论】:

  • 你为什么使用盒装数字而不是原语?

标签: java swing


【解决方案1】:

您最好的选择可能是利用现有的 GUI 编辑器,据传 Netbeans 中的那个很好。

然后您需要将您的组件添加到组件面板。查看 Netbeans 联机帮助以获取更多信息。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多