首先是界面设计要点:
~~~~JButton:
Dimension preferredSize=new Dimension(150,40);//改变按钮大小
button.setPreferredSize(preferredSize);
~~~~JLabel:
JLabel label=new JLabel("月份",JLabel.CENTER);//设置文字居中
label.setFont(new Font("宋体",Font.PLAIN,16));//设置字体
label.setBounds(0,0,1200,670);//设置位置及大小
label.setForeground(Color.red);//设置颜色
ImageIcon icon=new ImageIcon(D:/Eclipse/cat.jpg);//一般用绝对路径好,src/cat.jpg,转出exe时图片不显示
label.setIcon(icon);//标签显示图片
~~~~JTable//JTabel table = new JTabel();
table.setRowHeight(50);//指定每一行的 行高为50
table.setRowHeight(2,30);//指定第2行的高度为30
table.getTableHeader().setFont(new Font("Dialog",0,19));//设置标题字体及大小
table.setFont(new Font("Dialog",0,19))//设置表格字体及大小
for(int i=0;i<11;i++){
TableColumn column=table.getColumnModel().getColumn(i);
column.setPreferredWidth(80);//指定所有列宽为80
if(i<6){column.setPreferredWidth(80)}//指定前6列列宽为80
}
String[] Names={};
String[][]data={}
DefaultTableModel tablemodel=new DefaultTableModel(data,Names);
JTable table=new JTable(tabelmodel);
~~~~布局管理器
1、GridBagLayout,方法
public void addg(Component c,GridBagConstraints constraints,int x,int y,int w,int h) {
//此方法用来添加控件到容器中
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
add(c,constraints);
}
GridBagConstraints constrains=new GridBagConstraints();
constrains.fill=GridBagConstraints.NONE; //这个可以保证大小一致
constrains.anchor=GridBagConstraints.WEST; //左对齐
constrains.weightx=2; //这两行是关键,如果不设置,表示组件大小固定,不管面板如何变,
constrains.weighty=2;
panel.addg(jlmonth,constrains,0,0,1,1);
2、flowLayout
panel.setLayout(new FlowLayout(3,10,10));//3列,间隔10
panel.add(label1);panel.add(text1);panel.add(label11);
jf.getContentPane().add(panel);
_______________________________________________________________________________________
其次是数据库的增加,删除,修改。
~~~~进入数据库步骤:1、进入命令行输入D: 2、输入cd Program Files\mysql-5.5.62-winx64\bin
3、输入net start mysql 4、输入mysql -uroot -p 5、输入密码
6、修改密码方法mysql> set password for 用户名@localhost = password('新密码');
mysql> set password for root@localhost = password('123');
7、查看数据库show databases;------use test;-----show tables;-----select * from student;
~~~~连接数据库方法Connection conn=getConn();
public static Connection getConn() {
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","密码");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
~~~~~将数据插入数据库
PreparedStatement ps=null;
String sql = "INSERT INTO account(month,zhaoh,jianh,taobao,cash,stock,asset,remarks) "+"values(?,?,?,?,?,?,?,?)";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, jtmonth.getText());ps.setString(2, jtzhaoh.getText());
ps.setString(3, jtjianh.getText());ps.setString(4, jttaobao.getText());
ps.setString(5, jtcash.getText());ps.setString(6, jtstock.getText());
int sum=Integer.parseInt(jtzhaoh.getText())+...+Integer.parseInt(jtstock.getText());
ps.setString(7, String.valueOf(sum));ps.setString(8, jtremarks.getText());
//总资金为2到6相加,先相加存到数据库中,
ps.executeUpdate();//sql必须是insert,update,delete语句,或者为空
}catch (SQLException b){
b.printStackTrace();
}finally{
try{
conn.close(); System.out.println("MySQL 关闭成功");
}catch (SQLException c){
System.out.println("MySQL 关闭失败 "); c.printStackTrace();
}
}
~~~~查询数据库输入到JTable中
try{
PreparedStatement ps = null; ps=conn.prepareStatement("SELECT * FROM account");
ResultSet res = null; res=ps.executeQuery();//执行查询
while (res.next())
{
temp= res.getString(7);//总资产需先记下,以计算资产变化
if(num==1) {
String[]row={res.getString(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getString(6),res.getString(7),
"",res.getString(8)};
tablemodel.addRow(row);
}else{
int assetchange=Integer.parseInt(res.getString(7))-
Integer.parseInt(oldtemp);
String [] row= {res.getString(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getString(6),res.getString(7),
String.valueOf(assetchange),res.getString(8)};
//资产变化,直接计算然后输入到JTable中。
tablemodel.addRow(row);
}
oldtemp=temp;System.out.println(oldtemp);//记录上月资产
}
System.out.println("load ok!");
}catch(Exception q){
q.printStackTrace(); System.out.println("go die");
}finally {
try {
res.close(); ps.close(); conn.close();
System.out.println("close ok");
}catch (SQLException o){
o.printStackTrace(); System.out.println("go die 2");
}
}
~~~~改变数据库数据
String sql = "SELECT month,zhaoh,jianh,taobao,cash,stock,asset,remarks FROM account";//8
ResultSet res=null; Statement stat=null;
stat=conn.createStatement();
res=stat.executeQuery(sql);
while(res.next())
{
if (res.getString(1).equals(jtmonth.getText())) {
String sql2="UPDATE account SET zhaoh='"+jtzhaoh.getText()+"' WHERE
month='"+jtmonth.getText()+"'";//注意此处的格式,用where设置条件,用'"+value+"'表值
...
try{
stat=conn.createStatement();
stat=executeUpdate(sql2);
...
}...
}
~~~~删除数据库数据
String sql = "DELETE FROM account WHERE month='"+jtmonth.getText()+"'";
//jtmonth中的数据为条件,删除行。
try {
stat=conn.createStatement();
stat.executeUpdate(sql);
}catch (SQLException e) {
e.printStackTrace();
}finally {
try{
stat.close();conn.close(); System.out.println("MySQL 关闭成功");
}catch (SQLException c){
System.out.println("MySQL 关闭失败 "); c.printStackTrace();
}
}