Computers and Technology
Computers and Technology, 10.04.2020 04:59, weeblordd

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");

}

}

}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 03:00, tay9122
Jason, samantha, ravi, sheila, and ankit are preparing for an upcoming marathon. each day of the week, they run a certain number of miles and write them into a notebook. at the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. write a program to them analyze their data. your program must contain parallel arrays: an array to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. furthermore, your program must contain at least the following functions: a function to read and store the runners’ names and the numbers of miles run each day; a function to find the total miles run by each runner and the average number of miles run each day; and a function to output the results. (you may assume that the input data is stored in a file and each line of data is in the following form: runnername milesday1 milesday2 milesday3 milesday4 milesday5 milesday6 milesday7.)
Answers: 3
image
Computers and Technology, 24.06.2019 01:00, bellamyciana
What are two ways to access the options for scaling and page orientation? click the home tab, then click alignment, or click the file tab. click the file tab, then click print, or click the page layout tab. click the page layout tab, or click the review tab. click the review tab, or click the home tab?
Answers: 2
image
Computers and Technology, 24.06.2019 01:30, BIKRAMlawati5544
Could you find out how im still getting an 83 percent on this in edhesive a = input("enter an animal: ") s = input ("enter a sound: ") e = "e-i-e-i-o" print ("old macdonald had a farm, " + e) print ("and on his farm he had a " + a + "," + e) print ("with a " + s + "-" + s + " here and a " + s + "-" + s + " there") print ("here a " + s+ " there a " + s) print ("everywhere a " + s + "-" + s ) print ("old macdonald had a farm, " + e)
Answers: 2
image
Computers and Technology, 24.06.2019 13:30, lovecats12
To move an excel worksheet tab, simply right-click on it drag and drop it double-click on it delete it
Answers: 1
Do you know the correct answer?
Push Counter Example (pages 9-12):

//PushCounter. java

import javax. swing....

Questions in other subjects:

Konu
English, 23.09.2021 20:30
Konu
Mathematics, 23.09.2021 20:30
Konu
Chemistry, 23.09.2021 20:30
Konu
Mathematics, 23.09.2021 20:30