gpt4 book ai didi

java - SWT 创建一个关闭的小部件(滚动浏览复合)

转载 作者:行者123 更新时间:2023-12-01 11:41:33 25 4
gpt4 key购买 nike

我希望能够创建一个我之前处理过的复合 Material 。目的是循环显示和隐藏复合 Material 以更改屏幕(滚动复合 Material )。提前致谢。

public class prog {
static public void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell (display, SWT.CLOSE | SWT.TITLE | SWT.MIN );
shell.setText("EXAMPLE");
shell.setLayout(new GridLayout(1,false));

//I want comp1 initially be shown and then being hidden and that is shown comp2 with its components

final Composite comp1 = new Composite(shell, SWT.NONE);
comp1.setLayout(new GridLayout(1,false));
comp1.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

final Composite comp2 = new Composite(shell, SWT.BORDER);
comp2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

GridData data = (GridData)comp2.getLayoutData(); // Assumes layout was set earlier
data.exclude = true; // Don't include control in the layout

comp2.setVisible(false);

comp2.layout(true);

Label t = new Label(comp2, SWT.NONE);
t.setText("must be shown");

Button btn_create = new Button(comp1, SWT.PUSH);
btn_create.setText("change composite");
btn_create.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true,1,1));

btn_create.addSelectionListener(new SelectionAdapter() { //evento quando il bottone viene premuto
public void widgetSelected(SelectionEvent e) {
GridData data3 = (GridData)comp1.getLayoutData(); // Assumes layout was set earlier

data3.exclude = true; // Don't include control in the layout

comp1.setVisible(false);

comp1.layout(true);

GridData data2 = (GridData)comp2.getLayoutData(); // Assumes layout was set earlier

data2.exclude = false; // Don't include control in the layout

comp2.setVisible(true);

comp2.layout(true);
System.out.println(comp2.getVisible());



}});

//swt_create.create(display, shell, create);
shell.setSize(500, 100);

shell.addControlListener(new ControlAdapter() { // fa in modo che la dimensione della finestra si sempre
public void controlResized(ControlEvent e) { // uguale
shell.setSize(500,400);
}
});
shell.open();
shell.pack();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) { // questo etodo serve a leggere gli eventi che compiamo durante
display.sleep(); // l'esecuzione, quando si clicca sulla x si passa avanti e il ciclo viene
} // chiuso
}

display.dispose();
}}

最佳答案

一旦控件被释放,它就会消失并且无法重复使用。

您可以通过调用控件上的 setVisible(false) 来暂时隐藏控件,使其不可见。

当您使控件可见/不可见时,您需要更新父 Composite 的布局。根据您用于组合的布局,您可能还需要调整布局。

例如,如果您使用 GridLayout 作为父组合,则可以使用 GridDataexclude 选项来忽略该控件:

GridData data = (GridData)control.getLayoutData();  // Assumes layout was set earlier

data.exclude = true; // Don't include control in the layout

control.setVisible(false);

....

parentComposite.layout(true);

要显示控件,请反转设置:

GridData data = (GridData)control.getLayoutData();  // Assumes layout was set earlier

data.exclude = false; // Include control in the layout

control.setVisible(true);

....

parentComposite.layout(true);

关于java - SWT 创建一个关闭的小部件(滚动浏览复合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29487902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com