subject

Push Counter Example (pages 9-12):

//PushCounter. java

import javax. swing. JFrame;

public class PushCounter {

public static void main(String[] args) {

JFrameframe = new JFrame("PushCounter");

frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);

PushCounterPanelpanel = new PushCounterPanel();

frame. getContentPane().add(panel);

frame. pack();

frame. setVisible(true);

}

}

//PushCounterPanel. java

import java. awt.*;

import java. awt. event.*;

import javax. swing.*;

public class PushCounterPanelextends JPanel {

private intcount;

private JButtonpush;

private JLabellabel;

public PushCounterPanel() {

count = 0;

push = new JButton("Push Me!");

label = new JLabel();

push. addActionListener(newButtonListener ());

add(push);

add(label);

setBackground(Color. cyan);

setPreferredSize(newDimension(300, 40)); }

private class ButtonListenerimplements ActionListener {

public void actionPerformed(ActionEventevent){< br />
count++;

label. setText("Pushes: " + count);

}

}

}



Consider "Push Counter" example from GUI lecture slides (pages 9 – 12). Copy the code to your project and run it (note: you’ll only need 2 source files – one for main, and one for panel class; nested listener class DOES NOT require an additional source file).
Update the following example, so that it has an additional button called "Reset counter". When pressed, this button should reset the counter. Note: you may want to be able to determine the event source in your listener. To implement this, use "Determining event sources" example from the slides (pages 20 – 24), so that your listener looks similar to this:
public void actionPerformed(ActionEvent event)

{

if (event. getSource() == left)

label. setText("Left");

else

label. setText("Right");

}



Determining Event Sources Example(pages 20-24):

- The LeftRightPanel class creates one listener and applies it to both buttons.

- When either button is pressed, the actionPerformed method of the listener is invoked.

- The getSource method returns a referenced to the component that generated the event.

-We could have created two listener classes. Then the actionPerformed method would not have to determine where the event is originating.

//LeftRight. java

import javax. swing. JFrame;

public class LeftRight {

public static void main (String[] args) {

JFrame frame = new JFrame("Left Right");

frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);

frame. getContentPane().add(new LeftRightPanel());

frame. pack();

frame. setVisible(true);

}

}

//LeftRightPanel. java

import java. awt.*;

import java. awt. event.*;

import javax. swing.*;

public class LeftRightPanel extends JPanel {

private Button left, right;

private JLabel label;

private JPanel buttonPanel;

public LeftRightPanel() {

left = new JButton("Left");

right = new JButton("Right");

ButtonListener listener = new ButtonListener();

left. addActionListener(listener);

right. addActionListener(listener);

label = new JLabel("Push a button");

buttonPanel = new JPanel();

buttonPanel. setPreferredSize(new Dimension(200, 40));

buttonPanel. setBackground(Color. blue);

buttonPanel. add(left);

buttonPanel. add(right);

setPreferredSize(new Dimension(200, 80));

setBackground(Color. cyan);

add(label);

add(buttonPanel);

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

if (event. getSource() == left)

label. setText("Left");

else

label. setText("Right");

}

}

}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 02:00
Aisha has finished working on a word processing document that contains 15 pages. she has added some special elements in the first three pages, page 9 and 10, and page 15 from the document. she wants to print only these pages to see how they look. which option is the correct way to represent (in the print dialog box) the pages that aisha wants to print?
Answers: 3
question
Computers and Technology, 22.06.2019 07:00
Robots with telescoping arms are sometimes used to perform tasks (e.g., welding or placing screws) where access may be difficult for other robotic types. during a test run, a robot arm is programmed to extend according to the relationship r = 3 + 0.5cos(4θ) and the arm rotates according to the relationship θ=−π4t2+πt , where r is in feet, θ is in radians, and t is in seconds. use a computer program to plot the path of tip a in x and y coordinates for 0 ≤ t ≤ 4s.
Answers: 2
question
Computers and Technology, 22.06.2019 07:30
In the film "epic 2015," epic is the name for:
Answers: 3
question
Computers and Technology, 23.06.2019 15:00
To check whether your writing is clear , you can
Answers: 2
You know the right answer?
Push Counter Example (pages 9-12):

//PushCounter. java

import javax. swing....
Questions
question
Mathematics, 22.10.2020 01:01
question
Mathematics, 22.10.2020 01:01
Questions on the website: 13722359