代码如下:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;import tools.DBConn;public class SwingTest { private StyledText styledText; private Text text_2; private Text text; protected Shell shell; /**
 * Launch the application
 * 
 * @param args
 */
public static void main(String[] args) {
try {
SwingTest window = new SwingTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Open the window
 */
public void open() {
final 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(500, 375);
shell.setText("SWT Application"); final Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu); final MenuItem fileMenuItem = new MenuItem(menu, SWT.CASCADE);
fileMenuItem.setText("File"); final Menu menu_1 = new Menu(fileMenuItem);
fileMenuItem.setMenu(menu_1); final Label nameLabel = new Label(shell, SWT.NONE);
final Label nameLabel_1 = new Label(shell, SWT.NONE);
text = new Text(shell, SWT.BORDER);
text_2 = new Text(shell, SWT.BORDER);
final Button selectButton = new Button(shell, SWT.NONE);
final Button insertButton = new Button(shell, SWT.NONE);
styledText = new StyledText(shell, SWT.BORDER);
styledText.setBounds(0, 239, 492, 83); text.setBounds(102, 39, 80, 25);
text_2.setBounds(320, 40, 80, 25);
nameLabel.setBounds(30, 40, 50, 24);
nameLabel_1.setBounds(236, 40, 68, 24); nameLabel_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
nameLabel.setFont(SWTResourceManager.getFont("", 11, SWT.NONE)); final MenuItem selectMenuItem = new MenuItem(menu_1, SWT.NONE);
selectMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.setBounds(102, 39, 80, 25);
text_2.setBounds(320, 40, 80, 25);
nameLabel.setBounds(30, 40, 50, 24);
nameLabel_1.setBounds(236, 40, 68, 24);
text.setText("");
text_2.setText("");
nameLabel.setText("  Name");
nameLabel_1.setText("Address");
selectButton.setText("Select");
selectButton.setBounds(33, 175, 44, 23); selectButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
insertButton.setBounds(0, 0, 0, 0);
String username = text.getText();
String result = DBConn.getResult(username);
styledText.setText(result);
text.setText("");
text_2.setText("");
}
});
}
});
selectMenuItem.setText("Select"); final MenuItem insertMenuItem = new MenuItem(menu_1, SWT.NONE);
insertMenuItem.setText("Insert"); insertMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.setBounds(102, 39, 80, 25);
text_2.setBounds(320, 40, 80, 25);
nameLabel.setBounds(30, 40, 50, 24);
nameLabel_1.setBounds(236, 40, 68, 24);
text.setText("");
text_2.setText("");
styledText.setText("");
selectButton.setBounds(0, 0, 0, 0);
insertButton.setBounds(192, 164, 44, 23);
nameLabel.setText("姓名");
nameLabel_1.setText("地址");
insertButton.setText("插入");
insertButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String username = text.getText();
String password = text_2.getText();
System.out.println(username + " and " + password);
DBConn.insert(username, password);
text.setText("");
text_2.setText("");
styledText.setText("");
}
});
}
}); final MenuItem menuItem = new MenuItem(menu_1, SWT.NONE);
menuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
final Button button = new Button(shell, SWT.NONE);
button.setText("机选");
text.setBounds(0, 0, 0, 0);
text_2.setBounds(0, 0, 0, 0);
nameLabel.setBounds(0, 0, 0, 0);
nameLabel_1.setBounds(0, 0, 0, 0);
selectButton.setBounds(0, 0, 0, 0);
insertButton.setBounds(0, 0, 0, 0);
button.setBounds(192, 164, 44, 23);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int[] array1 = new int[7];
String result = "";
array1 = DBConn.choose();
array1 = DBConn.paixu(array1);
for (int i = 0; i < array1.length; i++) {
result = result + String.valueOf(array1[i]) + ",";
styledText.setText(result);
}
}
});
}
});
menuItem.setText("36选7"); final MenuItem exitMenuItem = new MenuItem(menu_1, SWT.NONE);
exitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
});
exitMenuItem.setText("Exit"); // }}
功能描述:有一个查询,插入和36选7的数字 的功能,这几个功能在DBConn类中实现。问题描述:我每次第一次运行查询或者插入功能,这两个功能都很正常,但是假如我先运行查询,然后切换到插入的功能,这是问题就出现了,它会插入两次记录,假如我再切换查询,再切换到插入,这时我插入的话就会自动插入3次,也就是说每切换一下更能,它激发同一个操作就会增加一次。 麻烦各位解答一下,谢谢!!!