如何让文本框滚动

时间:2022-01-04 15:49:29

In my code i'm trying to make a scroll bar with the a text box. I got the text box but i can't get it to scroll. I keep looking up new solutions but none of them work. If you know what is wrong can you help me

在我的代码中,我正在尝试使用文本框创建滚动条。我有文本框但我无法滚动它。我一直在寻找新的解决方案,但它们都不起作用。如果你知道什么是错的可以帮助我

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;

public class Tutorial extends JFrame
{
  public static void main(String args)
  {
    JPanel jp = new JPanel();
    JLabel jl = new JLabel();
    JTextField jt = new JTextField("TYPE", 200);
    JScrollPane myScrollPane = new JScrollPane(jt); 
    JButton jb = new JButton("Enter");
  }

  public Tutorial()
  {
    setTitle("TextBox");
    setVisible(true);
    setSize(400,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    jt.setSize(300,400);
    jp.add(jt);
    jp.add(jb);

    add(jp);
  }
}

3 个解决方案

#1


2  

You need to add the scrollpane to the frame:

您需要将滚动窗格添加到框架:

//jp.add(jt);
jp.add(myScrollPane);

You will also need to maybe sure that the horizontal scrollbar is always visible so that the proper height of the scrollpane can be calculated.

您还需要确保水平滚动条始终可见,以便可以计算滚动窗格的正确高度。

Also, when you post code, make sure you post code that can be compiled. The code you posted makes no sense and there is no way you could even have tested the above code.

此外,当您发布代码时,请确保发布可编译的代码。你发布的代码毫无意义,你甚至无法测试上面的代码。

#2


1  

Here's a complete example:

这是一个完整的例子:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class SamplePane extends JFrame  {

    private static final long serialVersionUID = -1775096938821736542L;

    public SamplePane() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,500);

        JPanel jp = new JPanel();
        JTextField jt = new JTextField("TYPE", 200);
        JScrollPane myScrollPane = new JScrollPane(jt); 
        jp.add(myScrollPane);

        setContentPane(myScrollPane);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SamplePane frame = new SamplePane();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }   

}

#3


1  

  • Another option is to use scroller.setModel(textField.getHorizontalVisibility());
    • The advantage of this method is that the JScrollBar thumb scroll while selecting text(or the right arrow key).
    • 此方法的优点是JScrollBar拇指在选择文本(或右箭头键)时滚动。

    • the disadvantages of this method is that the JScrollBar thumb is always show if all text removed.
    • 此方法的缺点是,如果删除了所有文本,则始终显示JScrollBar缩略图。

  • 另一种选择是使用scroller.setModel(textField.getHorizo​​ntalVisibility());此方法的优点是JScrollBar拇指在选择文本(或右箭头键)时滚动。此方法的缺点是,如果删除了所有文本,则始终显示JScrollBar缩略图。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class HorizontalVisibilityTest {
  private static final boolean DEBUG = false;
  private static final String TEXT = "javascript:(function(){var l=location,"
                                     + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0;}());";
  public JComponent makeUI() {
    Box box = Box.createVerticalBox();
    JScrollPane scroll = new JScrollPane(
      ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setViewportView(new JTextField(TEXT));
    box.add(new JLabel("JScrollPane + VERTICAL_SCROLLBAR_NEVER"));
    box.add(scroll);

    box.add(Box.createVerticalStrut(15));
    final JTextField textField = new JTextField(TEXT);
    JScrollBar scroller = new JScrollBar(JScrollBar.HORIZONTAL);
    scroller.setModel(textField.getHorizontalVisibility());

    if (DEBUG) { //I'm not sure if this is a bug...
      EmptyThumbHandler handler = new EmptyThumbHandler(textField, scroller);
      textField.addComponentListener(handler);
      textField.getDocument().addDocumentListener(handler);
    }

    box.add(new JLabel("BoundedRangeModel: textField.getHorizontalVisibility()"));
    box.add(textField, BorderLayout.SOUTH);
    box.add(Box.createVerticalStrut(5));
    box.add(scroller);

    JPanel p = new JPanel(new BorderLayout());
    p.add(box, BorderLayout.NORTH);
    p.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new HorizontalVisibilityTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

// class EmptyThumbHandler extends ComponentAdapter implements DocumentListener {
//   private final BoundedRangeModel emptyThumbModel = new DefaultBoundedRangeModel(0, 1, 0, 1);
//   private final JTextField textField;
//   private final JScrollBar scroller;
//   public EmptyThumbHandler(JTextField textField, JScrollBar scroller) {
//     super();
//     this.textField = textField;
//     this.scroller = scroller;
//   }
//   private void changeThumbModel() {
//     EventQueue.invokeLater(new Runnable() {
//       @Override public void run() {
//         BoundedRangeModel m = textField.getHorizontalVisibility();
//         int i = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; // -1: ?????
//         System.out.println(i);
//         if (i <= 0) {
//           scroller.setModel(emptyThumbModel);
//         } else {
//           scroller.setModel(textField.getHorizontalVisibility());
//         }
//       }
//     });
//   }
//   @Override public void componentResized(ComponentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void insertUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void removeUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void changedUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
// }

#1


2  

You need to add the scrollpane to the frame:

您需要将滚动窗格添加到框架:

//jp.add(jt);
jp.add(myScrollPane);

You will also need to maybe sure that the horizontal scrollbar is always visible so that the proper height of the scrollpane can be calculated.

您还需要确保水平滚动条始终可见,以便可以计算滚动窗格的正确高度。

Also, when you post code, make sure you post code that can be compiled. The code you posted makes no sense and there is no way you could even have tested the above code.

此外,当您发布代码时,请确保发布可编译的代码。你发布的代码毫无意义,你甚至无法测试上面的代码。

#2


1  

Here's a complete example:

这是一个完整的例子:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class SamplePane extends JFrame  {

    private static final long serialVersionUID = -1775096938821736542L;

    public SamplePane() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,500);

        JPanel jp = new JPanel();
        JTextField jt = new JTextField("TYPE", 200);
        JScrollPane myScrollPane = new JScrollPane(jt); 
        jp.add(myScrollPane);

        setContentPane(myScrollPane);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SamplePane frame = new SamplePane();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }   

}

