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

📄 jbuttondemo.java

📁 精通Java核心技术源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JButtonDemo extends JFrame {
	    private JButton button1,button2,button3;

	    public JButtonDemo() 
	    {
	        super( "JButtonDemo" );
		
	        // 获取content pane并设置布局管理器
	        Container container = getContentPane();
		   container.setLayout( new FlowLayout() );
		      
	        // 创建按钮
	        button1 = new JButton( "plain button" );
	        container.add( button1 );
	        
	        Icon icon = new ImageIcon("icon.gif");
	        button2 = new JButton( icon );
	        container.add( button2 );
        
	        button3 = new JButton( "button with icon and text",icon );
	        container.add( button3 );
        
	        // 注册事件处理器
	        ButtonHandler handler = new ButtonHandler();
	        button1.addActionListener( handler );
	        button2.addActionListener( handler );
	        button3.addActionListener( handler );
        
	        setSize( 230,120 );
	        setVisible( true );
	    }
    
	    // 用于事件处理的内部类
		private class ButtonHandler implements ActionListener
	    {
		public void actionPerformed( ActionEvent event )
		{
	    		String output = "";
				
			// 单击plain button
			if ( event.getSource() == button1 )
				output = "plain button pressed"; 
				
			// 单击 icon button
			else if ( event.getSource() == button2 )
				output = "icon button pressed"; 
					
			// 单击既有图标又有文字的button
			else if ( event.getSource() == button3 )
				output = "button with icon and text pressed"; 
				
			JOptionPane.showMessageDialog( null,output );
		  }
		}
		
    	public static void main(String[] args) 
	    {
	        JButtonDemo buttonDemo = new JButtonDemo();
	        buttonDemo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    }
	}

⌨️ 快捷键说明

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