mercredi 23 septembre 2015

Creating Buttons in JFrame

I'm trying to write a code that adds a student and will display all their information. Also I want it to display 10 times what the student is up to using the method whatIsUp(). I am not sure how to get my array onto the button.

app.java

public class  app
{
  public static void main(String args[])
  {
    myJFrame mjf = new myJFrame();
    student student1 = new student ("John", "Smith");

  }
}

myJFrame.java

import java.awt.*;
import javax.swing.*;

public class myJFrame extends JFrame
{
    public myJFrame ()
    {
        super ("My Frame");
//------------------------------------------------------
// Create components: Jpanel
        myPanel mjp = new myPanel();
        getContentPane().add(mjp,"Center");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize (700, 600);
        setVisible(true);
    }

}

myPanel.java

import java.awt.*;
import javax.swing.*;
import static javax.swing.UIManager.get;

public class myPanel extends JPanel {
    public myPanel ()
    {
       setBackground(new Color(200, 190, 255));
       JButton b1 = new JButton(get.whatsUp1());
       b1.setBackground(Color.green);
       add(b1);
       JButton b2 = new JButton(get.whatsUp1());
       b2.setBackground(Color.yellow);
       add(b2);
       JButton b3 = new JButton(get.whatsUp1());
       b3.setBackground(Color.magenta);
       add(b3);
       JButton b4 = new JButton(get.whatsUp1());
       b4.setBackground(Color.magenta);
       add(b4);
       JButton b5 = new JButton(get.whatsUp1());
       b5.setBackground(Color.magenta);
       add(b5);
       JButton b6 = new JButton(get.whatsUp1());
       b6.setBackground(Color.magenta);
       add(b6);
       JButton b7 = new JButton(get.whatsUp1());
       b7.setBackground(Color.magenta);
       add(b7);
       JButton b8 = new JButton(get.whatsUp1());
       b8.setBackground(Color.magenta);
       add(b8);
       JButton b9 = new JButton(get.whatsUp1());
       b9.setBackground(Color.magenta);
       add(b9);
       JButton b10 = new JButton(get.whatsUp1());
       b10.setBackground(Color.magenta);
       add(b10);

}
}

And my student.java

public class student {
    String firstName;
    String lastName;
    String[] whatsUp = {"walking","sleeping","in class","studying"};


    student(String myFirstName, String myLastName)
    {
    firstName = myFirstName;
    lastName= myLastName;
    }

    String getName()
    {
    return firstName + " " +lastName;
    }



    //--------METHODS---------
        String getInfo()
        {
        return "Name = " + firstName + " " + lastName;

        }

        String whatsUp1()
        {
        double rn = Math.random();
        double rn_max5 = rn * 4;
        int ri_max5 = (int) rn_max5;
        return whatsUp[ri_max5];




}
}




Aucun commentaire:

Enregistrer un commentaire