Java JScrollBar没有按我想要的方式运行
所以我有一个滚动条,一个JTextPane和代码,当文本到达边缘时,可以将文本推到下一行,但是有几个问题。JScrollBar的大小不会超过特定的大小,任何超过该大小的文本都将不再可见(因为它在屏幕外)并注释掉textArea.setPreferredSize(新维度(4001500));导致代码下降到下一行,无法工作。这是我的代码Java JScrollBar没有按我想要的方式运行,java,swing,jtextpane,jscrollbar,Java,Swing,Jtextpane,Jscrollbar,所以我有一个滚动条,一个JTextPane和代码,当文本到达边缘时,可以将文本推到下一行,但是有几个问题。JScrollBar的大小不会超过特定的大小,任何超过该大小的文本都将不再可见(因为它在屏幕外)并注释掉textArea.setPreferredSize(新维度(4001500));导致代码下降到下一行,无法工作。这是我的代码 import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.aw
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class WordFrame{
private JFrame appFrame;
private JMenuBar menuBar;
private JMenu fileMenu, editMenu, viewMenu;
JMenuItem saveMenuItem, openMenuItem, newMenuItem, exitMenuItem, fontMenuItem;
JTextPane textArea = new JTextPane();
static final int WIDTH = 1280, HEIGHT = 980;
private JScrollPane scrollBar = new JScrollPane(textArea);
JPanel wordPanel = new JPanel();
JFileChooser fileChooser = new JFileChooser();
private int textHeight = 12;
private Font defaultFont = new Font(Font.SANS_SERIF, 2, textHeight);
public WordFrame(){
appFrame = new JFrame();
setUI();
addMenuBar();
textArea.setFont(defaultFont);
}
public JFrame getAppFrame(){
return appFrame;
}
public void setFrameVisibility(boolean isVisible){
if(isVisible == true){
appFrame.setVisible(true);
}
else{
appFrame.setVisible(false);
}
}
public void setUI(){
appFrame.setTitle("Word Processor");
appFrame.setIconImage(new ImageIcon(this.getClass().getResource("Bridge.jpg")).getImage());
appFrame.setSize(WIDTH, HEIGHT);
appFrame.setLocation(0,0);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
wordPanel.setLayout(new BorderLayout());
//1500 allows for scrollbar thing, if i could make this dynamic it would be sweet
textArea.setPreferredSize(new Dimension(400,1500));
appFrame.add(wordPanel);
appFrame.add(scrollBar);
wordPanel.add(textArea);
// appFrame.add(textArea);
scrollBar.setViewportView(wordPanel);
textArea.setEditorKit(new WrapEditorKit());
}
public void addMenuBar(){
menuBar = new JMenuBar();
fileMenu = new JMenu(" File ");
editMenu = new JMenu("Edit ");
viewMenu = new JMenu("View ");
newMenuItem = new JMenuItem("New");
fileMenu.add(newMenuItem);
fileMenu.addSeparator();
fileMenu.setMnemonic('f');
openMenuItem = new JMenuItem("Open");
fileMenu.add(openMenuItem);
saveMenuItem = new JMenuItem("Save");
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
exitMenuItem = new JMenuItem("Exit");
fileMenu.add(exitMenuItem);
fontMenuItem = new JMenuItem("Font");
editMenu.add(fontMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
appFrame.setJMenuBar(menuBar);
}
public void setFontSize(int i){
this.textHeight = i;
}
public void addListener(ActionListener listener){
openMenuItem.addActionListener(listener);
exitMenuItem.addActionListener(listener);
saveMenuItem.addActionListener(listener);
fontMenuItem.addActionListener(listener);
}
@SuppressWarnings("serial")
class WrapEditorKit extends StyledEditorKit {
ViewFactory defaultFactory=new WrapColumnFactory();
@Override
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
class WrapColumnFactory implements ViewFactory {
@Override
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new WrapLabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
class WrapLabelView extends LabelView {
public WrapLabelView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
}
试着用另一种方式来解释,以防我上面的描述不是很好。假设您使用的是MicroSoft Word,当您键入一个句子时,文本将位于文本区域最右侧,此时您将自动跳转到下一行。最后三种方法(WrapColumnFactory、WrapLabelView、getMinimumSpan(int-axis))是建议我修复一个对我来说不起作用的bug的代码片段,因为目前对于新行bug似乎没有任何其他解决方案。下一个问题是JScrollBar,在启动应用程序时,垂直JScrollBar就在那里,正如我所希望的,但是JScrollBar只会滚动到从上到下占据JScrollBar允许滚动的文本区域,您可以继续键入,但JScrollBar不会增加,也就是说,您看不到超出该点的任何文本,就像它达到最大高度一样。在我决定注释掉textArea.setPreferredSize(新尺寸(4001500))后,我希望滚动条的高度不会受到限制,但实际情况是,最后3种修复新行错误的方法没有任何作用,因此文本继续从屏幕上消失,垂直滚动条的大小没有增加,也没有自动新行。
非常感谢您一如既往的帮助。不要为JTextPane设置首选大小 而是重写根视图的方法(ViewFactory返回的剖面视图) 返回X轴和Y轴所需跨度的步骤 例如,查看如何实现分页 这里
在这里很抱歉花了我的时间回复,似乎我在UI设计的许多方面都有很多需要学习的地方,比如分页、编辑和其他方面,但感谢您的回复,一旦我了解了某些事情,我将尝试处理您提到的内容。
public float getPreferredSpan(int axis)