自己的第一个JAVA程序

此博客展示了一个Java程序,定义了MyButton类继承自Button,实现按钮交互效果。在鼠标移动时,按钮会隐藏和显示。还创建了TestMyButton类,包含主方法,创建两个按钮并添加到窗口中,同时实现窗口关闭功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.*;
import java.awt.event.*;
class MyButton extends Button
{
 private MyButton friend;
 public void setfriend(MyButton friend)
 {
    this.friend=friend;
 }
 public MyButton(String name)
 {
     super(name);
  enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
 }
 protected void processMouseMotionEvent(MouseEvent e)
 {
  setVisible(false);
  friend.setVisible(true);
 }
}
public class TestMyButton
{
 public static void main(String args[])
 {
  MyButton btn1=new MyButton("你来抓我呀");
  MyButton btn2=new MyButton("你来抓我呀");
  btn1.setfriend(btn2);
  btn2.setfriend(btn1);
  btn1.setVisible(true);
  Frame f=new Frame("it315");
  f.setBackground(Color.red);
  f.addWindowListener(new MyWindowlistenter());
  f.add(btn1,"North");
  f.add(btn2,"South");
  f.setSize(300,300);
  f.setVisible(true);
  btn1.setVisible(false);
 }
}
class MyWindowlistenter extends WindowAdapter
{
 public void windowClosing(WindowEvent e)
 {
  e.getWindow().setVisible(false);
  ((Window)e.getComponent()).dispose();
  System.exit(0);
 }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值