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 10:30
Think about a recent customer service experience - either positive or negative. write a brief summary of that experience. now think about those four characteristics we look for in customer service representatives. how did the representative in your example stack up? write down your answer and give specific examples.
Answers: 1
question
Computers and Technology, 24.06.2019 14:00
What are the different components of the cloud architecture?
Answers: 2
question
Computers and Technology, 25.06.2019 05:10
Maia notices that her paragraphs are too close to one another. she wants to increase this space. which arrangement of steps does maia follow after she goes to the paragraph command group in the ribbon? type in the space needed, go to the after menu, go to the spacing part of the box, and open the paragraph dialog box. go to the spacing part of the box, go to the after menu, open the paragraph dialog box, and type in the space needed. open the paragraph dialog box, go to the spacing part of the box, go to the after menu, and type in the space needed. type in the space needed, open the paragraph dialog box, go to the after menu, and go to the spacing part of the box.
Answers: 1
question
Computers and Technology, 25.06.2019 06:30
During which stage do you think is the best time for a company to purchase an emerging technology
Answers: 1
You know the right answer?
Push Counter Example (pages 9-12):

//PushCounter. java

import javax. swing....
Questions
Questions on the website: 13722367