#3


1  

  • Another option is to use scroller.setModel(textField.getHorizontalVisibility());
    • The advantage of this method is that the JScrollBar thumb scroll while selecting text(or the right arrow key).
    • 此方法的优点是JScrollBar拇指在选择文本(或右箭头键)时滚动。

    • the disadvantages of this method is that the JScrollBar thumb is always show if all text removed.
    • 此方法的缺点是,如果删除了所有文本,则始终显示JScrollBar缩略图。

  • 另一种选择是使用scroller.setModel(textField.getHorizo​​ntalVisibility());此方法的优点是JScrollBar拇指在选择文本(或右箭头键)时滚动。此方法的缺点是,如果删除了所有文本,则始终显示JScrollBar缩略图。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class HorizontalVisibilityTest {
  private static final boolean DEBUG = false;
  private static final String TEXT = "javascript:(function(){var l=location,"
                                     + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0;}());";
  public JComponent makeUI() {
    Box box = Box.createVerticalBox();
    JScrollPane scroll = new JScrollPane(
      ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setViewportView(new JTextField(TEXT));
    box.add(new JLabel("JScrollPane + VERTICAL_SCROLLBAR_NEVER"));
    box.add(scroll);

    box.add(Box.createVerticalStrut(15));
    final JTextField textField = new JTextField(TEXT);
    JScrollBar scroller = new JScrollBar(JScrollBar.HORIZONTAL);
    scroller.setModel(textField.getHorizontalVisibility());

    if (DEBUG) { //I'm not sure if this is a bug...
      EmptyThumbHandler handler = new EmptyThumbHandler(textField, scroller);
      textField.addComponentListener(handler);
      textField.getDocument().addDocumentListener(handler);
    }

    box.add(new JLabel("BoundedRangeModel: textField.getHorizontalVisibility()"));
    box.add(textField, BorderLayout.SOUTH);
    box.add(Box.createVerticalStrut(5));
    box.add(scroller);

    JPanel p = new JPanel(new BorderLayout());
    p.add(box, BorderLayout.NORTH);
    p.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new HorizontalVisibilityTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

// class EmptyThumbHandler extends ComponentAdapter implements DocumentListener {
//   private final BoundedRangeModel emptyThumbModel = new DefaultBoundedRangeModel(0, 1, 0, 1);
//   private final JTextField textField;
//   private final JScrollBar scroller;
//   public EmptyThumbHandler(JTextField textField, JScrollBar scroller) {
//     super();
//     this.textField = textField;
//     this.scroller = scroller;
//   }
//   private void changeThumbModel() {
//     EventQueue.invokeLater(new Runnable() {
//       @Override public void run() {
//         BoundedRangeModel m = textField.getHorizontalVisibility();
//         int i = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; // -1: ?????
//         System.out.println(i);
//         if (i <= 0) {
//           scroller.setModel(emptyThumbModel);
//         } else {
//           scroller.setModel(textField.getHorizontalVisibility());
//         }
//       }
//     });
//   }
//   @Override public void componentResized(ComponentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void insertUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void removeUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
//   @Override public void changedUpdate(DocumentEvent e) {
//     changeThumbModel();
//   }
// }