Question

Can you fix my error? I created a program that changes labels every second in Java....

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

+ "</html>";

String ly1 = "<html> "

+ "<br> <font size='10' color='red'> <b> Test 22222222 <b/> </font> "

+ "</html>";

//Images Path In Array

String[] list = {

ly, ly1

};

public Map2(){

super("Java SlideShow");

panel = new JPanel();

pic = new JLabel();

//Call The Function SetImageSize

SetImageSize(1);

JButton start = new JButton("Start");

start.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

   //set a timer

tm = new Timer(1000,new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

SetImageSize(x);

x += 1;

if(x >= list.length )

x = 0;

System.out.println(x);

}

});

tm.start();

}

});

add(panel, BorderLayout.SOUTH);

panel.add(start);

add(pic);

setSize(800, 400);

getContentPane().setBackground(Color.decode("#bdb67b"));

setLocationRelativeTo(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

//create a function to resize the image

public void SetImageSize(int i){

pic = new JLabel(list[i]);

}

public static void main(String[] args){

new Map2();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package eventporg;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

// Defines a class Map2 derived from JFrame

public class Map2 extends JFrame

{

// Declares a JPanel object

JPanel panel;

// Declares a JLabel object

JLabel pic;

// Declares a Timer object

Timer tm;

// Variable to toggle font style

int x = 1;

// Creates a font style

String ly = "<html> "

+ "<br> <font size='10' color='red'> <b> Test 111111111 <b/> </font> "

+ "</html>";

// Creates a font style

String ly1 = "<html> "

+ "<br> <font size='10' color='blue'> <b> Test 222222222 <b/> </font> "

+ "</html>";

// Images Path In Array

String[] list = {ly, ly1};

// Default constructor definition

public Map2()

{

// Calls the JFrame parameterized constructor

super("Java SlideShow");

// Creates the panel

panel = new JPanel();

// Creates the label

pic = 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

// Adds the panel to south of the frame

add(panel, BorderLayout.SOUTH);

// Adds the button to panel

panel.add(start);

// Adds the label to frame

add(pic);

// Sets the frame properties

setSize(400, 200);

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)

{

pic.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

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Can you fix my error? I created a program that changes labels every second in Java....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • 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...

    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...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {...

    import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {    public static void createAndShowGUI() {        JFrame frame = new JFrame("Q1");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        Font courierFont = new Font("Courier", Font.BOLD, 40);        Font arialFont = new Font("Arial", Font.BOLD, 40);        Font sansFont = new Font("Sans-serif", Font.BOLD, 20);        JLabel label = new JLabel("Enter a word"); label.setFont(sansFont);        Font font1 = new Font("Sans-serif", Font.BOLD, 40);       ...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT