Problem mam taki chcę użyć layoutu w widoku mojej aplikacji i gdy tworze klikając gui(windows builder) i w gridLayaoutcie dodaje button to działa. Jedank gdy przeklejam kod do listenera osblugującego naciśnieecie przycisku to nie ma rzadnych błędów jednak niedziała:
Kod niedziłający
package gui;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridData;
public class MainWindow {
protected Shell shell;
private GridLayout gLayout = new GridLayout(1,false);
FormData fd_scrolledComposite;
Composite composite;
/**
* Launch the application.
* @param args
*/
/**
* Open the window.
* @wbp.parser.entryPoint
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(703, 564);
shell.setText("SWT Application");
shell.setLayout(new FormLayout());
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
mntmNewSubmenu.setText("French");
Menu menu_1 = new Menu(mntmNewSubmenu);
mntmNewSubmenu.setMenu(menu_1);
MenuItem mntmOptions = new MenuItem(menu_1, SWT.NONE);
mntmOptions.setText("Options");
MenuItem mntmDodajWyraz = new MenuItem(menu_1, SWT.NONE);
mntmDodajWyraz.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("dodawanie");
// InputWindow inWin = new InputWindow(composite, SWT.NONE, "asd");
new Label(composite, SWT.NONE);
Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setText("New Button");
}
});
mntmDodajWyraz.setText("Dodaj wyraz");
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
});
mntmExit.setText("Exit");
composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
FormData fd_composite = new FormData();
fd_composite.bottom = new FormAttachment(0, 378);
fd_composite.right = new FormAttachment(0, 560);
fd_composite.top = new FormAttachment(0, 2);
fd_composite.left = new FormAttachment(0, 30);
composite.setLayoutData(fd_composite);
// inputWindow1.setBounds(5, 5, 200, 27);
}
}
atutaj ten sam kod tylko, że dodanie nowego buttona jest w create content
package gui;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridData;
public class MainWindow {
protected Shell shell;
private GridLayout gLayout = new GridLayout(1,false);
FormData fd_scrolledComposite;
Composite composite;
/**
* Launch the application.
* @param args
*/
/**
* Open the window.
* @wbp.parser.entryPoint
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(703, 564);
shell.setText("SWT Application");
shell.setLayout(new FormLayout());
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
mntmNewSubmenu.setText("French");
Menu menu_1 = new Menu(mntmNewSubmenu);
mntmNewSubmenu.setMenu(menu_1);
MenuItem mntmOptions = new MenuItem(menu_1, SWT.NONE);
mntmOptions.setText("Options");
MenuItem mntmDodajWyraz = new MenuItem(menu_1, SWT.NONE);
mntmDodajWyraz.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("dodawanie");
// InputWindow inWin = new InputWindow(composite, SWT.NONE, "asd");
}
});
mntmDodajWyraz.setText("Dodaj wyraz");
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
});
mntmExit.setText("Exit");
composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
FormData fd_composite = new FormData();
fd_composite.bottom = new FormAttachment(0, 378);
fd_composite.right = new FormAttachment(0, 560);
fd_composite.top = new FormAttachment(0, 2);
fd_composite.left = new FormAttachment(0, 30);
composite.setLayoutData(fd_composite);
new Label(composite, SWT.NONE);
Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setText("New Button");
// inputWindow1.setBounds(5, 5, 200, 27);
}
}
dlaczego to tak się zachowuje i jak zrobić aby działało to po naciśnieciu przycisku w menu