在我的代码中,我正在尝试使用文本框创建滚动条。我有文本框但我无法滚动它。我一直在寻找新的解决方案,但它们都不起作用。如果你知道什么是错的可以帮助我
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);
}
}
答案 0 :(得分:2)
您需要将滚动窗格添加到框架中:
//jp.add(jt);
jp.add(myScrollPane);
您还需要确保水平滚动条始终可见,以便可以计算滚动窗格的正确高度。
此外,当您发布代码时,请确保发布可编译的代码。你发布的代码毫无意义,你甚至无法测试上面的代码。
答案 1 :(得分:1)
这是一个完整的例子:
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();
}
}
});
}
}
答案 2 :(得分:1)
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();
// }
// }