附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:
//增加两个控件
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);
}
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]
}
}
//完成运行,结果如下: