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:40
"it security policy enforcement and monitoring" respond to the following: describe how monitoring worker activities can increase the security within organizations. describe the rationale that managers should use to determine the degree of monitoring that the organization should conduct. explain the extent to which you believe an organization has the right to monitor user actions and traffic. determine the actions organizations can take to mitigate the potential issues associated with monitoring user actions and traffic.
Answers: 3
question
Computers and Technology, 23.06.2019 09:30
Write a function called checkfactor that takes two arrays of positive numbers, firstnumberrow and secondnumberrow. checkfactor checks if the first entry in firstnumberrow is divisible by the first entry in secondnumberrow, and performs the same operation on the next array elements until all entries have been checked. all the numbers are positive and the number of entries in the arrays are the same. the function should return the identified divisible numbers in two row arrays named firstdivisible and seconddivisible.restrictions: branches or loops should not be used. the code must use the internal mod and logical functions.hint: the mod function should be used to determine if two numbers are divisible. ex: for num1 and num2 if mod(num1,num2) is 0, then the two numbers are divisible.this is matlab
Answers: 2
question
Computers and Technology, 23.06.2019 19:30
Anul 2017 tocmai s-a încheiat, suntem trişti deoarece era număr prim, însă avem şi o veste bună, anul 2018 este produs de două numere prime, 2 şi 1009. dorel, un adevărat colecţionar de numere prime, şi-a pus întrebarea: “câte numere dintr-un interval [a,b] se pot scrie ca produs de două numere prime? “.
Answers: 3
question
Computers and Technology, 24.06.2019 13:00
Refer to the figure and match the theorem that supports the statement.1.if chords are =, then arcs are =.if bc = de, then arc bc = arc de2.if arcs are =, then chords are =.if arc bc = arc de, then bc = de3.diameters perpen
Answers: 3
You know the right answer?
Push Counter Example (pages 9-12):

//PushCounter. java

import javax. swing....
Questions
question
Mathematics, 31.07.2019 08:30
Questions on the website: 13722363