我写了一个简单的,自己改改,要睡觉了。
package qq;
public class Friend {
//昵称
private String displayName;
//唯一标识
private int id;
//是否在线
private boolean isOnLine;
public Friend(String displayName, int id, boolean isOnLine) {
this.displayName = displayName;
this.id = id;
this.isOnLine = isOnLine;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isOnLine() {
return isOnLine;
}
public void setOnLine(boolean isOnLine) {
this.isOnLine = isOnLine;
}
}
package qq;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FriendListPnl extends JPanel {
private List<Friend> friends;
private JButton[] friendList;
public FriendListPnl(List<Friend> friends) {
this.friends = friends;
initFriendList();
initGUI();
}
private void initGUI()
{
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1.0;
constraints.gridx = 0;
constraints.ipadx = 1;
constraints.ipady = 1;
constraints.anchor = GridBagConstraints.NORTHWEST;
for (int i = 0; i < friendList.length; i++) {
constraints.gridy = i;
if (i == friendList.length - 1)
{
constraints.weighty = 1.0;
}
this.add(friendList[i], constraints);
}
}
private void initFriendList()
{
friendList = new JButton[friends.size()];
Dimension d = new Dimension(100, 22);
JButton friend = null;
Friend friendInfo = null;
for (int i = 0; i < friendList.length; i++) {
friend = new JButton();
friend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "您好!");
}
});
friend.setBorder(null);
friendInfo = friends.get(i);
friend.setText(new StringBuffer().append(friendInfo.getDisplayName())
.append("(").append(friendInfo.getId()).append(")").toString());
friend.setPreferredSize(d);
friend.setEnabled(friendInfo.isOnLine());
friendList[i] = friend;
}
}
}
package qq;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class TestFrame {
public static void main(String[] args) {
Friend f1 = new Friend("天下", 22, true);
Friend f2 = new Friend("倚天", 21, false);
Friend f3 = new Friend("屠龙", 20, true);
List<Friend> list = new ArrayList<Friend>();
list.add(f1);
list.add(f2);
list.add(f3);
JFrame frame = new JFrame("QQ");
frame.setSize(150, 350);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(new Point(d.width - frame.getWidth() - 25, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new FriendListPnl(list)));
frame.setVisible(true);
}
}