Java二级操作题 界面题 (卷19)

这篇博客展示了如何使用Java Swing库创建多窗口应用程序。`MulticastFrame` 类作为主窗口,包含`MulticastPanel`,后者包含两个按钮:“New”和“Close all”。点击“New”按钮会创建一个新的`BlankFrame`窗口,而点击“Close all”则关闭所有已创建的`BlankFrame`窗口。每个`BlankFrame`窗口都有唯一的标题并能动态定位。

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

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

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

class MulticastFrame extends JFrame{
   public MulticastFrame(){
      setTitle("MulticastTest");
      setSize(WIDTH, HEIGHT);
      MulticastPanel panel = new MulticastPanel();
      Container contentPane = getContentPane();
      //*********Found**********
      contentPane.add(panel);
   }
   public static final int WIDTH = 300;
   public static final int HEIGHT = 200;  
}

class MulticastPanel extends JPanel{
   public MulticastPanel(){
      JButton newButton = new JButton("New");
      add(newButton);
      ActionListener newListener = new ActionListener(){
         public void actionPerformed(ActionEvent event){
            makeNewFrame();
         }
      };
      newButton.addActionListener(newListener);
      closeAllButton = new JButton("Close all");
      add(closeAllButton);
   }
   private void makeNewFrame(){
      final BlankFrame frame = new BlankFrame();
      frame.show();
      ActionListener closeAllListener = new ActionListener(){
         public void actionPerformed(ActionEvent event){
            //*********Found**********
            frame.setVisible(false);     //使窗口隐藏或消除
         }
      };
      //*********Found**********
      closeAllButton.addActionListener(closeAllListener);
   }
   private JButton closeAllButton;
}

class BlankFrame extends JFrame{
   public BlankFrame(){
      //*********Found**********
      counter ++ ;
      setTitle("Frame " + counter);
      setSize(WIDTH, HEIGHT);
      setLocation(SPACING * counter, SPACING * counter);     
   }
   public static final int WIDTH = 200;
   public static final int HEIGHT = 150;  
   public static final int SPACING = 30;
   private static int counter = 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值