这程序是swt编写的,请帮忙看下为什么在第二次点击按钮时group中的控件会消失
哪里有问题,请帮忙修改下
package com.tree;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
public class tes { protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
private Text text_3;
private Text text_4;
private Text text_5;
private Text text_6;
private Text text_7;
private Group group; /**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
tes window = new tes();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window.
*/
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(450, 482);
shell.setText("SWT Application");
shell.setLayout(new FormLayout());
final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
FormData fd_tabFolder = new FormData();
fd_tabFolder.bottom = new FormAttachment(0, 166);
fd_tabFolder.right = new FormAttachment(0, 385);
fd_tabFolder.top = new FormAttachment(0, 10);
fd_tabFolder.left = new FormAttachment(0, 10);
tabFolder.setLayoutData(fd_tabFolder);
TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem.setText("New Item");
Composite composite_2 = new Composite(tabFolder, SWT.NONE);
tbtmNewItem.setControl(composite_2);
text = new Text(composite_2, SWT.BORDER);
text.setBounds(10, 10, 70, 18);
text_1 = new Text(composite_2, SWT.BORDER);
text_1.setBounds(121, 10, 70, 18);
text_2 = new Text(composite_2, SWT.BORDER);
text_2.setBounds(10, 44, 70, 18);
text_3 = new Text(composite_2, SWT.BORDER);
text_3.setBounds(121, 44, 70, 18);
TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem_1.setText("New Item");
Composite composite = new Composite(tabFolder, SWT.NONE);
tbtmNewItem_1.setControl(composite);
text_4 = new Text(composite, SWT.BORDER);
text_4.setBounds(10, 23, 70, 18);
text_5 = new Text(composite, SWT.BORDER);
text_5.setBounds(128, 23, 70, 18);
TabItem tbtmNewItem_2 = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem_2.setText("New Item");
Composite composite_1 = new Composite(tabFolder, SWT.NONE);
tbtmNewItem_2.setControl(composite_1);
text_6 = new Text(composite_1, SWT.BORDER);
text_6.setBounds(22, 20, 70, 18);
text_7 = new Text(composite_1, SWT.BORDER);
text_7.setBounds(128, 20, 70, 18);
Button btnNewButton = new Button(shell, SWT.NONE);
FormData fd_btnNewButton = new FormData();
fd_btnNewButton.top = new FormAttachment(0, 189);
fd_btnNewButton.left = new FormAttachment(0, 54);
btnNewButton.setLayoutData(fd_btnNewButton);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TabItem[] item=tabFolder.getSelection();
TabItem it=item[0];
Composite cop=(Composite)it.getControl();
// for(Control c: cop.getChildren()){
// if(c instanceof Text){
// Text textField = (Text)c;
// // System.out.println(textField.getText());
//
// }
// }
for(Control c: group.getChildren()){
if(c instanceof Text){
Text textField = (Text)c;
textField.dispose();
}
}
group.setLayout(new GridLayout((int)Math.sqrt(cop.getChildren().length), false));
for(int i=0;i<(int)Math.sqrt(cop.getChildren().length);i++)
{
Text text=new Text(group,SWT.BORDER);
// text.setText("1 ");
// System.out.println("生成");
//
}
group.pack();
}
});
btnNewButton.setText("New Button");
Composite composite_3 = new Composite(shell, SWT.NONE);
FormData fd_composite_3 = new FormData();
fd_composite_3.bottom = new FormAttachment(0, 385);
fd_composite_3.right = new FormAttachment(0, 385);
fd_composite_3.top = new FormAttachment(0, 278);
fd_composite_3.left = new FormAttachment(0, 10);
composite_3.setLayoutData(fd_composite_3);
composite_3.setLayout(new FormLayout());
group = new Group(composite_3, SWT.NONE);
FormData fd_group = new FormData();
fd_group.bottom = new FormAttachment(0, 89);
fd_group.right = new FormAttachment(0, 348);
fd_group.top = new FormAttachment(0, 10);
fd_group.left = new FormAttachment(0, 10);
group.setLayoutData(fd_group);
group.setText("\u7ED3\u679C");
}
}
哪里有问题,请帮忙修改下
package com.tree;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
public class tes { protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
private Text text_3;
private Text text_4;
private Text text_5;
private Text text_6;
private Text text_7;
private Group group; /**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
tes window = new tes();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window.
*/
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(450, 482);
shell.setText("SWT Application");
shell.setLayout(new FormLayout());
final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
FormData fd_tabFolder = new FormData();
fd_tabFolder.bottom = new FormAttachment(0, 166);
fd_tabFolder.right = new FormAttachment(0, 385);
fd_tabFolder.top = new FormAttachment(0, 10);
fd_tabFolder.left = new FormAttachment(0, 10);
tabFolder.setLayoutData(fd_tabFolder);
TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem.setText("New Item");
Composite composite_2 = new Composite(tabFolder, SWT.NONE);
tbtmNewItem.setControl(composite_2);
text = new Text(composite_2, SWT.BORDER);
text.setBounds(10, 10, 70, 18);
text_1 = new Text(composite_2, SWT.BORDER);
text_1.setBounds(121, 10, 70, 18);
text_2 = new Text(composite_2, SWT.BORDER);
text_2.setBounds(10, 44, 70, 18);
text_3 = new Text(composite_2, SWT.BORDER);
text_3.setBounds(121, 44, 70, 18);
TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem_1.setText("New Item");
Composite composite = new Composite(tabFolder, SWT.NONE);
tbtmNewItem_1.setControl(composite);
text_4 = new Text(composite, SWT.BORDER);
text_4.setBounds(10, 23, 70, 18);
text_5 = new Text(composite, SWT.BORDER);
text_5.setBounds(128, 23, 70, 18);
TabItem tbtmNewItem_2 = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem_2.setText("New Item");
Composite composite_1 = new Composite(tabFolder, SWT.NONE);
tbtmNewItem_2.setControl(composite_1);
text_6 = new Text(composite_1, SWT.BORDER);
text_6.setBounds(22, 20, 70, 18);
text_7 = new Text(composite_1, SWT.BORDER);
text_7.setBounds(128, 20, 70, 18);
Button btnNewButton = new Button(shell, SWT.NONE);
FormData fd_btnNewButton = new FormData();
fd_btnNewButton.top = new FormAttachment(0, 189);
fd_btnNewButton.left = new FormAttachment(0, 54);
btnNewButton.setLayoutData(fd_btnNewButton);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TabItem[] item=tabFolder.getSelection();
TabItem it=item[0];
Composite cop=(Composite)it.getControl();
// for(Control c: cop.getChildren()){
// if(c instanceof Text){
// Text textField = (Text)c;
// // System.out.println(textField.getText());
//
// }
// }
for(Control c: group.getChildren()){
if(c instanceof Text){
Text textField = (Text)c;
textField.dispose();
}
}
group.setLayout(new GridLayout((int)Math.sqrt(cop.getChildren().length), false));
for(int i=0;i<(int)Math.sqrt(cop.getChildren().length);i++)
{
Text text=new Text(group,SWT.BORDER);
// text.setText("1 ");
// System.out.println("生成");
//
}
group.pack();
}
});
btnNewButton.setText("New Button");
Composite composite_3 = new Composite(shell, SWT.NONE);
FormData fd_composite_3 = new FormData();
fd_composite_3.bottom = new FormAttachment(0, 385);
fd_composite_3.right = new FormAttachment(0, 385);
fd_composite_3.top = new FormAttachment(0, 278);
fd_composite_3.left = new FormAttachment(0, 10);
composite_3.setLayoutData(fd_composite_3);
composite_3.setLayout(new FormLayout());
group = new Group(composite_3, SWT.NONE);
FormData fd_group = new FormData();
fd_group.bottom = new FormAttachment(0, 89);
fd_group.right = new FormAttachment(0, 348);
fd_group.top = new FormAttachment(0, 10);
fd_group.left = new FormAttachment(0, 10);
group.setLayoutData(fd_group);
group.setText("\u7ED3\u679C");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货