1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
package org.eclipse.swt.widgets.custom;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Main {
public static void makeWidget(Composite parent) {
final Composite container = new Composite(parent, SWT.NONE);
final Button button = new Button(container, SWT.CHECK);
final Group group = new Group(container, SWT.NONE);
final Text text = new Text(group, SWT.MULTI | SWT.BORDER);
final GridLayout layout = new GridLayout(1, false);
layout.marginTop = 5;
text.setLayoutData(new GridData(GridData.FILL_BOTH));
group.setLayout(layout);
button.moveAbove(group);
button.setText("Enable group");
button.setSelection(true);
button.addListener(SWT.Selection, new Listener() {
void enableControl(Control control, boolean enabled) {
control.setEnabled(enabled);
if (control instanceof Composite) {
Control[] children = ((Composite) control).getChildren();
for (int i = 0; i < children.length; i++) {
enableControl(children[i], enabled);
}
}
}
@Override
public void handleEvent(Event event) {
enableControl(group, button.getSelection());
}
});
container.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event event) {
Rectangle area = container.getClientArea();
Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int x = area.x + 10;
int y = area.y;
int width = size.x;
int height = size.y;
group.setBounds(area.x, area.y + size.y / 2, area.width,
area.height - size.y / 2);
button.setBounds(x, y, width, height);
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
FillLayout layout = new FillLayout();
layout.marginHeight = 10;
layout.marginWidth = 10;
makeWidget(shell);
shell.setLayout(layout);
shell.setBounds(100, 100, 400, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
} |
Partager