【问题标题】:How to read an imported text file into a JFrame如何将导入的文本文件读入 JFrame
【发布时间】:2013-12-02 17:54:33
【问题描述】:

所以我希望将我导入的名为“Instructions.txt”的文本文件添加到 JFrame 中,以便玩家查看他/她是否选择查看指令。我可以让一个词出现在屏幕的最左侧,但没有别的。此外,如果我通过拖动一侧来调整窗口大小,则左侧第一个单词的顶部会显示多个单词。有什么想法吗?

 //Battleship.java

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;

public class Battleship
{
 public static void main (String[] args) throws IOException
{
String name = JOptionPane.showInputDialog("What is your name?");

    String answer = JOptionPane.showInputDialog("Welcome to Battleship, "+name+". Would you         like to see a set of instructions?");

if(answer.equals ("yes") || answer.equals ("Yes"))
{
TextFrame textframe = new TextFrame();
  textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  Scanner scan = new Scanner (new File("Instructions.txt"));

  while (scan.hasNext())
  {
    textframe.displayText(scan.next());

  }

JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Buttons b = new Buttons();

JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}

else
{    
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Buttons b = new Buttons();

JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}

 }

 }

 //TextFrame.java

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

 public class TextFrame extends JFrame 
 {

    public TextFrame() 
{

    setTitle("Instructions");
    setSize(400,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void displayText(String text) 
{       
JLabel Text = new JLabel(text);          
add(Text);                               
 }
 }

【问题讨论】:

    标签: java jframe


    【解决方案1】:

    有一些组件会为您处理这个问题。从这里开始:

    http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html

    【讨论】:

    • 我不明白为什么这被否决了。虽然这只是官方文档的一般链接,没有直接编码帮助,但这些文档是相关的,有助于回答问题,符合 SO 规则。
    【解决方案2】:

    也许你应该在调用一个方法来显示它的每个标记之前阅读整个"Instructions.txt" 文件。声明一个变量,将文件内容写入其中,然后显示。正如前面的答案所暗示的,JLabel 并不是显示一堆文本的最佳组件。

    【讨论】:

      【解决方案3】:

      目前您正在为文本文件的每一行添加一个新的JLabel。所有这些JLabel 都添加到框架的中心区域,替换了您之前添加的所有组件。因此,您看到的唯一单词是文件中的最后一个单词。您可以使用以下代码解决此问题:

      Scanner scan = new Scanner (new File("Instructions.txt"));
      StringBuilder sb = new StringBuilder();
      
      while (scan.hasNext()) {
          sb.append(scan.next());
      }
      
      textframe.displayText(sb.toString());
      

      要在JLabel 中显示长文本,我还会将文本包装在 html 标签中:

      public void displayText(String text) {       
          JLabel lblText = new JLabel("<html><body>" + text + "</body></html>");          
          add(lblText);                             
      }
      

      此外,我建议使用BufferedReader 阅读您的文件。老实说,我从未使用过扫描仪来读取文本文件,我不确定这是否是一种好习惯。一个例子可以在这里找到:Reading and displaying data from a .txt file

      【讨论】:

        【解决方案4】:

        我用 JTextArea 更新了您的代码。我把它全部放在一个文件中,所以我删除了一些导入。你应该修复这些项目。

        import java.util.Scanner;
        import java.io.*;
        import javax.swing.JOptionPane;
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        
        public class Battleship {
        
            public static void main(String[] args) throws IOException {
        
                String name = JOptionPane.showInputDialog("What is your name?");
        
                String answer = JOptionPane.showInputDialog("Welcome to Battleship, " + name + ". Would you         like to see a set of instructions?");
        
                if (answer.equals("yes") || answer.equals("Yes")) {
                    TextFrame textframe = new TextFrame();
                    JTextArea jta = new JTextArea();
                    jta.setEditable(false);
                    jta.setLineWrap(true);
                    jta.setWrapStyleWord(true);
                    textframe.add(jta);
                    textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
                    Scanner scan = new Scanner(new File("Instructions.txt"));
        
                    StringBuilder sb = new StringBuilder();
                    while (scan.hasNextLine()) {
                        String line = scan.nextLine();
                        jta.append(line);
        
                    }
        
                    JFrame frame = new JFrame("Battleship");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
                   // Buttons b = new Buttons();
        
                    JPanel panel = new JPanel();
                   // panel.add(b);
                    panel.setBackground(Color.blue);
                    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        
                    frame.getContentPane().add(panel);
                    frame.pack();
                    frame.setVisible(true);
                } else {
                    JFrame frame = new JFrame("Battleship");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
                    //Buttons b = new Buttons();
        
                    JPanel panel = new JPanel();
                  //  panel.add(b);
                    panel.setBackground(Color.blue);
                    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        
                    frame.getContentPane().add(panel);
                    frame.pack();
                    frame.setVisible(true);
                }
        
            }
        
        }
        
          class TextFrame extends JFrame {
        
            public TextFrame() {
        
                setTitle("Instructions");
                setSize(400, 500);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setVisible(true);
            }
        
            public void displayText(String text) {
                JLabel Text = new JLabel(text);
                add(Text);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-07-30
          • 1970-01-01
          • 1970-01-01
          • 2015-11-12
          • 2016-04-20
          • 1970-01-01
          • 2016-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多