Java Lab – Tracking Buttons Deliverables Updated files for app6.java, myJFrame6.
ID: 3667731 • Letter: J
Question
Java Lab – Tracking Buttons Deliverables Updated files for app6.java, myJFrame6.java, myJPanel6.java, student.java
Create a solution that tracks the use of the button #1 (student’s name) Every time a user clicks on the first button (the button with the student’s name), you show a different what’sUp. do it in 2 parts
Part1.just show the what’s up as a text
Part2.then show it graphically with a different picture for each what’sUp( ) result. THIS IS THE PART I NEED HELP WITH:
HERE IS MY CODE SO FAR:
public class app6
{
public static void main(String args[])
{
myJFrame6 mjf = new myJFrame6();
}
}
import java.awt.*;
import javax.swing.*;
public class myJButton extends JButton
{
public myJButton(String text)
{
super(text);
setBackground(Color.red);
setOpaque(true);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJFrame6 extends JFrame
{
myJPanel6 p6;
public myJFrame6 ()
{
super ("My First Frame");
//------------------------------------------------------
// Create components: Jpanel, JLabel and JTextField
p6 = new myJPanel6();
//------------------------------------------------------
// Choose a Layout for JFrame and
// add Jpanel to JFrame according to layout
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p6, "Center");
//------------------------------------------------------
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (550, 400);
setVisible(true);
}
//-------------------------------------------------------------------
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel6 extends JPanel implements ActionListener
{
myJButton b1,b2;
public myJPanel6()
{
setLayout(new GridLayout(1,1));
//=====================================
student st1 = new student("Michael", "Robinson", 20);
//=====================================
b1 = new myJButton(st1.getName());
add(b1);
//=====================================
b2 = new myJButton(st1.WhatIsUp());
add(b2);
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
//=====================================
if (obj == b1){b2.setText("-"+b1.getText()+"- was clicked");}
}
}
class student
{
//---------Declaring attributes----
String firstName;
String lastName;
int age;
String status;
//------------------------------
//----------Constrcutor------------
public student(String a, String b, int c)
{
firstName = a;
lastName = b;
age = c;
if(age<=25) status = "Traditional";
else status = "Non-Traditional";
}
//---------------------------------
//---------- METHODS --------
String getName()
{
return("NAME = "+firstName+ " "+lastName + ", Age = "+age+", Status = "+status);
}
int getAge()
{
return age;
}
String getStatus()
{
return status;
}
String WhatIsUp()
{
String b = "dunno";
double r = Math.random();
int myNumber = (int)(r*3f);
if (myNumber == 0) b = "reading";
if (myNumber == 1) b = "talking";
if (myNumber == 2) b = "interacting";
return b;
}
//--------------------------------------------
}
and I have a folder tiitled images containging three images - talking.jpg, interacting.jpg, and reading.jpg
Explanation / Answer
Ans: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJPanel6 extends JPanel implements ActionListener { myJButton b1, b2; public myJPanel6() { setLayout(new GridLayout(1, 1)); //===================================== student st1 = new student("Michael", "Robinson", 20); //===================================== b1 = new myJButton(st1.getName()); b1.addActionListener(this); add(b1); //===================================== b2 = new myJButton(st1.WhatIsUp()); add(b2); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); if (obj == b1) { b2.setText(st1.whatIsUp()); } } } class student { String firstName; String lastName; int age; String status; public student(String a, String b, int c) { firstName = a; lastName = b; age = c; if (ageRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.