Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:54:35+00:00 2026-06-15T02:54:35+00:00

very new to writing Java, so am grateful for any help I can get.

  • 0

very new to writing Java, so am grateful for any help I can get.

I`m trying to create JFrame with text in it read from a separate text file (there are 5 paragraphs, each appearing one after the other). I need help making the text appear on the JFrame (not so worried about the delayed effect for now), as so far, not been successful. Am I far off the mark with the code below? I know this a common problem, but other threads have been dealing with events and other (for me) difficult ideas, and taking what I need from such code is still a challenge for me.

I have read I need a JLabel for the text to go on (although elsewhere I read JPanel?). I believe I also need a method for displaying the text in the StoryFrame class, where I can add the JLabel (and possibly state the nextLine commands so as to help create the delayed effect, instead of putting it into my ShowIntro class – I intend to repeat this process repeatedly). The cmd also made me aware that I have a call to a non-static method from a static place – tried looking in my Java for Dummies text but still can`t figure out how to resolve this. I know this code is insufficient, what am I missing?

import javax.swing.JFrame;
import javax.swing.JLabel;

public class StoryFrame extends JFrame {
        public StoryFrame() {
        setTitle("見張ってしながら...");
        setSize(400,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void displayText() {
        JLabel Text = new JLabel();
        add(Text); // I feel I need to add a lot more here to connect the two classes

        }
}


import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class ShowIntro {
 public static void main(String args[]) 
                throws IOException {

    new StoryFrame();
    Scanner reader = new Scanner(new File("Intro.txt"));

    while (reader.hasNext()) {
    StoryFrame.displayText();       
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {      
        }
    }
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:54:37+00:00Added an answer on June 15, 2026 at 2:54 am
    class ShowIntro {
     public static void main(String args[]) 
                    throws IOException {
    
        new StoryFrame();
        Scanner reader = new Scanner(new File("Intro.txt"));
    
        while (reader.hasNext()) {
            StoryFrame.displayText(); // 1 & 2     
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {      
            }
        }
     }
    }
    

    When looking at your ShowIntro class I notice 2 strange things:

    1. You are calling a non-static method (displayText()) on a class.
    2. You don’t pass that what the scanner reads into that method call.

    To fix (1) you should pass the instance of the StoryFrame class into a variable, and replace StoryFrame with the name of this variable in the marked line.
    To fix (2) you should add one parameter to the method displayText(), so replace the header of that method in the StoryFrame class with public void displayText(String text) { and pass the text read by the scanner when you call the method from the ShowIntro class.

    Corrected version (untested)

    class ShowIntro {
     public static void main(String args[]) 
                    throws IOException {
    
        StoryFrame frame = new StoryFrame();                  // Store the StoryFrame in a variable
        Scanner reader = new Scanner(new File("Intro.txt"));
    
        while (reader.hasNext()) {
            frame.displayText(reader.next());               // pass the text read by the scanner to the displayText() method
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {      
            }
        }
     }
    }
    

    And the displayText() method from the StoryFrame class:

    public void displayText(String text) {       // One parameter: a String
        JLabel Text = new JLabel(text);          // Use the String; make it the content of the JLabel
        add(Text);                               // Now add the JLabel to the JFrame.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Java. I am writing a program to read a
I'm very very new to android & JAVA, and I'm trying to learn writing
Im very new to java (mainly duel with embedded only) Im writing a simple
I'm new too Java, but I'm very experienced with PHP. When I was writing
I am very new (started today) to writing chrome extensions but need to write
I am new to the world of Javascript and am tinkering with writing very
Very new to python and can't understand why this isn't working. I have a
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
I am trying to begin writing a Java program in Xcode 4.4 but, At
I'm Writing a facebook application using the Java-facebook-api and MySQL 5.1. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.