📄 friendspanel.java
字号:
package librarysearchingsystem;
import javax.swing.JPanel;
import fileUtility.Student;
import fileUtility.StudentsManagement;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import fileUtility.*;
public class FriendsPanel extends JPanel implements ActionListener {
int friendsSerial[] = null, friendsInLibSerial[] = null;
JButton friendsButton[];
static int comWidth = 200, comHeight = 40, ffpHeight = 135;
static int xHap = 5, hHap = 5;
Frame f;
static Color buttonColor = new Color(145, 138, 216);
static FriendsFuncPanel ffp = new FriendsFuncPanel();
private int width = comWidth + 2 * xHap;
private int height = 650;
public static boolean isInLib = true;
static {
ffp.setBounds(xHap, hHap, comWidth, ffpHeight);
}
public FriendsPanel() {
try {
jbInit();
draw();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public FriendsPanel(int ID, Frame f) {
this.f = f;
friendsSerial = StudentsManagement.getFriendsSerial(ID);
friendsInLibSerial = getFriendsInLib(StudentsManagement.students);
try {
jbInit();
draw();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public FriendsPanel(int ID, Frame f, boolean justInLib) {
this.f = f;
isInLib = justInLib;
friendsSerial = StudentsManagement.getFriendsSerial(ID);
friendsInLibSerial = getFriendsInLib(StudentsManagement.students);
try {
jbInit();
draw();
} catch (Exception ex) {
ex.printStackTrace();
}
}
void printFriendSerial() {
if (friendsSerial == null) {
System.out.println("好友数00000000");
return;
}
System.out.println("好友数:" + friendsSerial.length);
for (int i = 0; i < friendsSerial.length; i++) {
System.out.println(friendsSerial[i]);
}
}
private void jbInit() throws Exception {
ffp.IDTextField.setText("");
ffp.fP = this;
this.setLayout(null);
this.setSize(width, height);
setBackground(new Color(187, 180, 216));
this.setAutoscrolls(true);
}
private void draw() {
int tempFriendsSerial[];
Student students[] = StudentsManagement.students;
this.add(ffp);
//获取在图书馆的好友序列号
if (isInLib) {
tempFriendsSerial = friendsInLibSerial;
} else {
tempFriendsSerial = friendsSerial;
}
// printFriendSerial();
//
/** @todo 测试用 */
if (tempFriendsSerial == null) {
return;
}
/** @todo */
friendsButton = new JButton[tempFriendsSerial.length];
for (int i = 0; i < friendsButton.length; i++) {
Student s = students[tempFriendsSerial[i]];
if (s.readingRoom == null) {
friendsButton[i] = new JButton(s.name + " 不在图书馆");
friendsButton[i].setEnabled(false);
} else {
friendsButton[i] = new JButton(s.name + " " +
s.readingRoom.name);
friendsButton[i].addActionListener(this);
}
friendsButton[i].setBounds(xHap,
ffpHeight + 2 * hHap +
i * (hHap + comHeight),
comWidth, comHeight);
friendsButton[i].setBackground(buttonColor);
this.add(friendsButton[i]);
}
height = ffpHeight + friendsButton.length * (hHap + comHeight) + hHap;
}
private int[] getFriendsInLib(Student[] students) {
if (friendsSerial == null) {
return null;
}
int friendsInLib = 0;
int fSerial[] = new int[friendsSerial.length];
for (int i = 0; i < friendsSerial.length; i++) {
Student s = students[friendsSerial[i]];
if (s.readingRoom != null) {
fSerial[friendsInLib] = friendsSerial[i];
friendsInLib++;
}
}
int inLibSerial[] = new int[friendsInLib];
for (int i = 0; i < inLibSerial.length; i++) {
inLibSerial[i] = fSerial[i];
}
// System.out.println("inLibSerial.length=" + inLibSerial.length);
return inLibSerial;
}
// public static void main(String[] args) {
// new StudentsManagement();
// friendsPanel f = new friendsPanel(2);
// f.printFriendSerial();
// }
public static void main(String[] args) throws InterruptedException {
new StudentsManagement();
FriendsPanel f = new FriendsPanel(2, null);
f.printFriendSerial();
f.setSize(300, 900);
JFrame ff = new JFrame();
ff.setSize(800, 600);
ff.getContentPane().setLayout(null);
ff.getContentPane().add(f);
ff.setDefaultCloseOperation(ff.EXIT_ON_CLOSE);
ff.show();
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < friendsButton.length; i++) {
if (e.getSource() == friendsButton[i]) {
Student stu;
if (isInLib) {
stu = StudentsManagement.students[friendsInLibSerial[i]];
} else {
stu = StudentsManagement.students[friendsSerial[i]];
}
// System.out.println(stu.serial+" friend");
if (!isInLib) {
// Student stu = StudentsManagement.students[friendsSerial[i]];
f.changeToreadingRoom(stu.readingRoom, stu.serial,
ReadingRoomPane.friend);
} else {
// System.out.println(stu.readingRoom);
f.changeToreadingRoom(stu.readingRoom, stu.serial,
ReadingRoomPane.friend);
}
}
}
}
public boolean isFriend(int serial) {
if (friendsSerial == null) {
return false;
}
for (int i = 0; i < friendsSerial.length; i++) {
if (friendsSerial[i] == serial) {
return true;
}
}
return false;
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -