Note that there are some explanatory texts on larger screens.

plurals
  1. POText problems with JPanel
    primarykey
    data
    text
    <p>After thinking I was on course to solving a problem with making text (read from a file) appear in a JPanel, I`m frustratingly back to square one. </p> <p>The code is below. The result is just a blank screen 400x500 screen. Some combinations of using nextLine() + nextLine() as displayText commands result in one word coming up from the file (the word has been different multiple times). This makes me wonder: do I need code that deals with text wrapping? The textfile itself is in paragraphs, and as such, I thought that sf.displayText should say sf.displayText(reader.next() + reader.nextline() + reader.nextline(), and have tried other combinations, but this may be confusing the while parameters. Have also tried a different textfile with simple sentences, no paragraphs, but again, nothing comes up. </p> <p>Having looked online, I have found that layouts may be an issue, and that alternative options may be BufferedReader or using JTextArea. Browsing through Big Java didn`t provide anything I felt I could take, as all discussion on the scanner went towards integers, whereas the file I want read is prose. I also tried putting a small piece of text in the code itself and cancelling out everything else below it to see if I could transfer text from the code to the JPanel:</p> <pre><code>StoryFrame sf = new StoryFrame(); sf.displayText("Life is beautiful"); </code></pre> <p>but still nothing came up. Ultimately, I want to put text from a file into a JPanel, and have each paragraph come up 5 seconds after the one before. So ultimately, my questions are:</p> <ul> <li>Why does my text fail to show up, or only display one word? Is it because I don`t specify a layout?</li> <li>Do I need to think about text wrapping? </li> <li>Should I look into JTextArea instead of JPanel, and BufferedReader instead of Scanner?</li> <li>Have I been using the nextLine method from the Scanner correctly? </li> <li>Can I put a command to read a file and display that file`s text in the display method of StoryFrame (I think this would make things a lot easier)?</li> </ul> <p>I know it`s a lot, so any answers to any of the questions would be greatly appreciated, thank you. Tom</p> <pre><code>import javax.swing.JFrame; import javax.swing.JLabel; public class StoryFrame extends JFrame { private JLabel mylabel; public StoryFrame() { setTitle("見張ってしながら..."); setSize(400,500); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); mylabel = new JLabel(); this.add(mylabel); setVisible(true); } public void displayText(String text) { JLabel storyText = new JLabel(); add(storyText); } } </code></pre> <h2>ShowIntro</h2> <pre><code>import java.util.Scanner; import java.io.File; import java.io.IOException; class ShowIntro { public static void main(String args[]) throws IOException { StoryFrame sf = new StoryFrame(); Scanner reader = new Scanner(new File("Try.txt")); while (reader.hasNextLine()) { //String line = in.nextLine() Not sure whether this would contribute, I doubt it does though sf.displayText(reader.next()); //sf.displayText(reader.next() + reader.nextLine() + reader.nextLine()); was also attempted. try { Thread.sleep(5000); } catch (InterruptedException e) { } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload