package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func main() {
newApp := app.New()
c := newApp.NewWindow("button")
btn(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func btn(w fyne.Window) {
label1 := widget.NewLabel("label") // 标签控件
btn1 := widget.NewButton("button click", func() { // 按钮昵称 按钮回调
fmt.Println("button click l")
label1.SetText("label is from")
})
btn1.Importance = widget.HighImportance
btn1.Alignment = widget.ButtonAlignCenter // 按钮字体方向
btn2 := widget.NewButtonWithIcon("btn2", theme.CancelIcon(), func() { // 带icon的按钮
label1.SetText("btn from label")
})
btn2.Alignment = widget.ButtonAlignLeading
button1 := NewMyButton("button1", func() { // 自定义按钮
fmt.Println("button1")
})
button1.SetCursor(desktop.TextCursor) // 自定义按钮 传入光标状态
w.SetContent(container.NewVBox(btn1, label1, btn2, button1))
}
type MyButton struct {
widget.Button
CCursor desktop.Cursor
}
func NewMyButton(label string, tapped func()) *MyButton {
button := &MyButton{}
button.Button.Text = label // 按钮标题
button.Button.OnTapped = tapped // 按钮事件
return button
}
func (b MyButton) Cursor() desktop.Cursor {
if b.CCursor != nil {
return b.CCursor
}
return desktop.CrosshairCursor
}
func (b MyButton) SetCursor(c desktop.Cursor) {
b.CCursor = c
}
package main
import (
"errors"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
c := myApp.NewWindow("输入框")
inputs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func inputs(w fyne.Window) {
input1 := widget.NewEntry() // 单行输入框
input1.TextStyle = fyne.TextStyle{Bold: true} // 输入框字体的粗细
input1.Wrapping = fyne.TextWrapBreak
input1.Validator = validation.NewRegexp("^[a-zA-Z]+$", "input")
input1.Password = true // 密码框展示
input1.SetPlaceHolder("pass is form password") // 提示语句
// 验证
input2 := widget.NewMultiLineEntry() // 多行输入框
input2.Wrapping = fyne.TextWrapBreak // 自动换行
input2.Validator = func(s string) error { // 验证
fmt.Println(s)
if s != "a" {
fmt.Println("input a")
return errors.New("错误")
}
return nil
}
input2.ActionItem = widget.NewButton("...", func() { // 输入框右边控件 (添加的按钮可以添加其他的)
input2.SetText("you clicked the actionitem")
})
w.SetContent(container.NewVBox(input1, input2))
}
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"strings"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("check控件多选框")
checks(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func checks(w fyne.Window) {
labelMsg := widget.NewLabel("")
check1 := widget.NewCheck("swim", func(b bool) {
if b {
labelMsg.Text = labelMsg.Text+",swim"
}else {
labelMsg.Text = strings.Replace(labelMsg.Text,",swim","",-1)
}
})
check2 := widget.NewCheck("sex", func(b bool) {
if b {
labelMsg.Text = labelMsg.Text+",sex"
}else {
labelMsg.Text = strings.Replace(labelMsg.Text,",sex","",-1)
}
})
check3 := widget.NewCheck("man", nil)
check3.OnChanged = func(b bool) {
if b {
labelMsg.Text = labelMsg.Text+","+check3.Text
}else {
labelMsg.Text = strings.Replace(labelMsg.Text,","+check3.Text,"",-1)
}
}
w.SetContent(container.NewVBox(labelMsg, check1,check2,check3))
}
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("radio单选框控件")
radios(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func radios(w fyne.Window) {
radio1 := widget.NewRadioGroup([]string{"a", "b"}, func(s string) {
fmt.Println(s)
})
radio1.Append("c") // 添加一个 选项
radio1.SetSelected("c") // 选中 c向
radio2 := widget.NewRadioGroup([]string{"a", "b"}, nil)
radio2.OnChanged = func(s string) {
fmt.Println(s)
}
radio2.Horizontal = true // 横向排列
radio2.Required = true
//widget.NewSeparator() // 一条横线区域
w.SetContent(container.NewVBox(radio1, widget.NewSeparator(), radio2))
}
发表评论