我写了下面的java程序,但我似乎无法让它做我想做的事情。我想在java中制作两个盒子。在两个盒子里都应该有一个绿色的盒子,里面包含一个人的名字,另一个盒子和一个情绪盒子,当情绪高兴的时候它会变成红色,否则它是灰色的。像这样的事情,我需要让我的Facebook_Graphics.java做在Java程序中修改图形?
我写了下面的类,但我我可以做什么。
import java.awt.*;
import jpb.*;
public class Facebook_Graphics{
private String name;
private String content;
DrawableFrame df;
private Graphics g;
public Facebook_Graphics(String nm){
content = "undefined";
name = nm;
// Create drawable frame
df = new DrawableFrame(name);
df.show();
df.setSize(200, 150);
// Obtain graphics context
g = df.getGraphicsContext();
// display name
g.drawString(name+"'s mood is undefined.", 20, 75);
// Repaint frame
df.repaint();
}
public void setContent(String newContent){
content = newContent;
if(content.equals("happy")){
g.setColor(Color.red);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
// display mood
g.drawString(name+"'s mood is:"+ "happy", 20, 75);
}
else{
g.setColor(Color.white);
g.fillRect(0, 0, 200, 150);
g.setColor(Color.black);
g.drawString(name+"'s mood is:"+ content, 20, 75);
}
// Repaint frame
df.repaint();
}
public String getContent(){
return content;
}
}
public class FacebookPerson_Graphics{
private String myName;
private String myMood;
private Facebook_Graphics myfacebook;
public FacebookPerson_Graphics(String name){
myName = name;
myfacebook = new Facebook_Graphics(myName);
}
public String getName(){
return myName;
}
public void setMood(String newMood){
myMood = newMood;
myfacebook.setContent(myMood);
}
public String getMood(){
return myMood;
}
}
import jpb.*;
@SuppressWarnings("deprecation")
public class testFacebook_Graphics{
public static void main (String[] args){
// Prompt user to enter the number of facebookpresons
SimpleIO.prompt("Enter the number of facebookpresons to be created: ");
String userInput = SimpleIO.readLine();
int numP = Integer.parseInt(userInput);
FacebookPerson_Graphics[] fbp = new FacebookPerson_Graphics[numP];
//Ask the user to enter the name for each person, and create the persons
for(int i=0; i< numP; i++){
SimpleIO.prompt("Enter the name for person "+ (i+1)+ ":");
String name = SimpleIO.readLine();
fbp[i] = new FacebookPerson_Graphics(name);
}
System.out.println("-------select a person and type the mood below--------");
//Ask the user to set the mood for a person, and update the mood, enter "####" to exit
while(true){
SimpleIO.prompt("Enter the name for a person (enter #### to exit):");
String name = SimpleIO.readLine();
if(name.equals("####"))
System.exit(0);
int personID = -1;
for(int i=0; i< numP; i++){
if(fbp[i].getName().equals(name)){
personID = i;
break;
}
}
if(personID!=-1){ // found the person
SimpleIO.prompt("Enter the mood for the person:");
String mood = SimpleIO.readLine();
fbp[personID].setMood(mood);
}
else
System.out.println("unrecognized name!");
} // end while
} // end main
}
什么问题呢? – atomman
哦,我的问题是,我试图让它看起来像上面的图像。但是我的程序在运行时输入了一个名称,它的名称在盒子中间,而开心的是红色的,但我不确定如何使它看起来像上面那样。 –
我不知道如何让我的Face_bookGraphics类做到这一点。\ –