Groovy SwingBuilder:控制JTabbedPane中标签标题的样式 [英] Groovy SwingBuilder : controlling the style of the tabs titles in JTabbedPane

查看:1170
本文介绍了Groovy SwingBuilder:控制JTabbedPane中标签标题的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种方式(是否有任何?)来设置JTabbedPane中标签标题的样式。



我可以更改标签窗格(见下文),但找不到一种方式来标记标签的标题;我想让它们加粗或红色,或者能够定义标签宽度,例如,我可以格式化第一个面板中标签的样式。



这里的代码,主要是灵感来自tim_yates(http://stackoverflow.com/questions/6444068/groovy-swingbuilder-using-a-scrollpanel-to-show-a-list-of-panels):

  import groovy.swing.SwingBuilder 
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font(Serif,Font.BOLD,13)
int numPanels = 20

swing = new SwingBuilder()
frame = swing.frame pack:true,visible:true,defaultCloseOperation:WC.DISPOSE_ON_CLOSE){
tabbedPane(id:'tabs',tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT){
panel(name:'Tab 1',background:java .awt.Color.WHITE){
boxLayout(axis:BXL.Y_AXIS)
panel(alignmentX:0f,background:java.awt.Color.WHITE){
label ',preferredSize:[104,24])setFont(font)
label('Label 2',preferredSize:[104,24] preferredSize:[104,24])setFont(font)
}
scrollPane(verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS){
vbox(){
(1..numPanels)。每个{num->
def panelID =panel $ num
def pane = panel(alignmentX:0f,id:panelID,background:java.awt.Color.GREEN){
label
textField(id:description $ num,text:panelID,columns:70)
button(id:buttonpanel $ num,text:panelID,actionPerformed:{
swing。 $ panelID.background = java.awt.Color.RED
})
}
}
}
}
}
面板name:'Tab 2',background:java.awt.Color.WHITE){
textField(text:'Some text',columns:15)
scrollPane(){
textArea :'Some text',columns:15,rows:4)
}
}
}
boxLayout(axis:BXL.Y_AXIS)
panel secondBanel',background:java.awt.Color.WHITE){
button('Quit',actionPerformed:{
dispose()
})
}
}
frame.size = [frame.width,600]

非常难以在Groovy中实现:





此外,Java文档不解释如何做,



感谢您的帮助。



b
$ b

Michel。



PS:Ant提供了链接



Groovy SwingBuilder:更改大小和/或(在jTabbedpane中)



到一个有趣的文章,但不直接有助于我的问题(最初太模糊)。

解决方案

我相信你需要调用 jtabbedpane.setTabComponentAt 可在此处找到的javadoc



这需要你使用Java 6(对于Java 5,你将不得不考虑写一个自定义的TabbedPaneUI类,并覆盖这个 - - 或者使用允许此操作的其他来源的自定义JTabbedPane类)



这是一个操作示例:

  import groovy.swing.SwingBuilder 
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font(Serif, Font.BOLD,13)
int numPanels = 20

swing = new SwingBuilder()

frame = swing.frame(title:'test' true,visible:true,defaultCloseOperation:WC.DISPOSE_ON_CLOSE){
vbox {
tabbedPane(id:'tabs',tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT){
panel(name:'Tab 1' ,background:java.awt.Color.WHITE){
vbox {
panel(background:java.awt.Color.WHITE){
label('Label 1',preferredSize:[104 ,24])。setFont(font 3),
label('Label 2',preferredSize:[104,24] ]。setFont(font)
}
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
textField(id:description $ num,text:panelID,columns:70)
button(id:buttonpanel $ num,text:panelID,actionPerformed:{
swing。 $ panelID.background = java.awt.Color.RED
})
}
}
}
}
}
}
面板(名称:'Tab 2',背景:java.awt.Color.WHITE){
textField(text:'Some text',columns:15)
scrollPane $ b textArea(text:'Some text',columns:15,rows:4)
}
}
}
panel(id:'secondPanel',background:java。 awt.Color.WHITE){
button('Quit',actionPerformed:{
dispose()
})
}
}
}

//定义标签列表
def tabComponents = [
swing.label(text:'Tab 1',font:font.deriveFont(Font.ITALIC)) ,
swing.label(text:'Tab 2',font:font.deriveFont(20.0f))
]
//将标签组件设置为我们的标签
tabComponents。 eachWithIndex {lbl,idx->
swing.tabs.setTabComponentAt idx,lbl
}

frame.size = [frame.width,600]

