简体   繁体   English

JScrollPane无法水平滚动

[英]JScrollPane not scrolling horizontally

I have a program I am working on and cannot figure out why it isn't scrolling horizontally. 我有一个正在使用的程序,无法弄清为什么它没有水平滚动。 I have set the scroll policies for both hor and ver however only vertical scroll is showing up. 我已经为hor和ver设置了滚动策略,但是仅显示垂直滚动。 The program gets a txt file that the user chooses and then displays it in the jtextarea. 该程序获取用户选择的txt文件,然后将其显示在jtextarea中。 All is working fine except the horizontal scroll. 除了水平滚动以外,其他所有工具都工作正常。 Any ideas? 有任何想法吗?

Thanks! 谢谢!

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(true); // remove

Turn off line wrap for your JTextArea using jta.setLineWrap(false); 使用jta.setLineWrap(false);关闭JTextArea的换行jta.setLineWrap(false); instead of jta.setLineWrap(true); 而不是jta.setLineWrap(true); . Otherwise no horizontal scroll bar is needed because the JTextArea wraps the lines such that it fits into the parent without scrolling. 否则,不需要水平滚动条,因为JTextArea将行换行以使其适合父级而无需滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM