package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("popupmenu 弹出菜单")
//popupmenus(c)
//popups(c)
modalPopups(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 弹出框是菜单
func popupmenus(w fyne.Window) {
label := widget.NewLabel("1213")
menu1 := fyne.NewMenu("file",
fyne.NewMenuItem("open", func() {
fmt.Println("open")
}), fyne.NewMenuItem("save", func() {
fmt.Println("save")
}),
)
popupmenu1 := widget.NewPopUpMenu(menu1, w.Canvas()) // 弹出菜单
btn1 := widget.NewButton("click", func() {
popupmenu1.Show() // 显示
})
btn2 := widget.NewButton("click2", func() {
popupmenu1.ShowAtPosition(fyne.NewPos(60, 60)) // 显示定位地方
})
w.SetContent(container.NewVBox(label, btn1, btn2))
}
// 弹出框可以自定义内容
func popups(w fyne.Window) {
label1 := widget.NewLabel("")
mune1 := container.NewVBox(widget.NewButton("func", func() {
label1.Text = "112"
}))
popup1 := widget.NewPopUp(container.NewVBox(label1, mune1), w.Canvas())
button1 := widget.NewButton("show popup", func() {
popup1.Show()
})
w.SetContent(container.NewVBox(button1))
}
// 全屏模态框 中间可以点击
func modalPopups(w fyne.Window) {
label1 := widget.NewLabel("")
mune1 := container.NewVBox(widget.NewButton("11", func() {
label1.Text = "666"
}))
modalpopup1 := widget.NewModalPopUp(mune1, w.Canvas())
button2 := widget.NewButton("close", func() {
modalpopup1.Hide()
})
button1 := widget.NewButton("btn", func() {
modalpopup1.Show()
})
mune1.Add(button2)
w.SetContent(button1)
}
二, toolbar 控件
package main
import (
_ "embed" // 讲静态文件打包成二进制文件
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.NewWithID("icons")
c := MyApp.NewWindow("icon")
toolbars(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
//go:embed 1.jpg
var icn []byte
func toolbars(w fyne.Window) {
lable1 := widget.NewLabel("") // 标签控件
// icon的 toolbar 按钮
toolbar1 := widget.NewToolbar(widget.NewToolbarAction(fyne.NewStaticResource("icons", icn), func() {
lable1.Text = "1123"
}),widget.NewToolbarAction(fyne.NewStaticResource("icons", icn), func() {
lable1.Text = "456"
}))
// icon的 toolbar 按钮
toolbar2 := widget.NewToolbar(widget.NewToolbarAction(theme.CancelIcon(), func() {
lable1.Text = "1123"
}),widget.NewToolbarAction(fyne.NewStaticResource("icons", icn), func() {
lable1.Text = "1123"
}))
w.SetContent(container.NewHBox(lable1, toolbar1,toolbar2))
}
三, 超链接(hyperlink 控件)
package main
import (
_ "embed" // 讲静态文件打包成二进制文件
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"net/url"
)
func main() {
MyApp := app.NewWithID("icons")
c := MyApp.NewWindow("icon")
hyperlinks(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func hyperlinks(w fyne.Window) {
link1 := widget.NewHyperlink("baidu", nil) // 创建一个link链接
link1.SetURLFromString("https://www.baidu.com/")// 给链接添加一个url
link2 := widget.NewHyperlink("dbsgw", urls("http://blog.dbsgw.com"))
w.SetContent(container.NewHBox(link2, link1))
}
// 转换字符串到 *url.URL
func urls(s string) *url.URL {
con, err := url.Parse(s)
if err != nil {
fmt.Println(err)
}
return con
}
四,icon (icon控件)
package main
import (
_ "embed" // 讲静态文件打包成二进制文件
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.NewWithID("icons")
c := MyApp.NewWindow("icon")
icons(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 打包一个 1.jpg到二进制 方便使用
//go:embed 1.jpg
var icn []byte
func icons(w fyne.Window) {
icon1 := widget.NewIcon(fyne.NewStaticResource("icons", icn)) // 创建一个icon图标
icon2 := widget.NewFileIcon(nil) // 创建一个file图标可添加路径的
icon2.SetURI(fyne.CurrentApp().Storage().RootURI()) // 设置图标路径
icon2.SetSelected(true) // 选中图标
lable := widget.NewLabel("") // 标签控件
w.SetIcon(fyne.NewStaticResource("111", icn)) // 给主程序设置图标
w.SetTitle("123456") // 主程序设置 title
w.SetContent(container.NewHBox(icon1, lable, icon2))
}
发表评论