---
title: 创建自定义窗口
summary: 使用 frame、table、文本和按钮构建 Lua 界面,并正确清理资源。
section: ui
kind: example
status: example
updated: 2026-10-27
tags: ["X4","modding","create","ui"]
sources: ["https://wiki.egosoft.com/X4%20Foundations%20Wiki/Modding%20Support/Breaking%20Changes/"]
---
## 选择一个方法
对于一个小设置,请考虑一个 [设置菜单 API](wiki:ui-settings). 其他为了添加一个卡或操作到现有菜单, [扩展标准 UI](wiki:extend-ui). 其他个性窗口可以使用Helper创建一个框架,表,行和小工具,然后显示它. 无需 Python 和网络服务.
## 登记Lua
声明Lua文件在您的扩展中 `ui.xml`, 根据下面的图. 在打开窗口之前等到Helper和游戏UI准备好. 装载Lua文件并不意味着世界,
```xml
```
## 打造窗户
其他 `ui/wiki_panel.lua` 举例来说,它只会一次登记菜单. 单独的事件在UI准备后打开面板. 它在3层上创建一个框架,添加一个表格和文本,绑定一个关闭按,显示框架. 检查内部回调和层使用与当前游戏UI.
MD `raise_lua_event` 部分属于现有 `actions` 区块. 您的模特提供了触发器,例如一个工作按或事件. 没有自动安装热钥匙.
```lua
local menu = { name = "WikiExamplePanel" }
menu.onViewCreated = function() end
menu.onRowChanged = function() end
menu.onSelectElement = function() end
function menu.onCloseElement()
if menu.shown then
Helper.clearFrame(menu, 3)
menu.shown = false
end
end
Menus = Menus or {}
table.insert(Menus, menu)
Helper.registerMenu(menu)
local function showPanel()
if menu.shown then return end
local width = Helper.scaleX(480)
local height = Helper.scaleY(150)
local frame = Helper.createFrameHandle(menu, {
layer = 3,
x = (Helper.viewWidth - width) / 2,
y = (Helper.viewHeight - height) / 2,
width = width, height = height,
standardButtons = {},
startAnimation = false,
playerControls = true,
})
local t = frame:addTable(1, { tabOrder = 1, width = width })
local title = t:addRow(false, {})
title[1]:createText("Hello, X4!", { fontsize = 14 })
local row = t:addRow(true, {})
row[1]:createButton({ height = 28 }):setText("Close")
row[1].handlers.onClick = menu.onCloseElement
menu.shown = true
frame:display()
end
RegisterEvent("Wiki.ShowPanel", function()
showPanel()
end)
```
```xml
```
## 检查生命周期
开放一次不应会产生回调. 测试关闭按,逃跑/控制器行为,菜单的更改和保存加载. 没有足够的高度的桌子可以完全消失. 不要清除其他菜单的框或改变全球播放器控制,只是为了显示文本. 游戏中没有单独测试,并且在X4版本中没有进行单独检测.
在添加共享顶级图标或第三方 UIX 之前,请阅读 [回调和上方菜单限制](wiki:ui-hooks-and-top-menu). 其他这些机制具有独立的兼容性限制.