我怎么能将这个JScrollPane类添加到我的swing构建器中?

时间:2017-12-25 06:22:44

标签: java swing user-interface groovy reflection

我正在尝试将这个类ScrollPaneDemo()与我的SwingBuilder组合在一起,但是这个单独存在以任何方式存在,我可以将两个组合出来,看起来像我读到的有关反射的相同界面,但我不知道如何我可以用它。

如何将此JScrollPane课程添加到我的摇摆建设者?

enter image description here

@Bindable
class Address { 
    String street, number, city
    String toString() { "address[street=$street,number=$number,city=$city]" }
}
class ScrollPaneDemo extends JFrame {

ScrollPaneDemo() {
    super("JScrollPane Demo");
    ImageIcon ii = new ImageIcon("pokemon.png");
    JScrollPane jsp = new JScrollPane(new JLabel(ii));
    getContentPane().add(jsp);
    setSize(300, 250);
    setVisible(true);
      jsp.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("Mouse X="+e.getY()+" Mouse Y="+e.getY());
            }
        });
  }}

  public static void main(String[] args) {
    new ScrollPaneDemo();
  }

更多定义

def address = new Address(street: 'Evergreen Terrace', number: '742', city: 'Springfield')
def swingBuilder = new SwingBuilder()
swingBuilder.edt {  // edt method makes sure UI is build on Event Dispatch Thread.
    lookAndFeel 'nimbus'  // Simple change in look and feel.

    frame(title: 'Address', size: [1000, 800],
            show: true, locationRelativeTo: null,
            defaultCloseOperation: EXIT_ON_CLOSE) {
        borderLayout(vgap: 5)
        panel(constraints: BorderLayout.CENTER,
                border: compoundBorder([emptyBorder(10), titledBorder('Enter your address:')])) {
            tableLayout {
                tr {
                    td {
                        label 'Street:'  // text property is default, so it is implicit.
                    }
                    td {
                        textField address.street, id: 'streetField', columns: 20
                    }
                }
                tr {
                    td {
                        label 'Number:'
                    }
                    td {
                        textField id: 'numberField', columns: 5, text: address.number
                    }
                }
                tr {
                    td {
                        label 'City:'
                    }
                    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
    td {
                        textField id: 'cityField', columns: 20, address.city
                    }
                }
            }
    new ScrollPaneDemo();
      }

        panel(constraints: BorderLayout.SOUTH) {
            button text: 'Save', actionPerformed: {
                println address
            }
        }

        // Binding of textfield's to address object.
        bean address,
            street: bind { streetField.text },
            number: bind { numberField.text },
            city: bind { cityField.text }
    } 
}

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了

scrollPane(id:'scroll',preferredSize: [200,200], constraints: context.CENTER) { 

    panel(layout: new FlowLayout()) {    

        label(icon: imageIcon(new URL('file:///path/pokemon.png')))
    }   

}