我知道有人对这个问题提出了类似的问题,但经过几个小时的研究,我没有找到解决办法。
我的问题是为什么我的JTextPane上没有滚动条显示。下面是我的代码:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
public class OtherNotesWindow extends JFrame{
JTextPane page;
JPanel panel;
public OtherNotesWindow() {
super("Other Notes Window");
init();
page.setFocusable(true);
this.setSize(400,400);
this.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(400,400);
}
public void init(){
panel = new JPanel();
page = new JTextPane();
JScrollPane scroll = new JScrollPane(page, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setViewportView(page);
panel.add(scroll);
this.add(page);
}
}我不知道为什么没有滚动条,无论是垂直的还是水平的,都没有出现。谁能告诉我原因吗?
预先感谢任何回复的人。:)
发布于 2014-08-06 01:37:21
仔细看看你的代码..。
// Create a panel...
panel = new JPanel();
// Create a scroll pane
page = new JTextPane();
// Create a scroll pane with page as the view
JScrollPane scroll = new JScrollPane(page, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// A little redudent as you did this when you created the scoll pane, but what ever
scroll.setViewportView(page);
// Add the scroll pane to the panel
panel.add(scroll);
// Add the page to this...wait what??!
this.add(page);最后一行是您要取消的,它是从page中移除JScrollPane,并简单地添加到JFrame...no滚动窗格或其他任何内容。
你得把它改成.
this.add(panel);或
this.add(scroll);请记住,JPanel默认使用FlowLayout,这可能不符合您的要求。
发布于 2014-08-06 01:03:43
this.add(page)应该是this.add(panel)
https://stackoverflow.com/questions/25150646
复制相似问题