Question

Instructions The application JFacts contains a JFrame, a label, and six random facts. Every time the...

Instructions

The application JFacts contains a JFrame, a label, and six random facts. Every time the user clicks the JButton, remove replace the label's text and add a different one. The facts should cycle in order, fact 1 to fact 6. When the JButton is pressed on face 6, the program should display fact 1 again.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFacts extends JFrame implements ActionListener {
FlowLayout flow = new FlowLayout();
JButton b = new JButton("Press to change fact");
JLabel stars = new JLabel("**********************************************");
JLabel fact = new JLabel();
String[] quotes = new String[] { "Only seven prisoners were in the Bastille when it was stormed. ",
"A Tale of Two Cities is a novel set during the French Revolution. ",
"Perhaps 40,000 people were executed at the guillotine. ",
"A loaf of bread cost a weeks wages prior to the French Revolution. ",
"Thomas Jefferson was the U.S. Minister to France during the Revolution. ",
"Protestant and Jewish religions were illegal in France before 1879. ", };
int counter = 0;

public JFacts() {
// Write your code here
this.setLayout(flow);
this.add(stars);
this.add(fact);
this.add(b);
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//Get test and set to label
fact.setText(quotes[counter]);
//Increment counter
counter++;
//Mod by 6 to reset counter to 0;
counter = counter % 6;
}
});
}

public static void main(String[] args) {
JFacts rFrame = new JFacts();
rFrame.setSize(440, 300);
rFrame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}

Someone tried helping me out with this code this code works but it's giving me a wierd error so it wont work for the test casses

Test Contents

public class CodevolveTestRunner {

   public static void main(String[] args) throws InterruptedException {
    JFacts jf = new JFacts();
    jf.setSize(440, 100);
    jf.setVisible(true);
    Thread.sleep(1000);
    // performs the click actions
    for(int i = 0; i < 6; ++i) {
       if(!jf.fact.getText().equals(jf.quotes[i])) {
              System.err.print("The label should read " + jf.quotes[i] + " but displays " + jf.fact.getText());
        }
       Thread.sleep(1000);
       jf.b.doClick();
      Thread.sleep(1000);
    }
    Thread.sleep(2000);
    System.exit(0);
   }
}

Errors

The label should read Only seven prisoners were in the Bastille when it was stormed.  but displays The label should read A Tale of Two Cities is a novel set during the French Revolution.  but displays Only seven prisoners were in the Bastille when it was stormed. The label should read Perhaps 40,000 people were executed at the guillotine.  but displays A Tale of Two Cities is a novel set during the French Revolution. The label should read A loaf of bread cost a weeks wages prior to the French Revolution.  but displays Perhaps 40,000 people were executed at the guillotine. The label should read Thomas Jefferson was the U.S. Minister to France during the Revolution.  but displays A loaf of bread cost a weeks wages prior to the French Revolution. The label should read Protestant and Jewish religions were illegal in France before 1879.  but displays Thomas Jefferson was the U.S. Minister to France during the Revolution. 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

There were couple of simple mistakes in your code. Initially during start up, the program doesn’t show a fact unless the button is clicked, but the test program expects the JFacts program to display the first fact even before the button click. So I have fixed the code to display it initially, and whenever the button is pressed, the counter is updated before setting the new fact. 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

// JFacts.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class JFacts extends JFrame implements ActionListener {

      FlowLayout flow = new FlowLayout();

      JButton b = new JButton("Press to change fact");

      JLabel stars = new JLabel("**********************************************");

      JLabel fact = new JLabel();

      String[] quotes = new String[] {

                  "Only seven prisoners were in the Bastille when it was stormed. ",

                  "A Tale of Two Cities is a novel set during the French Revolution. ",

                  "Perhaps 40,000 people were executed at the guillotine. ",

                  "A loaf of bread cost a weeks wages prior to the French Revolution. ",

                  "Thomas Jefferson was the U.S. Minister to France during the Revolution. ",

                  "Protestant and Jewish religions were illegal in France before 1879. ", };

      int counter = 0;

      public JFacts() {

            // Write your code here

            this.setLayout(flow);

            this.add(stars);

            this.add(fact);

            this.add(b);

            // you should display the fact initially (before button press)

            fact.setText(quotes[counter]);

            b.addActionListener(new ActionListener() {

                  @Override

                  public void actionPerformed(ActionEvent e) {

                        // whenever the button is clicked, the counter should be updated

                        // before setting the text

                        // Increment counter

                        counter++;

                        // Mod by 6 to reset counter to 0;

                        counter = counter % 6;

                        // Get test and set to label

                        fact.setText(quotes[counter]);

                  }

            });

      }

      public static void main(String[] args) {

            JFacts rFrame = new JFacts();

            rFrame.setSize(440, 300);

            rFrame.setVisible(true);

      }

      @Override

      public void actionPerformed(ActionEvent e) {

      }

}

Add a comment
Know the answer?
Add Answer to:
Instructions The application JFacts contains a JFrame, a label, and six random facts. Every time the...
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
  • Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...

    Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box that displays the temporary password should be read-only. The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year. If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields. If the user does not enter data, clear the...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

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