⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap12-1.txt

📁 JAVA 学习资源
💻 TXT
字号:
// 程序12-1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

     // 为了响应JButton事件处理,testEventButton类必须实现ActionListener接口
public class testEventButton implements ActionListener{
    JFrame  frame;
    Container  contentPane;
    JButton  button1,button2;
    JLabel  label;
   
           // 必须实现ActionListener接口中的方法actionPerformed( )
    public void actionPerformed(ActionEvent e){ 
        label.setText("You pressed Button: "+e.getActionCommand( ));
    }
    
    public testEventButton( ){  // 定义构造函数
        frame=new subJFrame("testEventButton");
        contentPane=frame.getContentPane( );          
        contentPane.setLayout(new BorderLayout(5,5));  // 设置内容格的布局
        
        label=new JLabel("  ");    // 生成一个没有显示文本的标签
        contentPane.add(label,BorderLayout.CENTER);     
        
             // 创建两个按钮对象
        button1=new JButton("A");
        button2=new JButton("B");
        contentPane.add(button1,BorderLayout.NORTH);     
        contentPane.add(button2,BorderLayout.SOUTH);     

            // 将当前对象作为button1和button2对象的监听者
        button1.addActionListener(this);
        button2.addActionListener(this);
        frame.setSize(200,150);
        frame.show( );
    } 
    
    public static void main(String args[ ]){
        testEventButton  obj=new testEventButton( );
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -