I created a program to display information about the presidents in Java. When I click the start button, it starts to move, but when I stop and click the start button again, it advance two pages at a time. It has to change one page at a time. Please correct this error.
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 Map2 extends 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'> <img src='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 political leader, military general, statesman, and Founding Father who also served as the first president of the United States from 1789 to 1797. He led Patriot forces to victory in the nation's War of Independence, and he presided at the Constitutional Convention of 1787 which established the new federal government. He has been called the \"Father of His Country\" for his manifold leadership in the formative days of the new nation. <b/> </font> "
+ "</html>";
String pre2 = "<html> "
+ "<font size='12'> <b> 1797 - 1801 <b/> </font> "
+ "<br> <p align='center'> <img src='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 the second president of the United States from 1797 to 1801. Before his presidency he was a leader of the American Revolution that achieved independence from Great Britain, and also served as the first vice president of the United States. <b/> </font>"
+ "</html>";
String pre3 = "<html> "
+ "<font size='12'> <b> 1801 - 1809 <b/> </font> "
+ "<br> <p align='center'> <img src='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 the third president of the United States from 1801 to 1809. Previously, he had served as the second vice president of the United States from 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'> <img src='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 fourth president of the United States from 1809 to 1817. He is hailed as the \"Father of the Constitution\" for his pivotal role in drafting and promoting the United States Constitution and the United States Bill of Rights. He also co-wrote The Federalist Papers, co-founded the Democratic-Republican Party, and served as the fifth United States 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'> <img src='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 of the United States from 1817 to 1825. A member of the Democratic-Republican Party, Monroe was the last president of the Virginia dynasty, and his presidency coincided with the Era of Good Feelings. <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 anonymous class
start.addActionListener(new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(ActionEvent e) {
// Set a timer
tm = new Timer(1000, new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(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 anonymous class
stop.addActionListener(new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(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 void SetImageSize(int i) {
label.setText(list[i]);
}// End of method
// main method definition
public static void main(String[] args) {
// Creates a anonymous object to calls default constructor
new Map2();
}// End of main method
}// End of class
The problem with your code was that whenever the start button is clicked, you didn’t check if the timer is already running before starting it. Also, the timer was being recreated every time the start button is clicked, leaving the original timer task running in background. So I made three changes mainly. 1- Moved the timer initialization outside the action listener of start button click, so that the timer is created only once. 2- inside the action listener of start button, stopped the timer if already running before starting the timer. 3- inside the action listener of stop button, stopped the timer only if it is running.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Map2.java
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 Map2 extends 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'> <img src='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 political leader, military general, statesman, and Founding Father who also served as the first president of the United States from 1789 to 1797. He led Patriot forces to victory in the nation's War of Independence, and he presided at the Constitutional Convention of 1787 which established the new federal government. He has been called the \"Father of His Country\" for his manifold leadership in the formative days of the new nation. <b/> </font> "
+ "</html>";
String pre2 = "<html> "
+ "<font size='12'> <b> 1797 - 1801 <b/> </font> "
+ "<br> <p align='center'> <img src='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 the second president of the United States from 1797 to 1801. Before his presidency he was a leader of the American Revolution that achieved independence from Great Britain, and also served as the first vice president of the United States. <b/> </font>"
+ "</html>";
String pre3 = "<html> "
+ "<font size='12'> <b> 1801 - 1809 <b/> </font> "
+ "<br> <p align='center'> <img src='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 the third president of the United States from 1801 to 1809. Previously, he had served as the second vice president of the United States from 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'> <img src='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 fourth president of the United States from 1809 to 1817. He is hailed as the \"Father of the Constitution\" for his pivotal role in drafting and promoting the United States Constitution and the United States Bill of Rights. He also co-wrote The Federalist Papers, co-founded the Democratic-Republican Party, and served as the fifth United States 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'> <img src='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 of the United States from 1817 to 1825. A member of the Democratic-Republican Party, Monroe was the last president of the Virginia dynasty, and his presidency coincided with the Era of Good Feelings. <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");
//creating the timer outside the action listener of button
tm = new Timer(1000, new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(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
tm.setInitialDelay(0);
// Registers the start button for event handling using anonymous class
start.addActionListener(new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(ActionEvent e) {
// if the timer is running when the start
// button is clicked, we should stop it
if (tm != null && tm.isRunning()) {
// stopping timer
tm.stop();
}
// 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 anonymous class
stop.addActionListener(new ActionListener() {
@Override
// Overrides the method of ActionListenter interface
public void actionPerformed(ActionEvent e) {
// here also, stopping the timer only if it is running
if (tm != null && tm.isRunning()) {
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 void SetImageSize(int i) {
label.setText(list[i]);
}// End of method
// main method definition
public static void main(String[] args) {
// Creates a anonymous object to calls default constructor
new Map2();
}// End of main method
}// End of class
I created a program to display information about the presidents in Java. When I click the start button, it starts to move, but when I stop and click the start button again, it advance two pages at a t...
Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. 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; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...