📄 homemanage.java
字号:
package myprojects.HomeManage;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.custom.*;
import myprojects.DBcon.DBcon;
import myprojects.queryCharge.*;
import myprojects.AlterPwd.*;
public class HomeManage
{
private static DBcon DB=new DBcon();//连立与数据库的连接
private static Shell shell;
private Combo cmbJZDH;
private Combo cmbPlace;
private Combo cmbDetails;
private Text txtName;
private Text txtWriteTime;
private Text txtYear;
private Text txtMonth;
private Text txtDay;
private Text txtPrice;
private Text txtCount;
private Text txtItemPrice;
private Text txtMark;
private Button btnSet;
private Button btnAlter;
private Button btnQuery;
private Button btnSave;
private Button btnReset;
private Button btnSaveAll;
private Button btnDel;
private Button btnExit;
private Table table;
private Group group;
private String[] tableHead={"消费名称","消费地点","单价","数量","合计","备注"};
private int adminFlag=0;
private int queryFlag=0;
private int delFlag=0;
private String userName="郭军";
private String userID="001";
public static void main(String args[])
{
Display display=new Display();
shell=new Shell(display,SWT.MIN);
HomeManage objManage=new HomeManage();
shell.setSize(500,350);
shell.setVisible(true);
shell.setText("家庭财务管理系统V1.0(测试版)");
try
{
shell.setImage(new Image(shell.getDisplay(),"image.ico"));
}
catch(Exception e)
{
}
shell.open();
if(!DB.getConnection())
{
MessageBox msBox = new MessageBox(shell,SWT.OK|SWT.ICON_ERROR);
msBox.setText("错误");
msBox.setMessage("数据库连接出错");
msBox.open();
return;
}
objManage.creatText();
objManage.creatTable();
objManage.createGroup();
while(!objManage.shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private void creatText()
{
Label lblJZDH=new Label(shell,SWT.None);
lblJZDH.setText("记帐单号");
lblJZDH.setBounds(20,35,50,15);
cmbJZDH=new Combo(shell,SWT.BORDER);
cmbJZDH.setBounds(80,30,120,20);
cmbJZDH.setEnabled(false);
Label lblName=new Label(shell,SWT.None);
lblName.setText("用户姓名");
lblName.setBounds(210,35,50,15);
txtName=new Text(shell,SWT.BORDER);
txtName.setBounds(265,30,45,20);
txtName.setEditable(false);
btnSet=new Button(shell,SWT.None);
btnSet.setText("设置");
btnSet.setBounds(320,30,45,20);
btnAlter=new Button(shell,SWT.None);
btnAlter.setText("修改密码");
btnAlter.setBounds(365,30,55,20);
btnQuery=new Button(shell,SWT.None);
btnQuery.setText("查询");
btnQuery.setBounds(420,30,45,20);
Label lblWriteTime=new Label(shell,SWT.None);
lblWriteTime.setText("记帐时间");
lblWriteTime.setBounds(20,60,50,15);
txtWriteTime=new Text(shell,SWT.BORDER);
txtWriteTime.setBounds(80,55,120,20);
txtWriteTime.setText(DB.getNowDate());
txtWriteTime.setEditable(false);
Label lblUseTime=new Label(shell,SWT.None);
lblUseTime.setText("消费时间:");
lblUseTime.setBounds(210,60,55,15);
txtYear=new Text(shell,SWT.BORDER);
txtYear.setBounds(270,55,30,20);
Label lblYear=new Label(shell,SWT.None);
lblYear.setText("年");
lblYear.setBounds(305,60,20,20);
txtMonth=new Text(shell,SWT.BORDER);
txtMonth.setBounds(325,55,20,20);
Label lblMonth=new Label(shell,SWT.None);
lblMonth.setText("月");
lblMonth.setBounds(350,60,20,20);
txtDay=new Text(shell,SWT.BORDER);
txtDay.setBounds(370,55,20,20);
Label lblDay=new Label(shell,SWT.None);
lblDay.setText("日");
lblDay.setBounds(395,60,20,20);
btnExit=new Button(shell,SWT.None);
btnExit.setText("退出");
btnExit.setBounds(420,55,45,20);
cmbJZDH.setText(creatJZDH());
txtYear.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent eYear)
{
if(eYear.keyCode==13)
{
if(txtYear.getText().length()!=0)
{
try
{
if(Integer.parseInt(txtYear.getText())>2100|Integer.parseInt(txtYear.getText())<2000)
{
txtYear.setText("");
showMessage("错误!","年只能在2000-2100年之间!",SWT.ICON_INFORMATION);
}
else
txtMonth.setFocus();
}
catch(Exception e)
{
txtYear.setText("");
showMessage("错误!","请输入正确的年份!",SWT.ICON_INFORMATION);
}
}
else
{
showMessage("错误!","年份不能为空!",SWT.ICON_INFORMATION);
}
}
}
});
txtMonth.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent eMonth)
{
if(eMonth.keyCode==13)
{
if(txtMonth.getText().length()!=0)
{
try
{
if(Integer.parseInt(txtMonth.getText())>12|Integer.parseInt(txtMonth.getText())<1)
{
txtMonth.setText("");
showMessage("错误!","输入的月不能小于1或大于12",SWT.ICON_INFORMATION);
}
else
txtDay.setFocus();
}
catch(Exception eM)
{
txtMonth.setText("");
showMessage("错误!","请输入正确的月份!",SWT.ICON_INFORMATION);
}
}
else
{
txtMonth.setText("");
showMessage("错误!","月份不能为空!",SWT.ICON_INFORMATION);
}
}
}
});
txtDay.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent eDay)
{
if(eDay.keyCode==13)
{
if(txtDay.getText().length()!=0)
{
try
{
if(Integer.parseInt(txtDay.getText())<1|Integer.parseInt(txtDay.getText())>31)
{
txtDay.setText("");
showMessage("错误!","日期必须在1到31之间!",SWT.ICON_INFORMATION);
}
else
cmbDetails.setEnabled(true);
cmbDetails.setFocus();
cmbDetails.removeAll();
getDetails();
}
catch(Exception eM)
{
txtDay.setText("");
showMessage("错误!","请输入正确的日期!",SWT.ICON_INFORMATION);
}
}
else
{
txtDay.setText("");
showMessage("错误!","日期不能为空!",SWT.ICON_INFORMATION);
}
}
}
});
btnExit.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent set)
{
int flag=showMessage("敬告!","你真的要退出此系统吗?(Y/N)",SWT.OK|SWT.CANCEL|SWT.ICON_INFORMATION);
if(flag==32)
{
shell.dispose();
}
}
});
btnQuery.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent set)
{
shell.getDisplay().dispose();
shell.dispose();
String[] toQuery=null;
queryCharge.main(toQuery);
}
});
btnAlter.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent set)
{
shell.getDisplay().dispose();
shell.dispose();
String[] toAlter=null;
AlterPwd.main(toAlter);
}
});
}
private void creatTable()
{
table=new Table(shell,SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setBounds(20,80,450,130);
TableColumn[] column=new TableColumn[tableHead.length];
for(int i=0;i<tableHead.length;i++)
{
column[i]=new TableColumn(table,SWT.LEFT);
column[i].setText(tableHead[i]);
}
column[0].setWidth(100);
column[1].setWidth(100);
column[2].setWidth(50);
column[3].setWidth(50);
column[4].setWidth(50);
column[5].setWidth(100);
table.addSelectionListener(new SelectionAdapter(){
public void widgetDefaultSelected(SelectionEvent select)
{
TableItem itemSelect=table.getSelection()[0];
tableToText(itemSelect);
btnSave.setText("修改");
System.out.println("查看的列数为"+itemSelect);
}
});
}
private void createGroup()
{
group=new Group(shell,SWT.None);
group.setBounds(20,210,450,95);
Label lblDtails=new Label(group,SWT.None);
lblDtails.setText("消费项目");
lblDtails.setBounds(5,20,50,15);
cmbDetails=new Combo(group,SWT.BORDER);
cmbDetails.setBounds(60,15,120,20);
Label lblPlace=new Label(group,SWT.None);
lblPlace.setText("消费地点");
lblPlace.setBounds(190,20,50,15);
cmbPlace=new Combo(group,SWT.BORDER);
cmbPlace.setBounds(250,15,120,20);
Label lblPrice=new Label(group,SWT.None);
lblPrice.setText("消费单价");
lblPrice.setBounds(5,45,50,15);
txtPrice=new Text(group,SWT.BORDER);
txtPrice.setBounds(60,40,60,20);
Label lblCount=new Label(group,SWT.None);
lblCount.setText("消费数量");
lblCount.setBounds(130,45,50,15);
txtCount=new Text(group,SWT.BORDER);
txtCount.setBounds(185,40,60,20);
Label lblAll=new Label(group,SWT.None);
lblAll.setText("价格合计");
lblAll.setBounds(255,45,50,15);
txtItemPrice=new Text(group,SWT.BORDER);
txtItemPrice.setBounds(310,40,60,20);
Label lblMark=new Label(group,SWT.None);
lblMark.setText("消费备注");
lblMark.setBounds(5,70,50,15);
txtMark=new Text(group,SWT.BORDER);
txtMark.setBounds(60,65,185,20);
btnSave=new Button(group,SWT.None);
btnSave.setText("加入");
btnSave.setBounds(255,65,55,20);
btnSave.setEnabled(false);
btnReset=new Button(group,SWT.None);
btnReset.setText("重填");
btnReset.setBounds(315,65,55,20);
btnDel=new Button(group,SWT.None);
btnDel.setText("删除记录");
btnDel.setBounds(380,20,60,25);
btnDel.setEnabled(false);
btnSaveAll=new Button(group,SWT.None);
btnSaveAll.setText("保存记录");
btnSaveAll.setBounds(380,50,60,25);
btnSaveAll.setEnabled(false);
txtYear.setFocus();
setDisableGroup();//设置相应的控件不可能!
cmbDetails.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent eve)
{
int flag = checkValidate(cmbDetails.getText());
if(flag==0)
{
showMessage("错误!","消费项目中有不法字符!(如单双引号)",SWT.ICON_INFORMATION);
cmbDetails.setText("");
cmbPlace.setEnabled(false);
}
else
{
cmbPlace.setEnabled(true);
}
return;
}
});
cmbDetails.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent Details)
{
if(Details.keyCode==SWT.CR)
{
if(cmbDetails.getText().length()==0)
{
showMessage("错误!","消费项目不能为空!",SWT.ICON_INFORMATION);
}
else
{
cmbPlace.setEnabled(true);
btnReset.setEnabled(true);
cmbPlace.setFocus();
cmbPlace.removeAll();
getPlace();
}
}
}
});
cmbPlace.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent eve)
{
int flag = checkValidate(cmbPlace.getText());
if(flag==0)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -