j_button.java.bak

来自「一个十分好的java基础学习的课件」· BAK 代码 · 共 50 行

BAK
50
字号
// ////////////////////////////////////////////////////////
// 
// J_Button.java
// 
// Created by Jun-Hai Yong,    on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
//      Example of event-handling model.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
//     Jun-Hai Yong. Programming in Java. 
//     Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
//     雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no 
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////

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

public class J_Button
{
    public static void main( String args[] )
    { 
        JFrame app = new JFrame( "Example of event-handling" );

        Container cp = app.getContentPane( );
        JButton   b= new JButton("Pressed 0 times!");
        cp.setLayout( new BorderLayout( ) );
        cp.add(b, BorderLayout.CENTER);
        b.addActionListener( new ActionListener( )
            {
                int m_count = 0;
                public void actionPerformed(ActionEvent e)
                {
                    JButton b= (JButton)e.getSource( );
                    b.setText("Pressed " + (++m_count) + " times");
                } // End of method: actionPerformed
            } // End of the anonymous inner class
        ); // End of invoking addActionListener

        app.setSize( 100, 80 );
        app.setVisible( true );
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    } // End of method: main

} // End of class: J_Button

⌨️ 快捷键说明

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