1

我编写了以下 java 程序,但我似乎无法让它做我想做的事。我想用java做两个盒子。在这两个框中应该有一个包含人名和另一个框的绿色框和一个情绪框,当心情愉快时变为红色,否则为灰色。这样的事情我需要让我的 Facebook_Graphics.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

    }
4

1 回答 1

1

将您的课程更改Facebook_Graphics为以下内容:

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();
        drawLayout();
        df.repaint();
    }

    public void setContent(String newContent) {
        content = newContent;

        clearGraphics();
        drawLayout();

        // Repaint frame
        df.repaint();
    }
    private void clearGraphics() {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 200, 150);
    }
    private void drawLayout() {
        g.setColor(Color.BLACK);
        g.drawString("Name", 20, 40);
        g.drawString("Mood", 20, 90);

        g.setColor(Color.GREEN);
        g.fillRect(80, 20, 100, 30);

        g.setColor(getMoodColor());
        g.fillRect(80, 70, 100, 30);

        g.setColor(Color.BLACK);
        g.drawString(name, 90, 40);
        g.drawString(content, 90, 90);
    }

    private Color getMoodColor() {
        return "happy".equals(content) ? Color.RED : Color.GRAY;
    }

    public String getContent() {
        return content;
    }
}
于 2013-11-01T20:41:07.143 回答