JScrollPane不能水平滚动

时间:2015-04-13 16:54:32

标签: 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();
         }
      });
   }
}

2 个答案:

答案 0 :(得分:3)

删除换行语句以允许滚动条的子组件具有大于视口大小的水平大小

jta.setLineWrap(true); // remove

答案 1 :(得分:3)

使用jta.setLineWrap(false);代替jta.setLineWrap(true);关闭JTextArea的换行。否则,不需要水平滚动条,因为JTextArea包装行使得它适合父类而不滚动。