SWT 创建一个被关闭的小部件(滚动复合)
SWT create a dismissed widget (scroll through composite)
我希望我可以创建一个我之前处理过的复合材料。目的是循环显示和隐藏复合材料以更改屏幕(滚动复合材料)。提前致谢。
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
作为父组合,您可以使用 GridData
的 exclude
选项来省略控件:
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);
我希望我可以创建一个我之前处理过的复合材料。目的是循环显示和隐藏复合材料以更改屏幕(滚动复合材料)。提前致谢。
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
作为父组合,您可以使用 GridData
的 exclude
选项来省略控件:
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);