Groovy SwingBuilder:使用滚动面板显示面板列表 [英] Groovy SwingBuilder : using a scrollpanel to show a list of panels

查看:122
本文介绍了Groovy SwingBuilder:使用滚动面板显示面板列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个包含组件的面板列表,即复选框,标签,按钮,它们都在同一水平线上;每个面板代表一组组件,以显示一项的信息. 我需要将面板列表(不确定的数字)放在滚动面板中,以适合主面板的高度.

I would like to show a list of panels containing components, i.e a checkbox, labels, buttons, all on the same horizontal line; each panel represents one set of components to display information for one item. I need to put the list of panels (number undetermined) inside a scrollpanel to fit within the main panel height.

我似乎找不到将滚动面板和面板与组件混合的解决方案.

I can't seem to find a solution for mixing scrollpanel and panels with components.

我想得到这个结果:

滚动面板{

  • 复选框| item1 | button1 | button1 |标签1 |标签1
  • 复选框| item2 | button2 | button2 |标签2 |标签2
  • 复选框| item3 | button3 | button3 |标签3 |标签3

  • checkbox | item1 | button1 | button1 | label1 | label1
  • checkbox | item2 | button2 | button2 | label2 | label2
  • checkbox | item3 | button3 | button3 | label3 | label3

[...]

}

这里有一个我目前显示的工作示例: Groovy SwingBuilder:用于更改面板颜色的按钮

There is a working example of what I have currently shown here : Groovy SwingBuilder : button to change the color of a panel

在那里,您可以看到有6个项目,每个项目都有各自与之相关的组件. 现在,如果我想显示60个项目而不是6个项目,则框架会扩展以适合它们,但超出了屏幕尺寸.

There, you can see there are 6 items, each one with their respective components relating to it. Now if I wanted to display 60 items instead of 6, the frame would expand to fit them but exceed the screen size.

对我来说,显而易见的是,那种滚动面板"可以胜任工作,但是尽管我检查了Java教程中的所有示例以及此处的相关问题,但我无法使它正常工作.

I seems so obvious to me that kind of a "scrollpanel" would do the job, but I can't get it working, although I checked all examples on the Java tutorials and the related questions here.

tia. 米歇尔

推荐答案

您可以将面板放在vbox内,而您又将其放在scrollPane内.

You can put the panels inside a vbox, which in-turn you put inside a scrollPane.

从上一个问题中获取代码,您将得到如下结果:

Taking the code from the previous question, you end up with something like this:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL

int numPanels = 20

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.HIDE_ON_CLOSE) {
  panel(id:'mainPanel'){
    scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) {
      vbox {
        (1..numPanels).each { num ->
          def panelID = "panel$num"
          def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
            label('description') 
            textField( id: "description$num", text:panelID, columns: 70 )
            button( id: "buttonpanel$num", text:panelID, actionPerformed:{
              swing."$panelID".background = java.awt.Color.RED
            } )
          }
        }
      }
    }

    boxLayout(axis: BXL.Y_AXIS)
    panel(id:'secondPanel' , alignmentX: 0f){                       
      button('Quit', actionPerformed:{
        frame.visible = false
      })
    }
  }       
}
frame.size = [ frame.width, 600 ]

这篇关于Groovy SwingBuilder:使用滚动面板显示面板列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