Scroll 使用GXUI使窗口滚动

Scroll 使用GXUI使窗口滚动,scroll,go,scrollbar,gxui,Scroll,Go,Scrollbar,Gxui,我正在尝试使用GXUI在Go中将滚动条添加到我的应用程序窗口中 假设我有这个代码: package main import ( "fmt" "github.com/google/gxui" "github.com/google/gxui/drivers/gl" "github.com/google/gxui/samples/flags" "github.com/google/gxui/themes/dark" ) func appMain(drive

我正在尝试使用GXUI在Go中将滚动条添加到我的应用程序窗口中

假设我有这个代码:

package main

import (
    "fmt"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/samples/flags"
    "github.com/google/gxui/themes/dark"
)

func appMain(driver gxui.Driver) {
    theme := dark.CreateTheme(driver)

    window := theme.CreateWindow(800, 600, "Grid")
    window.SetScale(flags.DefaultScaleFactor)
    window.OnClose(driver.Terminate)

    row := theme.CreateLinearLayout()
    row.SetDirection(gxui.LeftToRight)
    for c := 0; c < 4; c++ {
        col := theme.CreateLinearLayout()
        col.SetDirection(gxui.TopToBottom)
        for r := 0; r < 100; r++ {
            cell := theme.CreateLabel()
            cell.SetText(fmt.Sprintf("%d", r*4+c))
            col.AddChild(cell)
        }
        row.AddChild(col)
    }

    window.AddChild(row)
}

func main() {
    gl.StartDriver(appMain)
}
主程序包
进口(
“fmt”
“github.com/google/gxui”
“github.com/google/gxui/drivers/gl”
“github.com/google/gxui/samples/flags”
“github.com/google/gxui/themes/dark”
)
func appMain(驱动程序gxui.driver){
主题:=dark.CreateTheme(驱动程序)
window:=theme.CreateWindow(800600,“网格”)
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(驱动程序终止)
行:=主题。CreateLinearLayout()
行设置方向(gxui.LeftToRight)
C=0;C<4;C++ {
col:=theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
对于r:=0;r<100;r++{
单元格:=主题。CreateLabel()
cell.SetText(fmt.Sprintf(“%d”,r*4+c))
col.AddChild(单元格)
}
row.AddChild(列)
}
window.AddChild(行)
}
func main(){
总账StartDriver(appMain)
}
当我运行它时,我会看到以下窗口:


如何使窗口具有滚动条以便查看所有行?

我无法使用帮助ScrollLayout,但我可以根据中的示例提出此变体

主程序包
进口(
“fmt”
“github.com/google/gxui”
“github.com/google/gxui/drivers/gl”
“github.com/google/gxui/math”
“github.com/google/gxui/samples/flags”
“github.com/google/gxui/themes/dark”
)
类型customAdapter结构{
gxui.适配器基
}
func(a*customAdapter)Count()int{
返回1000
}
func(a*customAdapter)ItemAt(index int)gxui.AdapterItem{
回报指数
}
func(a*customAdapter)项索引(项gxui.AdapterItem)int{
返回项目(整数)
}
func(a*customAdapter)Size(theme gxui.theme)math.Size{
返回数学.Size{W:200,H:25}
}
func(a*customAdapter)Create(theme gxui.theme,index int)gxui.Control{
layout1:=theme.CreateLinearLayout()
布局1.SetDirection(gxui.LeftToRight)
C=0;C<4;C++ {
col:=theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
单元格:=主题。CreateLabel()
cell.SetText(fmt.Sprintf(“%d”,索引*4+c))
col.AddChild(单元格)
布局1.AddChild(列)
}
返回布局1
}
func appMain(驱动程序gxui.driver){
主题:=dark.CreateTheme(驱动程序)
window:=theme.CreateWindow(600400,“网格”)
window.BorderPen()
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(驱动程序终止)
适配器:=&customAdapter{}
list:=theme.CreateList()
list.SetAdapter(适配器)
列表.设置方向(gxui.垂直)
window.AddChild(列表)
}
func main(){
总账StartDriver(appMain)
}

每行都放置在列表中,它们的编号和大小在重写的方法中指定。优点是列表中已有滚动条。

以下代码使用ScrollLayout将滚动条添加到窗口中。诀窍是使ScrollLayout成为窗口的子窗口,并使下一个小部件(在本例中为LinearLayout)成为ScrollLayout的子窗口

package main

import (
    "fmt"
    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/samples/flags"
    "github.com/google/gxui/themes/dark"
)

func appMain(driver gxui.Driver) {
    theme := dark.CreateTheme(driver)
    window := theme.CreateWindow(800, 600, "Grid")
    window.SetScale(flags.DefaultScaleFactor)
    window.OnClose(driver.Terminate)
    sl := theme.CreateScrollLayout()
    row := theme.CreateLinearLayout()
    row.SetDirection(gxui.LeftToRight)
    for c := 0; c < 4; c++ {
        col := theme.CreateLinearLayout()
        col.SetDirection(gxui.TopToBottom)
        for r := 0; r < 100; r++ {
            cell := theme.CreateLabel()
            cell.SetText(fmt.Sprintf("%d", r*4+c))
            col.AddChild(cell)
        }
        row.AddChild(col)
    }
    sl.SetChild(row)
    window.AddChild(sl)
}

func main() {
    gl.StartDriver(appMain)
}
主程序包
进口(
“fmt”
“github.com/google/gxui”
“github.com/google/gxui/drivers/gl”
“github.com/google/gxui/samples/flags”
“github.com/google/gxui/themes/dark”
)
func appMain(驱动程序gxui.driver){
主题:=dark.CreateTheme(驱动程序)
window:=theme.CreateWindow(800600,“网格”)
window.SetScale(flags.DefaultScaleFactor)
window.OnClose(驱动程序终止)
sl:=theme.CreateScrollLayout()
行:=主题。CreateLinearLayout()
行设置方向(gxui.LeftToRight)
C=0;C<4;C++ {
col:=theme.CreateLinearLayout()
col.SetDirection(gxui.TopToBottom)
对于r:=0;r<100;r++{
单元格:=主题。CreateLabel()
cell.SetText(fmt.Sprintf(“%d”,r*4+c))
col.AddChild(单元格)
}
row.AddChild(列)
}
sl.SetChild(世界其他地区)
window.AddChild(sl)
}
func main(){
总账StartDriver(appMain)
}

请注意,当我增加行数(最右边的列开始被切断)时,我的计算机给了我显示问题,但其他人没有遇到这个问题,因此可能是因为我这边的安装不好。

你需要我认为…@AlexKroll,主要的问题是将显示区域与方形卷轴一起移动。卷轴布局似乎是一种方式,但我遇到了显示中断的问题。@BarryMcNamara,从100更改为2000,所有内容都按其应有的方式显示。@BarryMcNamara,这真的很奇怪。对于我来说,显示是这样的:大部分都是工作的,但是滚动条在屏幕中间是很难的(简单的修复),在添加它时,行变成了某种选择列表。这并不是让任何应用程序窗口滚动的通用解决方案。
package main

import (
    "fmt"
    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/samples/flags"
    "github.com/google/gxui/themes/dark"
)

func appMain(driver gxui.Driver) {
    theme := dark.CreateTheme(driver)
    window := theme.CreateWindow(800, 600, "Grid")
    window.SetScale(flags.DefaultScaleFactor)
    window.OnClose(driver.Terminate)
    sl := theme.CreateScrollLayout()
    row := theme.CreateLinearLayout()
    row.SetDirection(gxui.LeftToRight)
    for c := 0; c < 4; c++ {
        col := theme.CreateLinearLayout()
        col.SetDirection(gxui.TopToBottom)
        for r := 0; r < 100; r++ {
            cell := theme.CreateLabel()
            cell.SetText(fmt.Sprintf("%d", r*4+c))
            col.AddChild(cell)
        }
        row.AddChild(col)
    }
    sl.SetChild(row)
    window.AddChild(sl)
}

func main() {
    gl.StartDriver(appMain)
}