Topic: 请问有关于监听器的典型示范代码吗?

  Print this page

1.请问有关于监听器的典型示范代码吗? Copy to clipboard
Posted by: yalanda2004
Posted on: 2005-03-04 11:53

想为一对象增加监听器,这样任何对该对象的改变都被获取。

2.Re:请问有关于监听器的典型示范代码吗? [Re: yalanda2004] Copy to clipboard
Posted by: li_2001_01
Posted on: 2005-03-11 16:22

// ta.java: Create a Close button in the frame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//ActionListener 是 ActionEvent事件的监听器接口 ,implements 是引入接口的关键字
public class ta extends JFrame implements ActionListener
{
// Create an object for "Close" button
private JButton jbtOk = new JButton("确定");
private JButton jbtCancel = new JButton("取消");

// Default constructor
public ta()
{
// Set the window title
setTitle("事件处理");

// Set FlowLayout manager to arrange the components
// inside the frame
getContentPane().setLayout(new FlowLayout());

// Add buttons to the frame
getContentPane().add(jbtOk);
getContentPane().add(jbtCancel);

// Register listeners
jbtOk.addActionListener(this);
jbtCancel.addActionListener(this);
}

// Main method
public static void main(String[] args)
{
ta frame1 = new ta();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(100, 80);
frame1.setVisible(true);
}

// This method will be invoked when a button is clicked.
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtOk)
{
System.out.println("The OK button is clicked");
}
else if (e.getSource() == jbtCancel)
{
System.out.println("The Cancel button is clicked");
}
}
}

3.Re:请问有关于监听器的典型示范代码吗? [Re: yalanda2004] Copy to clipboard
Posted by: li_2001_01
Posted on: 2005-03-11 16:23

在JDK5通过。。。。。。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923