免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1024 | 回复: 0
打印 上一主题 下一主题

Applet(三):按钮 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-03 18:04 |只看该作者 |倒序浏览

简单介绍一些基本的applet构件,包括:按钮
创建按钮 [color="#000000"]相当简单: 只需要调用带有label的[color="#000000"]Button[color="#000000"]构造器. 一般情况下,我们需要创建一个引用,以方便以后的使用. [color="#000000"]The [color="#000000"]Button[color="#000000"] is a component, like its own little window, that will automatically get repainted as part of an update. This means that you don’t explicitly paint a button or any other kind of control; you simply place them on the form and let them automatically take care of painting themselves. So to place a button on a form you override [color="#000000"]init( ) [color="#000000"]instead of overriding [color="#000000"]paint( )[color="#000000"]: [color="#990000"]//: Button1.java
[color="#009900"]// Putting buttons on an applet
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class Button1 [color="#0000ff"]extends Applet {
  Button
    b1 = [color="#0000ff"]new Button("Button 1"),
    b2 = [color="#0000ff"]new Button("Button 2");
  [color="#0000ff"]public [color="#0000ff"]void init() {
    add(b1);
    add(b2);
  }
} [color="#009900"]///:~ [color="#000000"]仅仅创建一个[color="#000000"]Button[color="#000000"]还不够.我们可以调用[color="#000000"]Applet的[color="#000000"]add( )[color="#000000"] 方法把一个按钮放到applet上. 这个看起来很简单,可是按钮的位置布局等等就需要额外考虑了[color="#000000"]. 捕获事件 [color="#000000"]You’ll notice that if you compile and run the applet above, nothing happens when you press the buttons. This is where you must step in and write some code to determine what will happen. The basis of event-driven programming, which comprises a lot of what a GUI is about, is tying events to code that responds to those events. [color="#000000"]After working your way this far through the book and grasping some of the fundamentals of object-oriented programming, you might think that of course there will be some sort of object-oriented approach to handling events. For example, you might have to inherit each button and override some “button pressed” method (this, it turns out, is too tedious and restrictive). You might also think there’s some master “event” class that contains a method for each event you want to respond to. [color="#000000"]Before objects, the typical approach to handling events was the “giant switch statement.” Each event would have a unique integer value and inside the master event handling method you’d write a [color="#000000"]switch[color="#000000"] on that value. [color="#000000"]Java 1.0中的AWT没有使用任何面向对象的方法. 也不使用[color="#000000"]switch[color="#000000"]语句依赖于数字型变量来处理事件.取而代之,需要使用一些列的if语句[color="#000000"].What you’re trying to do with the [color="#000000"]if[color="#000000"] statements is detect the object that was the [color="#000000"]target[color="#000000"] of the event. That is, if you click on a button, then that particular button is the target. Normally, that’s all you care about – if a button is the target of an event, then it was most certainly a mouse click and you can continue based on that assumption. However, events can contain other information as well. For example, if you want to find out the pixel location where a mouse click occurred so you can draw a line to that location, the [color="#000000"]Event[color="#000000"] object will contain the location. (You should also be aware that Java 1.0 components can be limited in the kinds of events they generate, while Java 1.1 and Swing/JFC components produce a full set of events.) [color="#000000"]Java 1.0中的AWT是在action( )方法中使用的[color="#000000"]if语句[color="#000000"]. 尽管Java 1.0事件处理在Java 1.1是不提倡用的,它在一些不支持Java1. 1的系统中简单的applet中还是得到了广泛的使用,因此我们大家还是习惯使用一些这样的方法比较好.[color="#000000"] [color="#000000"]action( )[color="#000000"] 有两个参数: 第一个是[color="#000000"]Event类型,包含了可以触发action()方法的所有信息[color="#000000"].比如说他可以是:a mouse click, a normal keyboard press or release, a special key press or release, the fact that the component got or lost the focus, mouse movements, or drags, etc. 第二个参数通常是事件的目标, which you’ll often ignore. The second argument is also encapsulated in the [color="#000000"]Event[color="#000000"] object 通常这个参数是多余的。[color="#000000"]action( ) 使用的参数位置相当固定[color="#000000"]: 当我们在form上放置一些控件,其中一些(buttons, check boxes, drop-down lists, menus)有一些标准处理动作, which causes the call to [color="#000000"]action( )[color="#000000"] with the appropriate [color="#000000"]Event[color="#000000"] object. For example, with a button the [color="#000000"]action( )[color="#000000"] method is called when the button is pressed and at no other time. Usually this is just fine, since that’s what you ordinarily look for with a button. 稍后的章节我们可以看到,通过调用handleEvent( )是可以处理一些其他类型的事件[color="#000000"]. [color="#000000"]在上一个例子加一些时间处理: [color="#990000"]//: Button2.java
[color="#009900"]// Capturing button presses
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class Button2 [color="#0000ff"]extends Applet {
  Button
    b1 = [color="#0000ff"]new Button("Button 1"),
    b2 = [color="#0000ff"]new Button("Button 2");
  [color="#0000ff"]public [color="#0000ff"]void init() {
    add(b1);
    add(b2);
  }
  [color="#0000ff"]public [color="#0000ff"]boolean action(Event evt, Object arg) {
    [color="#0000ff"]if(evt.target.equals(b1))
      getAppletContext().showStatus("Button 1");
    [color="#0000ff"]else [color="#0000ff"]if(evt.target.equals(b2))
      getAppletContext().showStatus("Button 2");
    [color="#009900"]// Let the base class handle it:
    [color="#0000ff"]else
      [color="#0000ff"]return [color="#0000ff"]super.action(evt, arg);
    [color="#0000ff"]return [color="#0000ff"]true; [color="#009900"]// We've handled it here
  }
} [color="#009900"]///:~ [color="#000000"]To see what the target is, ask the [color="#000000"]Event[color="#000000"] object what its [color="#000000"]target[color="#000000"] member is and then use the [color="#000000"]equals( )[color="#000000"] method to see if it matches the target object handle you’re interested in. When you’ve written handlers for all the objects you’re interested in you must call [color="#000000"]super.action(evt, arg) [color="#000000"]in the [color="#000000"]else[color="#000000"] statement at the end, as shown above. Remember from Chapter 7 (polymorphism) that your overridden method is called instead of the base class version. However, the base-class version contains code to handle all of the cases that you’re not interested in, and it won’t get called unless you call it explicitly. The return value indicates whether you’ve handled it or not, so if you do match an event you should return [color="#000000"]true[color="#000000"], otherwise return whatever the base-class [color="#000000"]event( )[color="#000000"] returns. [color="#000000"]For this example, the simplest action is to print what button is pressed. Some systems allow you to pop up a little window with a message in it, but applets discourage this. However, you can put a message at the bottom of the Web browser window on its [color="#000000"]status line [color="#000000"]by calling the [color="#000000"]Applet[color="#000000"] method [color="#000000"]getAppletContext( )[color="#000000"] to get access to the browser and then [color="#000000"]showStatus( )[color="#000000"] to put a string on the status line.
[56]
[color="#000000"] You can print out a complete description of an event the same way, with [color="#000000"]getAppletContext().showStatus(evt + "" ). [color="#000000"](The empty [color="#000000"]String[color="#000000"] forces the compiler to convert [color="#000000"]evt[color="#000000"] to a [color="#000000"]String.[color="#000000"]) Both of these reports are really useful only for testing and debugging since the browser might overwrite your message. [color="#000000"]Strange as it might seem, you can also match an event to the [color="#000000"]text[color="#000000"] that’s on a button through the second argument in [color="#000000"]event( )[color="#000000"]. Using this technique, the example above becomes: [color="#990000"]//: Button3.java
[color="#009900"]// Matching events on button text
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class Button3 [color="#0000ff"]extends Applet {
  Button
    b1 = [color="#0000ff"]new Button("Button 1"),
    b2 = [color="#0000ff"]new Button("Button 2");
  [color="#0000ff"]public [color="#0000ff"]void init() {
    add(b1);
    add(b2);
  }
  [color="#0000ff"]public [color="#0000ff"]boolean action (Event evt, Object arg) {
    [color="#0000ff"]if(arg.equals("Button 1"))
      getAppletContext().showStatus("Button 1");
    [color="#0000ff"]else [color="#0000ff"]if(arg.equals("Button 2"))
      getAppletContext().showStatus("Button 2");
    [color="#009900"]// Let the base class handle it:
    [color="#0000ff"]else
      [color="#0000ff"]return [color="#0000ff"]super.action(evt, arg);
    [color="#0000ff"]return [color="#0000ff"]true; [color="#009900"]// We've handled it here
  }
} [color="#009900"]///:~ [color="#000000"]It’s difficult to know exactly what the [color="#000000"]equals( )[color="#000000"] method is doing here. The biggest problem with this approach is that most new Java programmers who start with this technique spend at least one frustrating session discovering that they’ve gotten the capitalization or spelling wrong when comparing to the text on a button. (I had this experience.) 并且,如果改变了按钮的名称,代码将不能正常工作(尽管不会出现任何编译错误或者运行时刻错误提示). 如果可能,请尽量不要使用这种方法.

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8682/showart_39189.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP