标签:private public package null import
package com.practice;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.exercise.dbo.DBManage;
/**
*
*功能:事件应答操作
* @author dreamer
*DATE 2016年4月28日上午12:00:19
*
*/
public class MainFrame extends JFrame{
private String currentId=null;
private JLabel[] lblFriends =null;
private JLabel lblQQId1 = new JLabel("QQ号码:");
public MainFrame(String currentId){
if(currentId==null){
this.currentId="10000";
}else{
this.currentId=currentId;
}
//默认关闭窗体
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置标题
this.setTitle("QQ登录");
//设置大小
this.setSize(300, 600);
//设置窗体居中
setCenterLocation();
//显示窗体
this.setVisible(true);
//设置窗体用户名
setCurrTilt();
//显示好友信息
setFriends();
}
private void setCurrTilt() {
String sql =String.format("select * from Users where id=%s",this.currentId);
ResultSet rs = DBManage.myexecuteQuery(sql);
try {
if(rs.next()){
this.setTitle(rs.getInt("id")+":"+rs.getString("NickName"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据连接
DBManage.CloseConnection();
}
}
private void setFriends(){
JPanel contentPane =(JPanel) getContentPane();
contentPane.setLayout(null);
contentPane.setLayout(null);
String sql =String.format
("select * from Users where id in(select FriendID from friends Where HostId=%s) ;"
,this.currentId);
ResultSet rs= DBManage.myexecuteQuery(sql);
try {
List <String> friendList = new ArrayList<String>();
while(rs.next()){
int id = rs.getInt("id");
String nickName = rs.getString("NickName");
String strFaceId =rs.getString("faceId");
//判断是否为空
if(strFaceId.isEmpty()){
strFaceId="1";
}
StringBuffer sb=new StringBuffer();
sb.append(id);
sb.append(",");
sb.append(nickName);
sb.append(",");
sb.append(strFaceId);
//添加到数据集合
friendList.add(sb.toString());
}
//为集合指定长度
lblFriends=new JLabel[friendList.size()];
for(int i=0;i<lblFriends.length;i++){
lblFriends[i]=new JLabel(friendList.get(i));
if(i==0){
lblFriends[i].setBounds(30,40,150,100);
}else{
lblFriends[i].setBounds(30,lblFriends[i-1].getY()+40,150,100);
}
String faceId = friendList.get(i).split(",")[2];
//图片路径
String imgUrl = String.format("images/%s.jpg",faceId);
lblFriends[i].setIcon(new ImageIcon(imgUrl));
lblFriends[i].setIconTextGap(JLabel.BOTTOM);
contentPane.add(lblFriends[i]);
}
lblQQId1.setIcon(new ImageIcon("images/1-1.jpg"));
lblQQId1.setIconTextGap(JLabel.BOTTOM);
lblQQId1.setBounds(10, 5, 80, 50);
contentPane.add(lblQQId1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据连接
DBManage.CloseConnection();
}
}
/**
* 设置窗体居中
*/
private void setCenterLocation(){
this.setLocation(
((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-this.getWidth())/2,
((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-this.getHeight())/2
);
}
}
JAVA界面好难控制呀,写着写着就不知道该怎么继续了........
标签:private public package null import
原文地址:http://11529455.blog.51cto.com/11519455/1768508