代码如下:
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.FileDialog;
import java.io.*;
import java.lang.*;
import java.awt.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Label;
import org.eclipse.jface.resource.*;
import java.sql.*;
import org.eclipse.swt.widgets.Text;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class sc {
Shell sShell = null; // @jve:decl-index=0:visual-constraint="26,-11"
private Button button = null;
private Label label = null;
private Label label1 = null;
private Label label2 = null;
private Label label3 = null;
private Label label4 = null;
private Text text = null;
private Text text1 = null;
private Text text2 = null;
private Text text3 = null;
private Button button1 = null;
/**
* This method initializes sShell
*/
/**
* @param args
*/
public static void main(String[] args) {
}
public static void open() {
Display display = Display.getDefault();
sc thisClass = new sc();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public String nowTime() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date(0).getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return dateFormat.format(c.getTime());
}
void createSShell() {
sShell = new Shell();
sShell.setText("Shell");
sShell.setSize(new Point(561, 368));
sShell.setLayout(null);
button = new Button(sShell, SWT.NONE);
button.setBounds(new Rectangle(276, 251, 82, 41));
button.setText("上传照片");
label = new Label(sShell, SWT.NONE);
label.setBounds(new Rectangle(80, 224, 118, 102));
label.setText("Label");
label1 = new Label(sShell, SWT.NONE);
label1.setBounds(new Rectangle(45, 167, 90, 33));
label1.setText("相册名");
label2 = new Label(sShell, SWT.NONE);
label2.setBounds(new Rectangle(44, 30, 94, 34));
label2.setText("相片名");
label3 = new Label(sShell, SWT.NONE);
label3.setBounds(new Rectangle(45, 74, 86, 31));
label3.setText("上传路径");
label4 = new Label(sShell, SWT.NONE);
label4.setBounds(new Rectangle(45, 125, 82, 34));
label4.setText("上传时间");
text = new Text(sShell, SWT.BORDER);
text.setBounds(new Rectangle(246, 26, 137, 34));
text1 = new Text(sShell, SWT.BORDER);
text1.setBounds(new Rectangle(245, 79, 132, 30));
text2 = new Text(sShell, SWT.BORDER);
text2.setBounds(new Rectangle(247, 123, 136, 32));
text3 = new Text(sShell, SWT.BORDER);
text3.setBounds(new Rectangle(249, 166, 136, 34));
button1 = new Button(sShell, SWT.NONE);
button1.setBounds(new Rectangle(395, 256, 92, 34));
button1.setText("保存");
button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=dz";
String userName = "sa";
String userPwd = "123456";
Connection dbConn = null;
String zh = text.getText();
String bb = text1.getText();
String nn = text2.getText();
String mm = text3.getText();
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName,
userPwd);
Statement stmt = dbConn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql;
Variable variable = new Variable();
sql = "insert into xpxx(xpm,xplj,username,data,xpms,xcm) values('"
+ zh
+ "','"
+ bb
+ "','"
+ variable.getUsername()
+ "','" + nn + "','','" + mm + "') ";
int rs = stmt.executeUpdate(sql);
if (rs != 0) {
JOptionPane.showMessageDialog(null, "成功!");
variable.setPhotoname(zh);
}
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("widgetSelected()"); // TODO Auto-generated
// Event stub
// widgetSelected()
}
});
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
FileDialog fDialog = new FileDialog(sShell, SWT.SAVE);
fDialog.setFilterExtensions(new String[] { "*.jpg", "*.JPG" });
fDialog.setFilterNames(new String[] { "jpeg文件(*.jpg)",
"JPEG文件(*.JPG)" });
String picName = fDialog.open();
String saveName = "xfw";
try {
FileInputStream fis = new FileInputStream(picName);
FileOutputStream fos = new FileOutputStream("c:/"
+ saveName + ".jpg");
int b = fis.read();
while (b != -1) {
fos.write(b);
b = fis.read();
}
fos.close();
fis.close();
Display display = Display.getDefault();
label.setImage(new Image(display, "c:/xfw.jpg"));
sShell.layout();
text.setText("xfw.jpg");
text1.setText("c:/xfw.jpg");
text2.setText(nowTime());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
}
// TODO Auto-generated Event stub widgetSelected()
}
});
}
}