在加入这一行以后,这个程序就已经完成了,下面的工作便是编译与运行了,选择"Build"菜单中的"Build"或者"Rebuild"选项,这就是编译了,编译后,如果有错误,会在最下面的浮动窗口"Task List"中提示你,如果没有错误,在状态栏上会显示出"Solution update successed "表示已经编译成功,然后选择"Debug"菜单中的"Start"或者按下F5键,运行,这时,会弹出一个Dos窗口,显示出这个程序的结果:"Hello!"。其实,你也可以将编译与运行看结果合成一个动作,那就是直接运行,如果没有编译的化,它会自动对你的程序进行编译。工具栏上有快捷图标" ",用鼠标一点。就会自动编译并运行了。运行结果如下:
虽然我对大家建议不要用WFC,但这里还是给大家举一个用WFC的例子,在前面的图中,我们选择第一个选项"Windows Application",填好名字与路径之后,应该会出现如下所示的窗口界面。
这个时候,如果你运行的化,会发现它有一个基本的窗口,能最大化,最小化,以及关闭程序等功能。
如果你仔细看看属性浮动窗口,会发现和VB一样的许多窗口属性,我在这里就不一一说明它的作用了,你自己按着属性窗口中的内容一项项自己调整或者改变,然后看看你的界面变了些什么,多用几遍后,就知道这个属性窗口是干什么用的了。
现在给大家举一个简单的例子,两个文本输入框,一个按钮,你能够在第一个文本框中输入内容,点击按钮后,第二个文本框中的内容将会与第一个文本框中的内容一模一样,也就是复制第一个文本框的内容。这个例子做起来,很简单,步骤如下:
1. 拖动工具栏上的文本框控件到你的Form界面上,拖两个,那么系统会自动将第一个命名为"Edit1"和"Edit2",如下图:
2. 将第二个文本框选中,然后在属性窗口中选择"enabled"选择项,然后将后面的选择项的内容由"true"改为"false"使之不可以被编辑,也就是不能输入文字的意思。如下图:
3. 增加一个按钮,由工具箱上直接拖动到"Form"中就可以了。按钮名字自己随便了。如下图:
4. 双击按钮,将会打开代码编辑器,并且显示如下的内容:
private void button1_click(Object source, Event e)
{
}
在这个按钮事件代码中增加一行,如下:
edit2.setText(edit1.getText());
好了,这个简单程序也已经编写完了,试着点一下运行图标看看,就会看到你自己想要的界面与输出结果了。程序的完整代码如下:
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
/**
* This class can take a variable number of parameters on the command
* line. Program execution begins with the main() method. The class
* constructor is not invoked unless an object of type 'Form1' is
* created in the main() method.
*/
public class Form1 extends Form
{
public Form1()
{
// Required for Visual J++ Form Designer support
initForm();
// TODO: Add any constructor code after initForm call
}
/**
* Form1 overrides dispose so it can clean up the
* component list.
*/
public void dispose()
{
super.dispose();
components.dispose();
}
private void button1_click(Object source, Event e)
{
edit2.setText(edit1.getText());
}
/**
* NOTE: The following code is required by the Visual J++ form
* designer. It can be modified using the form editor. Do not
* modify it using the code editor.
*/
Container components = new Container();
Edit edit1 = new Edit();
Edit edit2 = new Edit();
Button button1 = new Button();
private void initForm()
{
this.setText("Form1");
this.setAutoScaleBaseSize(new Point(6, 12));
this.setClientSize(new Point(192, 165));
edit1.setLocation(new Point(48, 16));
edit1.setSize(new Point(100, 19));
edit1.setTabIndex(0);
edit1.setText("edit1");
edit2.setEnabled(false);
edit2.setLocation(new Point(48, 56));
edit2.setSize(new Point(100, 19));
edit2.setTabIndex(1);
edit2.setText("edit2");
button1.setLocation(new Point(64, 120));
button1.setSize(new Point(75, 23));
button1.setTabIndex(2);
button1.setText("button1");
button1.addOnClick(new EventHandler(this.button1_click));
this.setNewControls(new Control[] {
button1,
edit2,
edit1});
}
/**
* The main entry point for the application.
*
* @param args Array of parameters passed to the application
* via the command line.
*/
public static void main(String args[])
{
Application.run(new Form1());
}
}
至于其他的程序,也就是不用WFC,而又要编写有界面的程序,也就是用AWT写界面,那就没有办法了,只能自己写AWT代码了。基本过程为:
1. 选择"New"选项中的"Visual J++ Projects"选项中的"Empty Projects"。选择好名字与路径之后,系统就会帮你建立一个新的工程了。但是这个里面什么文件也没有。
2. 这个工程是空的,这时候,你可以导入代码文件也可以自己建立代码文件。建立类或者"Form"之类的文件,请选择"Project"菜单中的"Add Item"选项,建立自己想要的文件就可以了,代码嘛,当然只能自己手工输入了,没有别的办法。不过,这里倒是什么文件都可以建立,包括Java Applet在内。因此,如果你的工程不止一个文件或者比较大的话,是肯定要用到这个选择项的!这是一个必须学会的用法了。
|