Groovy GUI

  • Beitrags-Autor:
  • Beitrags-Kategorie:Java
  • Beitrags-Kommentare:0 Kommentare

Folgendes Skript erzeugt eine kleine Oberfläche mittels Groovy. Zum Einsatz kommen unter anderem verschiedene SwingBuilder-Komponenten und das GridBagLayout.

package bob.pokolm.usage
 
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import java.awt.BorderLayout as BL
import java.awt.GridBagConstraints
import java.awt.FlowLayout
import javax.swing.filechooser.FileFilter
import javax.swing.JFileChooser
import javax.swing.UIManager
 
 
def swing = new groovy.swing.SwingBuilder()
 
createFileChooserButton = { t, c ->
    int h = t.preferredSize.height
    b = swing.button(
        borderPainted: false, contentAreaFilled: false, constraints: c,
        preferredSize: [h,h], minimumSize: [h,h],
        icon: UIManager.getIcon("FileChooser.newFolderIcon"),
        actionPerformed:{
            fc = swing.fileChooser(
                dialogTitle:"Verzeichnis wählen",
                fileSelectionMode : JFileChooser.DIRECTORIES_ONLY
            )
            if (fc.showOpenDialog() == JFileChooser.APPROVE_OPTION) {
                t.text = fc.selectedFile;
            }
        })
    return b
}
 
def frame = swing.frame(title:'SwingDemo',
    defaultCloseOperation: WC.EXIT_ON_CLOSE) {
 
    gridBagLayout()
 
    label(text:"Pfad 1:", constraints:gbc(gridx:0, gridy:0, insets:[5,5,0,0]))
    t1 = textField(text:"", columns: 30, enabled:false, 
        constraints:gbc(gridx:1, gridy:0, weightx:1.0,
            fill:GridBagConstraints.HORIZONTAL, insets:[5,5,0,0]))
    createFileChooserButton(t1, gbc(gridx:2, gridy:0, insets:[5,2,0,2]));
 
    label(text:"Pfad 2:", constraints:gbc(gridx:0, gridy:1, insets:[5,5,0,0]))
    t2 = textField(text:"", columns: 30, enabled:false, 
        constraints:gbc(gridx:1, gridy:1, weightx:1.0,
            fill:GridBagConstraints.HORIZONTAL, insets:[5,5,0,0]))
    createFileChooserButton(t2, gbc(gridx:2, gridy:1, insets:[5,2,0,2]));
 
    panel (constraints:gbc(gridx:0, gridy:2, weightx:1.0, weighty:1.0,
            gridwidth:GridBagConstraints.REMAINDER,
            anchor:GridBagConstraints.LAST_LINE_END, insets:[15,0,0,0])) {
        borderLayout(hgap: 5)
        emptyBorder([5,5,5,5], parent:true)
        cb = comboBox(items:['Aktion 1', 'Aktion 2'], constraints: BL.CENTER)
        button(text:'Ausführen', actionPerformed: {
                println "Aktion: ${cb.selectedItem}"
            }, constraints: BL.EAST)
    }
 
}
frame.pack()
frame.show()

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.