当前位置: 代码迷 >> J2SE >> 那位帮忙看看 swt
  详细解决方案

那位帮忙看看 swt

热度:242   发布时间:2016-04-24 12:36:26.0
那位大虾帮忙看看 swt
package abc;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.Display;
import org.eclipse.swt.widgets.Shell;

public class TestScroleComposite {

/**
* @param args
*/
public static void main(String[] args) {

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());

ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER| SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));
sc.setAlwaysShowScrollBars(true);


Composite c = new Composite(sc, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 4;
c.setLayout(layout);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

for (int i = 0; i < 20; i++) {
Button button = new Button(c, SWT.BORDER | SWT.PUSH);
button.setText("button" + i);
c.computeSize(SWT.DEFAULT, SWT.DEFAULT);
}

sc.setContent(c);
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
display.sleep();
}
}

}

}


问题时 界面中没有显示内容,只有一个空窗口


------解决方案--------------------
如果我没看错的话,sc没有setLayout
------解决方案--------------------
package swt.demo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.Display;
import org.eclipse.swt.widgets.Shell;

public class TestScroleComposite {

/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("程序标题");
shell.setLayout(new FillLayout());
shell.setSize(400, 300);
ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
sc1.setAlwaysShowScrollBars(true);

Composite c1 = new Composite(sc1, SWT.NONE);
c1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
GridLayout gl2 = new GridLayout(4, true);// 等宽的四列
c1.setLayout(gl2);
GridData gd1 = new GridData(GridData.FILL_BOTH);
c1.setLayoutData(gd1);
c1.setBounds(0, 0, 360, 400);
for (int i = 1; i < 21; i++) {
Button button = new Button(c1, SWT.BORDER | SWT.PUSH);
button.setText("button" + i);
}
c1.layout();

sc1.setContent(c1);

shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
display.sleep();
}
}

}

}

------解决方案--------------------
package swt.demo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.Display;
import org.eclipse.swt.widgets.Shell;

public class TestScroleComposite {

/**
* @param args
*/
  相关解决方案