java - JScrollPane 不水平滚动

标签 java swing

我有一个正在处理的程序,但无法弄清楚为什么它不能水平滚动。我已经为 hor 和 ver 设置了滚动策略,但是只显示垂直滚动。程序获取一个用户选择的txt文件,然后显示在jtextarea中。除水平滚动外,一切正常。有什么想法吗?

谢谢!

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

class FileChooserDemo3{

   JLabel jlab;
   JButton jbtnShow;
   JFileChooser jfc;
   JTextArea jta;
   JScrollPane scrollPane;
   String filename = null;

   FileChooserDemo3() {
      //create new JFrame container.
      JFrame jfrm = new JFrame("JFileChooser Demo");

      //Specify FlowLayout for layout manager
      jfrm.setLayout(new FlowLayout());

      //Give the frame initial size
      jfrm.setSize(800,800);

      //End program when user closes application
      jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //Create a label to show selected file
      jlab=new JLabel();

      //Create button to show dialog
      jbtnShow = new JButton("Show File Chooser");

      //create textarea with ability to textwrap (p889-891) and scroll (hint:  Use JScrollPane)
      jta = new JTextArea(45, 40);
      jta.setLineWrap(true);
      JScrollPane scrollPane = new JScrollPane(jta);






      //Create file chooser starting at default directory
      jfc=new JFileChooser();
      String name = null;
      //Show file chooser when show file chooser button pressed
      jbtnShow.addActionListener(new ActionListener()  {
         public void actionPerformed(ActionEvent le) {
            //Pass null for the parent.  This centers the dialog on the screen.
            int result = jfc.showOpenDialog(null);

            if(result==JFileChooser.APPROVE_OPTION){
               jlab.setText("Selected file is:  " + jfc.getSelectedFile().getName());
               File file = jfc.getSelectedFile();
               jfrm.add(scrollPane);
               scrollPane.setVerticalScrollBarPolicy(
         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      scrollPane.setHorizontalScrollBarPolicy(
         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
               jta.setText("");
               Scanner in = null;

               //Get selected file stored as a file.


               try{
                  //Do file processing here
                  in = new Scanner(file);
                  while(in.hasNext()) {
                     String line = in.nextLine();
                     jta.append(line+"\n");
                  }
               }

               catch(IOException e){
                  System.out.println("Exception");

               }
               finally {
                  in.close();
              }    



            }
            else{
               jlab.setText("No file selected.");
            }
         }
      });

         //add the show file chooser button and label to content pane
         jfrm.add(jbtnShow);
         jfrm.add(jlab);


         //Display the frame
         jfrm.setVisible(true);
   }

   public static void main(String[] args){
      //Create GUI on the event dispatching thread.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new FileChooserDemo3();
         }
      });
   }
}

最佳答案

使用 jta.setLineWrap(false); 而不是 jta.setLineWrap(true); 关闭 JTextArea 的换行。否则不需要水平滚动条,因为 JTextArea 将行换行以使其适合父级而无需滚动。

关于java - JScrollPane 不水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29610837/

相关文章:

java - 从 JButton 中删除图像

java - 使用代码构建 GUI 时在 NetBeans 中预览 GUI

java - 从 JTextPane 获取原始文本

java - JScrollPane、JTextArea 和 JPanel 的问题

java - 在 Java 桌面应用程序中嵌入 fckeditor

java - Spring 启动: how to match all routes?

java - ListView 中的按钮每个项目布局

Java,修改一个文件excel

Java 获取 java.lang.Number 或原语的通用子类的 valueOf

java - 我可以构建并运行我的项目,但我的 IDE 一直显示错误