标签:style blog io color os ar java for sp
import java.awt.event.*; import acm.graphics.*; import acm.program.*; import stanford.facepamphlet.*; import javax.swing.*; public class FacePamphlet extends GraphicsProgram implements FPConstants { /* Initializes the application */ public void init() { repository = new FPRepositoryStub("eroberts","Eric Roberts"); currentVisitingPerson="Eric Roberts"; AvatarName="Eric Roberts.jpg"; currentStatus="Haven‘t say anything yet..."; initCenter(); initWestPanel(); initEastPanel(); addActionListeners(); } /*Initializes the center of the display window */ private void initCenter(){ removeAll(); Name=new GLabel(currentVisitingPerson); Name.setFont(PROFILE_NAME_FONT); add(Name,LEFT_MARGIN,TOP_MARGIN+Name.getAscent()); Avatar=new GImage(AvatarName); if(Avatar.getWidth()>APPLICATION_WIDTH-2*LEFT_MARGIN){ Avatar.scale((APPLICATION_WIDTH-2*LEFT_MARGIN)/Avatar.getWidth(),IMAGE_HEIGHT/Avatar.getHeight()); } if(Avatar.getWidth()<=APPLICATION_WIDTH-2*LEFT_MARGIN){ Avatar.scale(1.0,IMAGE_HEIGHT/Avatar.getHeight()); } add(Avatar,LEFT_MARGIN,TOP_MARGIN+Name.getAscent()+NAME_IMAGE_SEP); Status=new GLabel(currentStatus); Status.setFont(PROFILE_STATUS_FONT); add(Status,LEFT_MARGIN,TOP_MARGIN+Name.getAscent()+NAME_IMAGE_SEP+IMAGE_HEIGHT+IMAGE_STATUS_SEP+Status.getAscent()); } /*Initializes the west control panel*/ private void initWestPanel(){ currentUser=new JButton("Eric Roberts"); add(currentUser,WEST); add(new JLabel("Friends:"),WEST); friendList=new FPScrollableList(); add(friendList,WEST); visit=new JButton("Visit"); add(visit,WEST); add(new JLabel("Send a friend request"),WEST); sendRequest=new JTextField(); add(sendRequest,WEST); request=new JButton("Request"); add(request,WEST); add(new JLabel("Pending friend request:"),WEST); requestList=new FPScrollableList(); add(requestList,WEST); accept=new JButton("accept"); add(accept,WEST); reject=new JButton("reject"); add(reject,WEST); add(new JLabel("Status:"),WEST); updateStatus=new JTextField(); add(updateStatus,WEST); changeStatus=new JButton("Chang Status"); add(changeStatus,WEST); add(new JLabel("Image"),WEST); imageFileName=new JTextField(); add(imageFileName,WEST); changeImage=new JButton("Change Image"); add(changeImage,WEST); friendList.addActionListener(this); friendList.setActionCommand("Visit"); requestList.addActionListener(this); requestList.setActionCommand("Accept"); sendRequest.addActionListener(this); sendRequest.setActionCommand("Request"); updateStatus.addActionListener(this); updateStatus.setActionCommand("Change Status"); changeImage.addActionListener(this); changeImage.setActionCommand("Change Image"); } /*Initializes the east control panel*/ private void initEastPanel(){ whosWall=new JLabel("",JLabel.CENTER); add(whosWall,EAST); contentOfWall=new FPScrollableTextArea(); add(contentOfWall,EAST); add(new JLabel("Message Area",JLabel.CENTER),EAST); contentToSend=new FPScrollableTextArea(); add(contentToSend,EAST); contentToSend.setEditable(true); send=new JButton("Send"); add(send,EAST); } public void actionPerformed(ActionEvent e){ if(changeStatus==e.getSource()){ String content=updateStatus.getText(); changeMyStatus(content); } if(visit==e.getSource()){ String friend=friendList.getSelectedName(); visitFriend(friend); currentVisitingPerson=friend; } if(changeImage==e.getSource()){ String image=imageFileName.getText(); changeImage(image); } if(accept==e.getSource()){ String name=requestList.getSelectedName(); acceptFriend(name); } if(reject==e.getSource()){ String name=requestList.getSelectedName(); rejectFriend(name); } if(request==e.getSource()){ String name=sendRequest.getText(); sendaRequest(name); } if(send==e.getSource()){ String content=contentToSend.getText(); String name=currentVisitingPerson; leaveAMessageToSomeone(content,name); } } public void leaveAMessageToSomeone(String content,String name){ println("I send this: "+content+" to "+name); } public void sendaRequest(String name){ println("I am going to ask "+name+" to be my friend"); } public void rejectFriend(String name){ println("I am going to reject "+name+"‘s request"); } public void acceptFriend(String name){ println("I am going to accept "+name+" as my friend"); } public void changeImage(String image){ repository.setProperty(IMAGE_KEY, image); println("image = " + repository.getProperty(IMAGE_KEY)); Avatar.setImage(repository.getProperty(IMAGE_KEY)); initCenter(); } public void visitFriend(String friend){ println("I am going to visit " + friend); } public void changeMyStatus(String newStatus) { repository.setProperty(STATUS_KEY, newStatus); println("status = " + repository.getProperty(STATUS_KEY)); Status.setLabel( repository.getProperty(STATUS_KEY)); } /* Instance variables */ private FPRepository repository; private String currentVisitingPerson; /* Instance variables of west control panel*/ private JButton currentUser; private FPScrollableList friendList; private JButton visit; private JTextField sendRequest; private JButton request; private FPScrollableList requestList; private JButton accept; private JButton reject; private JTextField updateStatus; private JButton changeStatus; private JTextField imageFileName; private JButton changeImage; /* Instance variables of east control panel*/ private JLabel whosWall; private FPScrollableTextArea contentOfWall; private FPScrollableTextArea contentToSend; private JButton send; /* Instance variables of center graphical display */ private String currentStatus; private String AvatarName; private GLabel Name; private GImage Avatar; private GLabel Status; private GLabel ErorrMessage; }
标签:style blog io color os ar java for sp
原文地址:http://www.cnblogs.com/livingisgood/p/4058730.html