利用反射实现窗体动态加载

听朋友说动态加载窗体的方式很不错,今天试了下,原来专业术语叫反射.又张见识了,嘿嘿!

代码如下:

         // 通过程序集动态加载窗体
        
// 解决方案名称"动态加载窗体",包含两个项目:WindowsApplication1,WindowsApplication2.
        
// WindowsApplication1项目中,包含窗体Form1和Form2,Form1中有两个Button:button1,button2并有单击事件.
        
// WindowsApplication2项目中无改动.

        
// 此事件显示当前项目(WindowsApplication1)中窗体Form2
         private   void  button1_Click( object  sender, EventArgs e)
        
{
            
//路径表示要调用的程序集所在路径,可以是窗体生成的*.exe文件或者*.dll文件
            string path = Application.StartupPath + @"WindowsApplication1.exe";

            Form from2 
= null;
            
//WindowsApplication2.Form1表示命名空间WindowsApplication1下的窗体Form2
            from2 = (Form)Activator.CreateInstance(Assembly.LoadFile(path).GetType("WindowsApplication1.Form2"false), true);
            from2.Show();
        }


        
// 此事件显示项目WindowsApplication2中窗体Form1
         private   void  button2_Click( object  sender, EventArgs e)
        
{
            
string path = System.IO.Path.GetFullPath(@"......WindowsApplication2inDebugWindowsApplication2.exe");

            Form from1 
= null;
            from1 
= (Form)Activator.CreateInstance(Assembly.LoadFile(path).GetType("WindowsApplication2.Form1"false), true);
            from1.Show();

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在JFrame窗体加载动态图片,可以使用JLabel组件和Timer类来实现。 以下是一个示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class DynamicImageFrame extends JFrame { private JLabel imageLabel; // 图片标签 private ImageIcon[] imageIcons; // 图片数组 private int currentIndex; // 当前图片索引 public DynamicImageFrame() { setTitle("动态图片"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 初始化图片数组 imageIcons = new ImageIcon[3]; imageIcons[0] = new ImageIcon("image1.jpg"); imageIcons[1] = new ImageIcon("image2.jpg"); imageIcons[2] = new ImageIcon("image3.jpg"); // 初始化图片标签 imageLabel = new JLabel(imageIcons[currentIndex]); imageLabel.setHorizontalAlignment(SwingConstants.CENTER); imageLabel.setVerticalAlignment(SwingConstants.CENTER); add(imageLabel, BorderLayout.CENTER); // 设置定时器,每隔一段时间切换图片 Timer timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { currentIndex = (currentIndex + 1) % imageIcons.length; imageLabel.setIcon(imageIcons[currentIndex]); } }); timer.start(); } public static void main(String[] args) { DynamicImageFrame frame = new DynamicImageFrame(); frame.setVisible(true); } } ``` 在上述代码中,首先定义了一个JLabel组件,用于显示图片。然后定义了一个ImageIcon数组,用于存储要显示的图片。在构造函数中,将第一张图片添加到图片标签中,并使用Timer类每隔一段时间切换图片。在定时器的ActionListener中,通过改变数组索引和设置图片标签的图标来实现动态显示图片。最后在main方法中创建JFrame窗体并显示出来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值