PS:您可能想要移除我刚刚发现的其他问题一般来说,编辑一个问题以包括额外的信息比发布一个新的问题,询问相同的东西,但更多的信息


I can't find a way (is there any ?) to format the style of tabs titles in a JTabbedPane.

I can change the background color of the tab panes (see below), but can't find a way to style the titles of the tabs; I would like to have them bold or red or be able to define the tabs width, for instance, like I could format the style of the labels in the first panel.

Here's the code, mostly inspired by tim_yates (http://stackoverflow.com/questions/6444068/groovy-swingbuilder-using-a-scrollpanel-to-show-a-list-of-panels) :

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font("Serif", Font.BOLD, 13) 
int numPanels = 20

swing = new SwingBuilder()
frame = swing.frame(title:'test', pack:true,   visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    tabbedPane(id: 'tabs', tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT) {
        panel(name: 'Tab 1', background:java.awt.Color.WHITE ) {
            boxLayout(axis: BXL.Y_AXIS) 
            panel(alignmentX: 0f, background:java.awt.Color.WHITE){
                label ( 'Label 1', preferredSize: [104, 24]).setFont(font) 
                label ( 'Label 2', preferredSize: [104, 24]).setFont(font) 
                label ( 'Label 3', preferredSize: [104, 24]).setFont(font) 
            }   
            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
                            } )
                        }
                    }
                }
            }
        }       
        panel(name: 'Tab 2', background:java.awt.Color.WHITE) {
            textField(text: 'Some text', columns: 15)
            scrollPane() {
                textArea(text: 'Some text', columns: 15, rows: 4)
            }
        }       
    }
    boxLayout(axis: BXL.Y_AXIS)
    panel(id:'secondPanel', background:java.awt.Color.WHITE){                       
        button('Quit', actionPerformed:{
        dispose()
        })
    }   
}
frame.size = [ frame.width, 600 ]

I found these links which look very difficult (to me) to implement in Groovy :

Also the Java docs do not explain how to do that, and I didn't find any example using styled tabs.

Thanks for your help.

Regards,

Michel.

PS : Ant's offered a link

Groovy SwingBuilder : changing size and/or font of tabs (in jTabbedpane)

to an interesting article, but not directly helpful for my question (initially too vague).

解决方案

I believe you need to call jtabbedpane.setTabComponentAt (the javadoc for which can be found here)

This requires you be using Java 6 (for Java 5, you are going to have to look into writing a custom TabbedPaneUI class, and overriding this -- or use a custom JTabbedPane class from some other source that allows this)

Here's an example of it in action:

import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JTabbedPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import java.awt.Font

Font font = new Font("Serif", Font.BOLD, 13) 
int numPanels = 20

swing = new SwingBuilder()

frame = swing.frame(title:'test', pack:true,   visible:true, defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
    vbox {
        tabbedPane(id: 'tabs', tabLayoutPolicy:JTabbedPane.SCROLL_TAB_LAYOUT) {
            panel( name:'Tab 1', background:java.awt.Color.WHITE ) {
                vbox {
                    panel( background:java.awt.Color.WHITE ){
                        label ( 'Label 1', preferredSize: [104, 24]).setFont(font) 
                        label ( 'Label 2', preferredSize: [104, 24]).setFont(font) 
                        label ( 'Label 3', preferredSize: [104, 24]).setFont(font) 
                    }   
                    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
                                    } )
                                }
                            }
                        }
                    }
                }
            }       
            panel(name: 'Tab 2', background:java.awt.Color.WHITE) {
                textField(text: 'Some text', columns: 15)
                scrollPane() {
                    textArea(text: 'Some text', columns: 15, rows: 4)
                }
            }       
        }
        panel(id:'secondPanel', background:java.awt.Color.WHITE){                       
            button('Quit', actionPerformed:{
                dispose()
            })
        }   
    }
}

// Define a list of labels for our tabs
def tabComponents = [
  swing.label( text:'Tab 1', font:font.deriveFont( Font.ITALIC ) ),
  swing.label( text:'Tab 2', font:font.deriveFont( 20.0f ) )
]
// Set the tab componets to our labels
tabComponents.eachWithIndex { lbl, idx ->
  swing.tabs.setTabComponentAt idx, lbl
}

frame.size = [ frame.width, 600 ]

PS: You might want to remove your other question that I just found... Generally editing a question to include extra informations is better than posting a new question asking the same thing but with more information

这篇关于Groovy SwingBuilder:控制JTabbedPane中标签标题的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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