juce教程003

附JUCE4.1下载地址:http://www.juce.com

                                         http://pan.baidu.com/s/1o74IDHC


第三篇文章主要讲解第二篇的内容,因为第2编的内容是以自动化的菜鸟式的方式生成的代码,有些重要的东西还是要提一提,因此本章将以手动写代码的方式实现:

而且这篇文章,也是官方提供的教程之一,大家可以去官网察看,本人在此翻译核心内容。

主要的核心内容是JUCE的观察者模式,先来个总结:



实现步骤如下:

1,按第2篇的教程新建一个默认的Helloworld程序。或者接着上一篇文章的完成的工程也可以继续。

2,(此步如果是接上一篇文章的工程,否则忽略)用visual studio打开工程,main.cpp中的MainWindow构造函数改一下代码:

            setContentOwned (new MainContentComponent(), true);//改回默认的MainContentComponent组件调用。
    //setContentOwned(new NewComponent(), true);

3,visual studio打开工程后,进入MainContentComponent.h文件,增加继承ButtonListener类,红色为增加的部分:

class MainContentComponent   : public Component ,public ButtonListener
{
public:
    MainContentComponent();
    ~MainContentComponent();


    void paint (Graphics&);
    void resized();
void buttonClicked(Button* button);//声明回调函数


private:

//增加两个控件

TextButton checkTheTimeButton;  
Label timeLabel;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};


4,进入MainContentComponent.cpp文件,如下修改:

MainContentComponent::MainContentComponent()
{
addAndMakeVisible(checkTheTimeButton);    //为了button可见,这是必须的
checkTheTimeButton.setButtonText("check the time...");       //button的显示字符
checkTheTimeButton.setBounds(0, 0, 600, 50);              //button的大小
checkTheTimeButton.addListener(this);    //button增加听众this


addAndMakeVisible(timeLabel);
timeLabel.setColour(Label::backgroundColourId, Colours::black);
timeLabel.setColour(Label::textColourId, Colours::white);
timeLabel.setJustificationType(Justification::horizontallyCentred);

timeLabel.setBounds(0, 55, 600, 50);


setSize(600, 110);    //component的总的大小。
}




void MainContentComponent::buttonClicked(Button* button)                  //回调函数定义
{
if (button == &checkTheTimeButton)
{
const Time currentTime(Time::getCurrentTime()); // [4]
const bool includeDate = true;
const bool includeTime = true;
const String currentTimeString(currentTime.toString(includeDate, includeTime)); // [5]
timeLabel.setText(currentTimeString, dontSendNotification); // [6]


}
}


//完成运行,结果如下:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值