(Solved) : Created Program Display Information Presidents Java Click Start Button Starts Move Stop Cl Q37934448 . . .
I created a program to display information about the presidentsin Java. When I click the start button, it starts to move, but whenI stop and click the start button again, it advance two pages at atime. It has to change one page at a time. Please correct thiserror.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
// Defines a class Map2 derived from JFrame
public class Map2extends JFrame {
// Declares a JPanel object
JPanel panel;
// Declares a JLabel object
JLabel label;
// Declares a Timer object
Timer tm;
// Variable to toggle font style
int x = 1;
// Creates a font style
String pre1 = “<html> “
+ “<font size=’12’> <b> 1789-1797 <b/></font> “
+ “<br> <p align=’center’> <imgsrc=’https://presidenstory.com/usimag/phot2/washington.jpg’alt=”Picture” width=’150px’ height=’120px’ > </p>”
+ “<font size=’10’> <b> George Washington <b/></font> “
+ “<br> <font size=’6′> <b> 1st US President<b/> </font> “
+ “<br> <font size=’4′> <b> George Washington(February 22, 1732 – December 14, 1799) was an American politicalleader, military general, statesman, and Founding Father who alsoserved as the first president of the United States from 1789 to1797. He led Patriot forces to victory in the nation’s War ofIndependence, and he presided at the Constitutional Convention of1787 which established the new federal government. He has beencalled the “Father of His Country” for his manifold leadership inthe formative days of the new nation. <b/> </font>”
+ “</html>”;
String pre2 = “<html> “
+ “<font size=’12’> <b> 1797 – 1801 <b/></font> “
+ “<br> <p align=’center’> <imgsrc=’https://presidenstory.com/usimag/phot2/adams.jpg’alt=”Picture” width=’150px’ height=’120px’ > </p>”
+ “<font size=’10’> <b> John Adams <b/></font> “
+ “<br> <font size=’6′> <b> 2nd US President<b/> </font> “
+ “<br> <font size=’4′> <b> John Adams(October 30, 1735 – July 4, 1826) was an American statesman,attorney, diplomat, writer, and Founding Father who served as thesecond president of the United States from 1797 to 1801. Before hispresidency he was a leader of the American Revolution that achievedindependence from Great Britain, and also served as the first vicepresident of the United States. <b/> </font>”
+ “</html>”;
String pre3 = “<html> “
+ “<font size=’12’> <b> 1801 – 1809 <b/></font> “
+ “<br> <p align=’center’> <imgsrc=’https://presidenstory.com/usimag/phot2/jefferson.jpg’alt=”Picture” width=’150px’ height=’120px’ > </p>”
+ “<font size=’10’> <b> Thomas Jefferson <b/></font> “
+ “<br> <font size=’6′> <b> 3rd US President<b/> </font> “
+ “<br> <font size=’4′> <b> Thomas Jefferson(April 13, 1743 – July 4, 1826) was an American statesman,diplomat, lawyer, architect, and Founding Father who served as thethird president of the United States from 1801 to 1809. Previously,he had served as the second vice president of the United Statesfrom 1797 to 1801. <b/> </font> “
+ “<br> <font size=’4′ color=’red’> <b> State- Virginia <b/> </font> “
+ “</html>”;
String pre4 = “<html> “
+ “<font size=’12’> <b> 1809 – 1817 <b/></font> “
+ “<br> <p align=’center’> <imgsrc=’https://presidenstory.com/usimag/phot2/madison.jpg’alt=”Picture” width=’150px’ height=’120px’ > </p>”
+ “<font size=’10’> <b> James Madison <b/></font> “
+ “<br> <font size=’6′> <b> 4th US President<b/> </font> “
+ “<br> <font size=’4′> <b> James Madison Jr.(March 16, 1751 – June 28, 1836) was an American statesman, lawyer,diplomat, philosopher, and Founding Father who served as the fourthpresident of the United States from 1809 to 1817. He is hailed asthe “Father of the Constitution” for his pivotal role in draftingand promoting the United States Constitution and the United StatesBill of Rights. He also co-wrote The Federalist Papers, co-foundedthe Democratic-Republican Party, and served as the fifth UnitedStates secretary of State from 1801 to 1809. <b/></font> “
+ “</html>”;
String pre5 = “<html> “
+ “<font size=’12’> <b> 1817 – 1825 <b/></font> “
+ “<br> <p align=’center’> <imgsrc=’https://presidenstory.com/usimag/phot2/monroe.jpg’alt=”Picture” width=’150px’ height=’120px’ > </p>”
+ “<font size=’10’> <b> James Monroe <b/></font> “
+ “<br> <font size=’6′> <b> 5th US President<b/> </font> “
+ “<br> <font size=’4′> <b> James Monroe(April 28, 1758 – July 4, 1831) was an American statesman, lawyer,diplomat, and Founding Father who served as the fifth president ofthe United States from 1817 to 1825. A member of theDemocratic-Republican Party, Monroe was the last president of theVirginia dynasty, and his presidency coincided with the Era of GoodFeelings. <b/> </font> “
+ “<br> <font size=’4′ color=’red’> <b> State- Virginia <b/> </font> “
+ “</html>”;
// Images Path In Array
String[] list = {pre1, pre2, pre3, pre4, pre5};
// Default constructor definition
public Map2() {
// Calls the JFrame parameterized constructor
super(“Java SlideShow”);
// Creates the panel
panel = new JPanel();
// Creates the label
label = new JLabel();
// Call The Function SetImageSize
SetImageSize(0);
// Creates the button
JButton start = new JButton(“Start”);
// Registers the start button for event handling using anonymousclass
start.addActionListener(new ActionListener(){
@Override
// Overrides the method of ActionListenter interface
public voidactionPerformed(ActionEvent e) {
// Set a timer
tm = new Timer(1000, newActionListener() {
@Override
// Overrides the method of ActionListenter interface
public voidactionPerformed(ActionEvent e) {
// Call The Function SetImageSize with index position style
SetImageSize(x);
// Increase the value by one
x += 1;
// Checks if x value is greater than or equals to
// list length
if(x >= list.length)
// Reset the x to 0
x = 0;
}// End of method
});// End of anonymous inner class for Timer
// Starts the timer
tm.start();
}// End of method
});// End of anonymous inner class for ActionListener
// Creates the button
JButton stop = new JButton(“Stop”);
// Registers the stop button for event handling using anonymousclass
stop.addActionListener(new ActionListener(){
@Override
// Overrides the method of ActionListenter interface
public voidactionPerformed(ActionEvent e) {
tm.stop();
}// End of method
});// End of anonymous inner class for ActionListener
// Adds the panel to south of the frame
add(panel, BorderLayout.SOUTH);
// Adds the button to panel
panel.add(start);
panel.add(stop);
// Adds the label to frame
add(label);
// Sets the frame properties
setSize(400, 600);
getContentPane().setBackground(Color.decode(“#bdb67b”));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}// End of default constructor
// Create a method to resize the image
public voidSetImageSize(int i) {
label.setText(list[i]);
}// End of method
// main method definition
public staticvoid main(String[] args) {
// Creates a anonymous object to calls default constructor
new Map2();
}// End of main method
}// End of class
Expert Answer
Answer to Created Program Display Information Presidents Java Click Start Button Starts Move Stop Cl Q37934448 . . .
OR