【问题标题】:Printing out .txt file with JOptionPane使用 JOptionPane 打印出 .txt 文件
【发布时间】:2013-03-14 04:31:15
【问题描述】:

我正在尝试获取我必须滚动的单词列表,而不是从屏幕上运行的窗口。滚动条现在正在工作,但我无法让维度工作。它说( new Dimension( 200, 400 )) ; 有错误。找不到符号

import javax.swing.JDialog;
import javax.swing.*;
import java.util.*;
import java.io.*;


public class print1 
{
    public static void main(String [] args) throws IOException
    {
        String input = "";

        BufferedReader reader = new BufferedReader(new FileReader("wordlist.txt"));
        String line = null;
        while ((line = reader.readLine()) != null) {

            input += line + "\n";
        }
        reader.close();

        JTextArea textArea = new JTextArea(input);
        JScrollPane scrollPane = new JScrollPane(textArea);  
        textArea.setLineWrap(true);  
        textArea.setWrapStyleWord(true); 

        scrollPane.setPreferredSize( new Dimension( 200, 400 )) ;
        JOptionPane.showMessageDialog(null,  scrollPane, "Complete Word List:",           
        JOptionPane.PLAIN_MESSAGE);
    }
}

【问题讨论】:

  • 尝试使用问题标签下的“编辑”按钮发布您的代码。
  • 否,因为它不会显示滚动条并且窗口对于屏幕来说太大了?
  • 你需要这一行来自己设置大小。 scrollPane.setPreferredSize( new Dimension( 200, 400 ) ); 试验窗口大小,看看哪种效果最好。
  • 我运行了相同的代码,但使用了最后一行,它可以工作。阅读我对答案所做的修改。
  • 谢谢哥们。你能把你做的代码发给我吗,因为我一直收到错误?

标签: java joptionpane


【解决方案1】:

也许这会对你有所帮助:

    public class JOptionTest {

        private String text;

        public void readFileContent() throws FileNotFoundException{

           text = new Scanner( new File("install.txt") ).useDelimiter("\\A").next();

        }

        public String getFileContent(){

           return this.text;
        }

        public void displayJOptionTest(){

            JOptionPane.showMessageDialog(null,
            text,
            "StackOverFlow Test",
            JOptionPane.ERROR_MESSAGE);

        }


        public static void main(String [] args) throws FileNotFoundException{

             JOptionTest test = new JOptionTest();

             test.readFileContent();
             test.displayJOptionTest();
        }
    }

【讨论】:

  • 是的,我的错;该方法从文件中读取内容,但我写的代码有点快,我输入了错误的方法名称。对不起。
【解决方案2】:

它比您拥有的代码要复杂一些。此外,您正在尝试使用FileWriterPrintWriter,而您的问题暗示您要读取文件。

读取文本文件的基本设置通常是这样的:

String input = "";
//Setup the reader
BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
//Loop through every line in the .txt file
while ((line = reader.readLine()) != null) {
    //Add the line and then "\n" indicating a new line
    input += line + "\n"
}
reader.close();

现在你有了变量input,它包含了txt文件的所有行。

阅读更多关于阅读文件here

现在,您可能会发现使用 JTextArea 设置简单的 JFrame 而不是 JOptionPane(用于发送简短的消息)更容易。阅读如何使用它们:herehere。您需要创建一个JFrame,然后添加一个JTextArea

更新:或者你可以做这样的事情(取自this answer on SO):

JTextArea textArea = new JTextArea(input);
JScrollPane scrollPane = new JScrollPane(textArea);  
textArea.setLineWrap(true);  
textArea.setWrapStyleWord(true); 
scrollPane.setPreferredSize( new Dimension( 200, 400 ) ); //whatever size you want
JOptionPane.showMessageDialog(null, 
    scrollPane, 
    "text file contents:", 
    JOptionPane.PLAIN_MESSAGE);

我运行了上面的代码,结果如下:

我认为这与您尝试做的很接近。

【讨论】:

  • 非常感谢,但是当有大量单词时,有没有办法让 joption 窗口带有滚动按钮​​??
  • 带有 JList 和 JScrollPane 的 JFrame 可以满足您的需求 @user2205055 。
  • 我将编辑上面的文字并向您展示我的进度。它不会显示滚动条吗??
  • 我相信只有在给定尺寸的框架内有太多文本时才会显示滚动条。
  • 是的,你是对的 Jaybob。如果文本将超出框架大小,则 JScrollBar 将出现在垂直和水平两侧。如果你遇到任何问题,我可以告诉你如何做到这一点:)
【解决方案3】:

以下将过滤后的jpg文件从文件夹添加到JList

private void listPicFiles(){
  DefaultListModel dlm = new DefaultListModel();  
  String path = "C:/New folder";  
  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 

  for (int i = 0; i < listOfFiles.length; i++) { 
   if (listOfFiles[i].isFile()) {
   files = listOfFiles[i].getName();
       if (files.endsWith(".jpg") || files.endsWith(".jpg"))   {
           dlm.add(i, files);
           //System.out.println(files);
        }
     }    
  }
  JList list = new JList(dlm);
  JOptionPane.showMessageDialog(null, list);
  System.out.println(list.getSelectedValue());
}

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多