eventdemo3.java

来自「本java源程序包括了大量的学习程序(共27章)方便大家学习」· Java 代码 · 共 51 行

JAVA
51
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
public class EventDemo3 extends JFrame implements ActionListener   //主类实现ActionListener方法
{
   private JButton button;
   private Container container;
   
   public EventDemo3() 
   {
	   super("EventDemo3");
       setSize(300,200);
       
  
       try
       {     //系统外观
 		 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 	   }catch(Exception e){}
 	  
       //获取内容面板
       container = getContentPane();
       
       //创建按钮
       button = new JButton("改变颜色");
       button.setFont(new Font("Serif", Font.PLAIN, 14));
       
       //注册监听器
       button.addActionListener(this);
       container.add(button, BorderLayout.SOUTH);
      
       setVisible(true);   //设置窗口可见
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public void actionPerformed(ActionEvent event)  //ActionListener接口中的,处理事件的方法
    {
    	int red = (int)(Math.random() * 256);
		int green = (int)(Math.random() * 256);
		int blue = (int)(Math.random() * 256);
		container.setBackground(new Color(red, green, blue));
    }
   
    public static void main(String[] arguments)
    {
       EventDemo3 application = new EventDemo3();     
    }
}
       


⌨️ 快捷键说明

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