java: swing 写一个简单的Multi Document Interface (MDI)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * encoding: utf-8
 * 版权所有 2025 涂聚文有限公司
 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
 * 描述:
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2025 - 2025/2/8 - 22:36
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javaWeather
 * # File      : MDIApplication.java  类
 * # explain   : 学习
 **/
 
package UI;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
/**
 *主窗口:
 */
public class MDIApplication extends JFrame {
 
    /**
     * 用于承载子窗口的桌面面板
     */
    private JDesktopPane desktopPane; //
 
    /**
     * 设置窗口
     */
    public MDIApplication() {
 
        // 设置窗口标题
        setTitle("Multi Document Interface (MDI) 示例");
        setSize(800, 600); // 设置窗口大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
        setLocationRelativeTo(null); // 窗口居中
 
        // 创建桌面面板
        desktopPane = new JDesktopPane();
        setContentPane(desktopPane);
 
        // 创建菜单栏
        createMenuBar();
 
        // 显示窗口
        setVisible(true);
    }
 
    /**
     * 创建菜单栏
     */
    private void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
 
        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem newWindowItem = new JMenuItem("新建子窗口");
        JMenuItem exitItem = new JMenuItem("退出");
 
        // 新建子窗口菜单项的事件监听器
        newWindowItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createInternalFrame();
            }
        });
 
        // 退出菜单项的事件监听器
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
 
        fileMenu.add(newWindowItem);
        fileMenu.addSeparator(); // 添加分隔线
        fileMenu.add(exitItem);
 
        // 窗口菜单
        JMenu windowMenu = new JMenu("窗口");
        JMenuItem cascadeItem = new JMenuItem("层叠");
        JMenuItem tileItem = new JMenuItem("平铺");
 
        // 层叠子窗口菜单项的事件监听器
        cascadeItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cascadeInternalFrames();
            }
        });
 
        // 平铺子窗口菜单项的事件监听器
        tileItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tileInternalFrames();
            }
        });
 
        windowMenu.add(cascadeItem);
        windowMenu.add(tileItem);
 
        // 将菜单添加到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(windowMenu);
 
        // 设置菜单栏
        setJMenuBar(menuBar);
    }
 
    /**
     * 创建子窗口
     */
    private void createInternalFrame() {
        JInternalFrame internalFrame = new JInternalFrame("子窗口 " + (desktopPane.getAllFrames().length + 1),
                true, true, true, true);
        internalFrame.setSize(300, 200);
        internalFrame.setLocation(desktopPane.getAllFrames().length * 20, desktopPane.getAllFrames().length * 20);
        internalFrame.setVisible(true);
 
        // 将子窗口添加到桌面面板
        desktopPane.add(internalFrame);
    }
 
    /**
     * 层叠子窗口
     */
    private void cascadeInternalFrames() {
        JInternalFrame[] frames = desktopPane.getAllFrames();
        int x = 0;
        int y = 0;
        for (JInternalFrame frame : frames) {
            frame.setLocation(x, y);
            x += 20;
            y += 20;
        }
    }
 
    /**
     * 平铺子窗口
     */
    private void tileInternalFrames() {
        JInternalFrame[] frames = desktopPane.getAllFrames();
        if (frames.length == 0) return;
 
        int rows = (int) Math.sqrt(frames.length);
        int cols = (int) Math.ceil((double) frames.length / rows);
        int width = desktopPane.getWidth() / cols;
        int height = desktopPane.getHeight() / rows;
 
        int x = 0;
        int y = 0;
        for (JInternalFrame frame : frames) {
            frame.setSize(width, height);
            frame.setLocation(x, y);
            x += width;
            if (x >= desktopPane.getWidth()) {
                x = 0;
                y += height;
            }
        }
    }
 
}

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import UI.MDIApplication;;
import java.awt.*;
import javax.swing.*;
 
 
public class Main {
    public static void main(String[] args) {
 
 
        // 在事件调度线程中创建和显示 GUI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //new TextSaverGUI();
                new MDIApplication();
            }
        });
}
}

  

 

posted @   ®Geovin Du Dream Park™  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2023-02-08 CSharp: donet 7 create logging File with EF Core 7.02
2018-02-08 Csharp: read Sybase SQL anywhere5.5 using c#
2017-02-08 Python:dictionary
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示