Java 避免在SWT中的小部件构造函数中设置父级?

Java 避免在SWT中的小部件构造函数中设置父级?,java,swing,swt,Java,Swing,Swt,虽然Swing体系结构假定小部件是一个“添加”到另一个小部件中的,但是SWT使用另一个范例:每个小部件都是在构造函数中设置了父级的 我认为前一种方法更好,因为我可以隔离创建每个控件的代码 比如说 // creating button 1 JButton button1 = new JButton("Button 1"); // creating button 2 JButton button2 = new JButton("Button 2"); // creating button 3 J

虽然
Swing
体系结构假定小部件是一个“添加”到另一个小部件中的,但是
SWT
使用另一个范例:每个小部件都是在构造函数中设置了父级的

我认为前一种方法更好,因为我可以隔离创建每个控件的代码

比如说

// creating button 1
JButton button1 = new JButton("Button 1");

// creating button 2
JButton button2 = new JButton("Button 2");

// creating button 3
JButton button3 = new JButton("Button 3");

// creating panel
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);

// creating frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);


frame.pack();
frame.setVisible(true);
这里我们可以有三个小部件,每个小部件都是在自己的代码块中创建的

如果我想再添加一个框架级别,我只需添加新块和一个小补丁

// creating button 1
JButton button1 = new JButton("Button 1");

// creating button 2
JButton button2 = new JButton("Button 2");

// creating button 3
JButton button3 = new JButton("Button 3");

// creating panel
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);

// creating another panel
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(panel);

// creating frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.add(panel);
frame.add(panel2); // one fix


frame.pack();
frame.setVisible(true);
SWT
中并非如此。我必须做很多修正,以重新分配所有的子创建代码到新的父

Display display = new Display();

Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.HORIZONTAL));


Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new RowLayout(SWT.HORIZONTAL));

Composite composite2 = new Composite(composite, SWT.NONE);
composite2.setLayout(new RowLayout(SWT.HORIZONTAL));


//Button button1 = new Button(composite, SWT.PUSH);
Button button1 = new Button(composite2, SWT.PUSH); // fix 1
button1.setText("Button 1");

// Button button2 = new Button(composite, SWT.PUSH);
Button button2 = new Button(composite2, SWT.PUSH); // fix 2
button2.setText("Button 2");

//Button button3 = new Button(composite, SWT.PUSH);
Button button3 = new Button(composite2, SWT.PUSH); // fix 3
button3.setText("Button 3");


shell.pack();
shell.open();
while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
        display.sleep();
}    
这是潜在错误的来源


有什么方法可以避免这个问题吗?

SWT小部件依赖于运行应用程序的系统的本机小部件,并且与它们紧密绑定。查找SWT和JFace权威指南的摘录,其中对其进行了很好的解释

我想到的这个“问题”的唯一解决方案是为SWT小部件创建自己的代理。代理将具有所需的小部件属性设置器和一些
create
方法,这些方法将以SWT的方式构造小部件,而不是将属性设置为代理

public class ButtonProxy
{
    private String text;
    private String tooltipText;

    // all the other parameters you would like to use

    public void setText(String text)
    {
        this.text = text;
    }

    public void setTooltipText(String tooltipText)
    {
        this.tooltipText = tooltipText;
    }

    // all the other setters

    public Button create(Composite parent, int flags)
    {
        Button btn = new Button(parent, flags);
        btn.setText(this.text);
        btn.setTooltipText(this.tooltipText);
        return btn;
    }
}
您还可以为代理制作一些
工厂
,使用生成器设计模式等


希望这有帮助

你的比较有缺陷。在swing中,在旧面板和框架之间引入新面板,而在SWT中,在按钮和组合之间引入新组合。如果你想在按钮和面板之间添加swing面板,你必须更改比SWT代码中更多的行…是的,你是对的,这是我的错。顺便说一句:SWT方法的一些优点:1。您不能忘记向父级添加控件。2.不能错误地将控件添加到多个父控件。