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

📄 containertest.java

📁 这个是Java的swing相关的典型的源码
💻 JAVA
字号:
// Demonstrating the ContainerListener 


import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ContainerTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ContainerListener cont = new ContainerListener() {
      ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("Selected: " + e.getActionCommand());
        }
      };

      public void componentAdded(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.addActionListener(listener);
        }
      }

      public void componentRemoved(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.removeActionListener(listener);
        }
      }
    };

    contentPane.addContainerListener(cont);

    contentPane.setLayout(new GridLayout(3, 2));
    contentPane.add(new JButton("First"));
    contentPane.add(new JButton("Second"));
    contentPane.add(new JButton("Third"));
    contentPane.add(new JButton("Fourth"));
    contentPane.add(new JButton("Fifth"));

    frame.setSize(300, 200);
    frame.show();
  }
}


           

⌨️ 快捷键说明

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