我有这个类(class)作为我的问题的一个例子。滚动 Pane 不滚动,我看不出一个很好的理由:
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class DoesNotScroll{
public static void main(String[] args){
String str = "this\n\nshould\n\n\n\nscroll\n\n\nthis is the bottom";
message("test", str, JOptionPane.INFORMATION_MESSAGE);
}
public final static void message(String title, String message, int messageType){
JTextArea messageArea = new JTextArea();
messageArea.setMinimumSize(new Dimension(300, 100));
messageArea.setMaximumSize(new Dimension(300, 100));
messageArea.setPreferredSize(new Dimension(300, 100));
messageArea.setEditable(false);
JScrollPane scroller = new JScrollPane(messageArea);
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
messageArea.setText(message);
messageArea.revalidate();
messageArea.repaint();
JOptionPane.showMessageDialog(null, scroller, title, messageType);
}
}
感谢任何帮助,
-C
最佳答案
The problem with my setup here was that I was calling
messageArea.setPreferredSize()
instead ofscroller.setPreferredSize()
在这种情况下,需要调用其中任何一个都表明存在问题。通过在构造函数中提供列/行大小来设置文本区域的“首选大小”。将其添加到滚动 Pane 中。工作完成。
import javax.swing.*;
public class DoesScroll {
public static void main(String[] args){
String str = "this\n\nshould\n\n\n\nscroll\n\n\nthis is the bottom";
message("test", str, JOptionPane.INFORMATION_MESSAGE);
}
public final static void message(String title, String message, int messageType){
JTextArea messageArea = new JTextArea(3,20);
messageArea.setEditable(false);
JScrollPane scroller = new JScrollPane(messageArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
messageArea.setText(message);
JOptionPane.showMessageDialog(null, scroller, title, messageType);
}
}
关于Java : Why doesn't this scrollpane scroll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590278/