Basic button tracking
Deliverables
Updated files for
app6.java
myJFrame6.java
myJPanel6.java
student.java
Contents
You can start with the files available here.
public class app6
{
public static void main(String args[])
{
myJFrame6 mjf = new myJFrame6();
}
}
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
{
JButton b1,b2;
public myJPanel6()
{
setLayout(new
GridLayout(1,1));
//=====================================
student st1 = new
student("DaeSean", "Hamilton", 20);
//=====================================
b1 = new
JButton(st1.getName());
add(b1);
//=====================================
b2 = new
JButton(st1.WhatIsUp());
add(b2);
}
}
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;
}
//--------------------------------------------
}
An example of how to add images to buttons is available here.
public class app
{
public static void main(String args[])
{
myJFrame mjf = new myJFrame();
}
}
import java.awt.*;
import javax.swing.*;
public class myJFrame extends JFrame
{
myJPanelstd mjp;
public myJFrame ()
{
super ("My First Frame");
//------------------------------------------------------
// Create components
mjp = new myJPanelstd();
//------------------------------------------------------
// Choose a Layout for JFrame and
// add Jpanel to JFrame according to layout
getContentPane().setLayout(new
BorderLayout());
getContentPane().add(mjp,"Center");
//------------------------------------------------------
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (800, 700);
setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class myJPanelstd extends JPanel
{
public myJPanelstd()
{
setBackground(Color.pink);
setLayout(new GridLayout(3,1));
JButton jb1, jb2,
jb3,jb4;
ImageIcon
imageFred = new ImageIcon("images/fred.jpg");
jb1 = new JButton(imageFred);
//JButton with image only
jb1.setText("Fred");// adds a text to an existing button
jb2 = new JButton("is a fan of .....");
ImageIcon imageRon = new ImageIcon("images/r10.jpg");//creates the
image to be used in a JButton
jb3 = new
JButton("Ronaldinho");
jb3.setIcon(imageRon); //
adds an image to an existing button
//-------------------------------------------------------
// add buttons to JPanel
//-------------------------------------------------------
add(jb1);
add(jb2);
add(jb3);
}
}
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 will show a different what’sUp in the second button.
Do it in 2 parts
Just show the what'sUp as a text
Then show it graphically with a different picture for each
what'sUp() result.
Hello, I have a solution for you. Implemented everything as per the requirements.
myJPanel.java (part1)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel6 extends JPanel {
JButton b1, b2;
public myJPanel6() {
setLayout(new GridLayout(1, 1));
// =====================================
final student st1 = new student("DaeSean", "Hamilton", 20);
// =====================================
b1 = new JButton(st1.getName());
/**
* Setting background color of first button to red
*/
b1.setBackground(Color.RED);
add(b1);
/**
* Defining a button with a random whatisup message
*/
b2=new JButton(st1.WhatIsUp());
/**
* Setting background color to RED
*/
b2.setBackground(Color.RED);
add(b2);
/**
* Adding action listener for b1
*/
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/**
* Setting a random whatisup message in button b2 everytime
* button b1 is clicked (Answer for first part)
*/
b2.setText(st1.WhatIsUp());
}
});
}
}
myJPanel.java (part2)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class myJPanel6 extends JPanel {
JButton b1, b2;
String whatisUp;
ImageIcon readingIcon, interactingIcon, talkingIcon;
public myJPanel6() {
/**
* Defining icons for each what is up messages
*/
readingIcon = new ImageIcon("reading.jpg");
interactingIcon = new ImageIcon("interacting.jpg");
talkingIcon = new ImageIcon("talking.jpg");
setLayout(new GridLayout(1, 1));
// =====================================
final student st1 = new student("DaeSean", "Hamilton", 20);
// =====================================
b1 = new JButton(st1.getName());
/**
* Setting background color of first button to red
*/
b1.setBackground(Color.RED);
add(b1);
/**
* getting a random whatisup message
*/
whatisUp = st1.WhatIsUp();
/**
* Defining a button with the received whatisup message
*/
b2=new JButton(whatisUp);
/**
* Setting background color to RED
*/
b2.setBackground(Color.RED);
/**
* calling method to update the icon of the button, with the current whatisup activity
*/
setImage(whatisUp);
add(b2);
/**
* Adding action listener for b1
*/
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/**
* Setting a random whatisup message in button b2 everytime
* button b1 is clicked (Answer for first part)
*/
whatisUp = st1.WhatIsUp();
b2.setText(whatisUp);
/**
* Updating the button icon (Answer for part2)
*/
setImage(whatisUp);
}
});
}
/**
* method to update the second button's icon based on given string whatisup
* message
*/
public void setImage(String whatIsUp) {
if (whatisUp.equalsIgnoreCase("reading")) {
b2.setIcon(readingIcon);
} else if (whatisUp.equalsIgnoreCase("talking")) {
b2.setIcon(talkingIcon);
} else {
b2.setIcon(interactingIcon);
}
}
}
//reading.jpg
//interacting.jpg
//talking.jpg
/*OUTPUT*/
Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...
Deliverables app.java, myJFrame.java, myJPanel.java and student.java as requested below. (Use the student.java class that you already have from previous labs) Contents Based on the graphics you learned this week Create an instance (an object, i.e., st1) of student in myJPanel Display his/her basic information Display for 10 times what he/she is up to (using the method whatIsUp() that your student class already has) -------------------------------------------------------------------------------------------------- \\app.java public class app { public static void main(String args[]) { myJFrame mjf = new myJFrame();...
This is the java GUI program. I want to add an icon to a button, but it shows nothing. Can you fix it for me please? import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Icon extends JFrame{ public static void main(String args[]){ Icon frame = new Icon("title"); frame.setVisible(true); } Icon(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png"); JButton button1 = new JButton(icon1); p.add(button1); Container contentPane...
Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame { public QuitIt() { startIt(); //Create everything and start the listener } public final void startIt() { //Create the JPanel JPanel myPanel =...
Any thoughts on this, i can get it to run but can't pass code around and the button doesnt move around. i didnt pass the student, or the app.java. What will you learn Combined use of Timer and the tracking of user interactions Deliverables app.java myJFrame.java myJPanel.java and other necessary Java files Contents The objective of the lab is to create a game in which the player has to click on a moving student-button to score. The student button has...
I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel { private static final int...
Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off. Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...
Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener { public static final int width = 500; public static final int height = 300; public static final int no_of_lines = 10; public static final int chars_per_line = 20; private JTextArea lan1; private JTextArea lan2; public static void main(String[] args){ translatorApp gui = new translatorApp();...
22. Fill in the codes that creates a Java GUI application that contains a label and a button. The label reads "Click the button to change the background color." When the button is clicked, the background is changed to a red color. The program produces the following output. Point 7 22.1 package javaapplication 179; import javax.swing": import java.ant": import java.awt.exst. public class ColorChange extends JFrame / not allowed to change JLabel label - new JLabel("Click the button to change the...
Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...
Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The...