在我的代码中,我试图使滚动条与文本框。我有文本框,但我不能让它滚动。我一直在寻找新的解决方案,但没有一个奏效。如果你知道出了什么问题,你能帮帮我吗?
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);
}
}
你需要将滚动面板添加到框架中:
//jp.add(jt);
jp.add(myScrollPane);
你可能还需要确保水平滚动条始终可见,以便可以计算出滚动窗格的适当高度。
另外,当您发布代码时,请确保您发布的代码可以编译。你发布的代码没有意义,你甚至没有办法测试上面的代码。
下面是一个完整的例子:
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();
}
}
});
}
}
- 另一种选择是使用
scroller.setModel(textField.getHorizontalVisibility());
- 这种方法的优点是,
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();
// }
// }