我正在使用文字处理器,我希望允许用户执行所有文字处理器允许他们执行的操作。我现在的问题是试图了解一旦达到最大宽度(帧的大小),如何强制用户写入的文本到下一行。例如,如果我正在写一个句子(或关于堆栈溢出的问题),一旦我的文本到达文本区域的边界,它就会自动将我带到下一行而不按Enter / return。在对线条包装进行一些研究后,我遇到了以下情况 http://www.java-forums.org/awt-swing/59790-line-wrapping-jtextpane.html Toggling text wrap in a JTextpane
然而我似乎无法从下面的JTextPane获得我想要的行为是我的代码,因为我总是欣赏任何提供的帮助。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
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(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// scrollbar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
wordPanel.setLayout(new BorderLayout());
appFrame.add(wordPanel);
textArea.setPreferredSize(new Dimension(400,500));
appFrame.add(scrollBar);
wordPanel.add(textArea);
scrollBar.setViewportView(wordPanel);
}
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);
}
}
插入我在互联网上找到的代码后。一旦边缘(右)被文本击中,文本就会换行(基本上如果它到达应用程序屏幕的最右边,它会自动下降到一个新行。但是出现了一个新问题,这涉及到JScrollBar,因为每个人都会期待,一旦文本点击应用程序屏幕的底部,滚动条就会更新,以便用户能够向上和向下滚动页面,这是有道理的,但是我的代码不允许这样做。这是我之后的修改后的代码尝试修复换行问题(我仍然认为这是同一个问题),如果以下内容被注释掉,文字换行不再起作用,则会更多
textArea.setPreferredSize(new Dimension(400,500));
那里的代码导致文本换行完全失败。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
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(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
wordPanel.setLayout(new BorderLayout());
// appFrame.getContentPane().add(scrollBar, BorderLayout.CENTER);
textArea.setPreferredSize(new Dimension(400,500));
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);
}
class WrapEditorKit extends StyledEditorKit {
ViewFactory defaultFactory=new WrapColumnFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
}
class WrapColumnFactory implements ViewFactory {
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);
}
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);
}
}
}
}