JLabel和JButton--事件响应原理示例

 
/**
 * @(#)c10_1.java
 *
 *
 * 
@author 
 * 
@version 1.00 2008/4/8
 
*/


import  java.awt. * ;
import  javax.swing. * ;
import  java.awt.event. * ;

public   class  c10_1  extends  JApplet  implements  ActionListener  {
    Container cp 
= getContentPane();
    Icon pa
= new ImageIcon("g:/JavaDocs/a1.gif");
    Icon pb
= new ImageIcon("g:/JavaDocs/a2.gif");
    Icon pc
= new ImageIcon("g:/JavaDocs/a3.gif");
    JButton bt
=new JButton();
    Icon pd
=new ImageIcon("g:/JavaDocs/a4.gif");
    JLabel lb
=new JLabel("HELLO",pd,JLabel.CENTER);

    
    
/** Initialization method that will be called after the applet is loaded
     *  into the browser.
     
*/

    
public void init() {
        
// TODO start asynchronous download of heavy resources
        bt.setRolloverEnabled(true);
        bt.setText(
"Click");
        bt.setHorizontalTextPosition(JButton.CENTER);
        bt.setVerticalTextPosition(JButton.BOTTOM);
        lb.setHorizontalTextPosition(JLabel.CENTER);
        lb.setVerticalTextPosition(JLabel.BOTTOM);
        cp.add(lb,BorderLayout.NORTH);
        cp.add(bt,BorderLayout.SOUTH);
        bt.setIcon(pa);
        bt.setRolloverIcon(pb);
        bt.setPressedIcon(pc);
        bt.addActionListener(
this);
    }


    
public void actionPerformed(ActionEvent e){
       
if (e.getSource()==bt)
       
if (lb.getText()=="HELLO")
              lb.setText(
"你好");
              
else 
              lb.setText(
"HELLO");
              
if(bt.getText()=="Click")
                   bt.setText(
"点击");
                       
else
                           bt.setText(
"Click");}

            
       }

        
    }

关于ImageIcon(string filename)...貌似要指定绝对路径。。因为我开始没指定绝对路径就算把图片文件和.class放在同一个文件夹也无法正常显示。。

 支持的图片有GIF.JPG.JPEG还有从1.3版本开始支持PNG等。不支持ico。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值