我如何在 oneline 中使用 jlabel 和 setbounds 而无需在 jpanel 中使用额外的变量?

How I could use jlabel with setbounds in oneline without use a extra variable in jpanel?

我有我的 use setbound 代码,但这使用了 3 行而不使用 hellolabel 变量

JLabel helloLabel = new JLabel("Hello world!");
helloLabel.setBounds( 10, 50, 60, 20 );  
panel.add(helloLabel);

我怎么能做到这一点,但使用像这样的单行样式

paintPane.add((new JLabel("Hello world!")).setBounds( 10, 50, 60, 20 ));

我用这个但是出现"void' type not allowed here".

我想创建自己的方法来创建这样的标签:

public JLabel createLabel(String title, Rectangle bounds){
    JLabel label = new JLabel(title);
    label.setBounds(bounds);
    return label;
}

所以你可以在一行中使用你的方法:

Panel panel = new Panel();
panel.add(createLabel("Hello world!", new Rectangle(10, 50, 60, 20)));