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

📄 multicasttest.java

📁 这个是Java的swing相关的典型的源码
💻 JAVA
字号:
/**
   @version 1.41 2004-05-04
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MulticastTest
{
   public static void main(String[] args)
   {
      MulticastFrame frame = new MulticastFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A frame with buttons to make and close secondary frames
*/
class MulticastFrame extends JFrame
{
   public MulticastFrame()
   {
      setTitle("MulticastTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add panel to frame

      MulticastPanel panel = new MulticastPanel();
      add(panel);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;  
}

/**
   A panel with buttons to create and close sample frames.
*/
class MulticastPanel extends JPanel
{
   public MulticastPanel()
   {
      // add "New" button

      JButton newButton = new JButton("New");
      add(newButton);
      final JButton closeAllButton = new JButton("Close all");
      add(closeAllButton);

      ActionListener newListener = new 
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               BlankFrame frame = new BlankFrame(closeAllButton);
               frame.setVisible(true);
            }
         };

      newButton.addActionListener(newListener);
   }
}

/**
   A blank frame that can be closed by clicking a button.
*/
class BlankFrame extends JFrame
{
   /**
      Constructs a blank frame
      @param closeButton the button to close this frame
   */
   public BlankFrame(final JButton closeButton)
   {
      counter++;
      setTitle("Frame " + counter);
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
      setLocation(SPACING * counter, SPACING * counter);     
      
      // 在构造每个新框架(BlankFrame)时,都会向closeButton按钮对象注册一个新的动作监听器,这个动作监听器
      // 在actionPerformed方法中关闭这个框架。这样closeButton这个对象就注册多个动作监听器,
      //这样当用户点击Closeall按钮时,就会向这些动作监听器发送一个ActionEvent,而这些动作监听器就会执行
      // actionPerformed方法中关闭这个框架.
      //这是一种多点传送技术,即将多个事件监听器注册到同一对象上,当产生事件时就会向所有注册的监听器对象发送事件。
      
      closeListener = new 
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
            	// Removes the specified action listener so that it no longer receives action events from this button.
               closeButton.removeActionListener(closeListener);
               // 
               dispose();
            }
         };
         
      closeButton.addActionListener(closeListener);
   }

   private ActionListener closeListener;
   public static final int DEFAULT_WIDTH = 200;
   public static final int DEFAULT_HEIGHT = 150;  
   public static final int SPACING = 40;
   private static int counter = 0;
}

⌨️ 快捷键说明

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