演示Frame界面展开与关闭动画

  1. package com.yy;
  2. import java.awt.Dimension;
  3. import java.awt.Frame;
  4. import java.awt.Point;
  5. import java.awt.Toolkit;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. /**
  9.  * 演示Frame界面展开与关闭动画
  10.  * 
  11.  * @author cyl
  12.  * 
  13.  */
  14. public class frameExit extends JFrame {
  15.     JButton btn_cancel = null;
  16.     frameExit frame;
  17.     public frameExit() {
  18.         frame = frameExit.this;
  19.         frame.setSize(8060);
  20.         btn_cancel = this.getJButton1();
  21.         add(btn_cancel);
  22.         actionCreate(frame);
  23.     }
  24.     // exit action
  25.     private JButton getJButton1() {
  26.         if (btn_cancel == null) {
  27.             btn_cancel = new JButton();
  28.             btn_cancel.setText("cancel");
  29.             btn_cancel.addActionListener(new java.awt.event.ActionListener() {
  30.                 public void actionPerformed(java.awt.event.ActionEvent e) {
  31.                     actionExit(frame);
  32.                 }
  33.             });
  34.         }
  35.         return btn_cancel;
  36.     }
  37.     // create motion
  38.     private void actionCreate(Frame frame) {
  39.         frame.setVisible(true);
  40.         final int loop = 60;
  41.         if (frame != null) {
  42.             Point oldP = frame.getLocation();
  43.             Dimension dim = frame.getSize();
  44.             int maxwidth = 800;
  45.             int maxheigth = 600;
  46.             oldP.x += dim.width * 2;
  47.             oldP.y += dim.height * 2;
  48.             int dx = maxwidth / loop;
  49.             int dy = maxheigth / loop;
  50.             for (int ii = 0; ii < loop; ii++) {
  51.                 dim.width += dx;
  52.                 dim.height += dy;
  53.                 frame.setSize(dim);
  54.                 centerFrame(frame);
  55.                 frame.repaint();
  56.                 try {
  57.                     Thread.sleep(600 / loop);
  58.                 } catch (InterruptedException e) {
  59.                     e.printStackTrace();
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     // Exit motion
  65.     private void actionExit(Frame frame) {
  66.         final int loop = 60;
  67.         if (frame != null) {
  68.             Point oldP = frame.getLocation();
  69.             Dimension dim = frame.getSize();
  70.             oldP.x += dim.width / 2;
  71.             oldP.y += dim.height / 2;
  72.             int dx = dim.width / loop;
  73.             int dy = dim.height / loop;
  74.             for (int ii = 0; ii < loop; ii++) {
  75.                 dim.width -= dx;
  76.                 dim.height -= dy;
  77.                 frame.setSize(dim);
  78.                 centerFrame(frame);
  79.                 try {
  80.                     Thread.sleep(600 / loop);
  81.                 } catch (InterruptedException e) {
  82.                     e.printStackTrace();
  83.                 }
  84.             }
  85.         }
  86.         frame.setVisible(false);
  87.         frame.dispose();
  88.         System.exit(1);
  89.     }
  90.     // center frame in screen
  91.     private void centerFrame(Frame frame) {
  92.         Toolkit kit = Toolkit.getDefaultToolkit();
  93.         Dimension screenSize = kit.getScreenSize();
  94.         int screenHeight = screenSize.height;
  95.         int screenWidth = screenSize.width;
  96.         int frameH = frame.getHeight();
  97.         int frameW = frame.getWidth();
  98.         frame.setLocation((screenWidth - frameW) / 2, (screenHeight - frameH) / 2);
  99.     }
  100.     public static void main(String[] args) {
  101.         new frameExit();
  102.     }
  103. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Python中,我们可以使用各种框架来实现界面跳转。其中,比较常用的框架有PyQt、Tkinter和wxPython。 PyQt是Python中最流行的GUI编程框架之一。它基于Qt库开发,提供了丰富的组件和功能,可以用于创建跨平台的桌面应用程序。在PyQt中,通过创建不同的窗口对象并设置其属性和方法来实现界面跳转。可以通过连接按钮的点击事件,使用`show()`方法显示下一个界面,以实现从一个界面跳转到另一个界面。 Tkinter是Python的标准GUI库之一,它提供了创建简单的界面的功能。在Tkinter中,可以使用`Tk`类创建主窗口,并使用`Button`组件创建按钮。通过给按钮绑定回调函数,在回调函数中使用`Toplevel`类创建新的窗口,然后使用`withdraw()`方法隐藏当前窗口,使用`deiconify()`方法显示新的窗口,以实现界面跳转。 wxPython是Python中另一个常用的GUI框架,它基于wxWidgets开发。在wxPython中,可以通过创建`Frame`对象来创建主窗口,并使用`Button`组件创建按钮。通过给按钮绑定事件处理函数,在事件处理函数中使用`Hide()`方法隐藏当前窗口,使用`Show()`方法显示新的窗口,以实现界面跳转。 总的来说,无论是使用PyQt、Tkinter还是wxPython,实现界面跳转的基本原理都是通过创建不同的窗口对象,并在按钮点击事件或其它事件中使用相关方法来实现。具体的实现方式可以根据不同的框架和项目需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值