gpt4 book ai didi

java - 在 JTextPane 中转换为大写或小写时,文本样式和格式发生变化

转载 作者:行者123 更新时间:2023-12-02 10:28:51 25 4
gpt4 key购买 nike

我正在创建一个简单的 RTF 文档编辑器。我添加了代码来创建新的 RTF 文档、打开 RTF 文档、保存文档和其他格式设置功能,例如粗体、斜体、下划线和更改大小写(大写更改为小写,反之亦然)。我正在使用 JTextPane 组件。

我的问题:虽然 JTextPane 的内容包含不同的字体和不同颜色的不同字体大小,但如果我选择所有内容并选择“大写”菜单选项,内容将更改为大写,但所有不同的字体,字体大小和颜色正在更改为相同的字体、相同的字体大小和相同的颜色,验证图像:转换为大写之前:

enter image description here

选择所有内容并选择“大写”选项:

enter image description here

转换为大写后:

enter image description here

我该如何解决这个问题?提前致谢。最少代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

public class MyNotepadMini implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public Document document;
public JScrollPane scrollPane;
public JToolBar toolBar;
public StyledDocument styledDocument;
public Style defaultStyle;
public AttributeSet attrs;
public Style style;
public MutableAttributeSet mas;
public JButton lowerAndUpperCaseBtn;
public JPopupMenu popupMenu;
public JMenuItem lowerCaseMI;
public JMenuItem upperCaseMI;
public SimpleAttributeSet simpleAttrs;

public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}

frame = new JFrame("My Wordpad");
panel = new JPanel(new BorderLayout());
toolBar = new JToolBar();
toolBar.setBounds(0,0,100,30);

rtf = new RTFEditorKit();
textPane = new JTextPane();
textPane.setEditorKit(rtf);
textPane.setMargin(new Insets(10,5,5,5));
styleContext = new StyleContext();
mas = textPane.getInputAttributes();
simpleAttrs = new SimpleAttributeSet();
styledDocument = textPane.getStyledDocument();
textPane.setDocument(styledDocument);

scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);

lowerAndUpperCaseBtn = new JButton("Change Case");
lowerAndUpperCaseBtn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int startPosition = 0;
int endPosition = 0;
if ((textPane.getSelectionStart() != textPane.getSelectionEnd())) {
startPosition = textPane.getSelectionStart();
endPosition = textPane.getSelectionEnd();
textPane.setSelectionStart(startPosition);
textPane.setSelectionEnd(endPosition);
textPane.getCaret().setSelectionVisible(true);
}
}
});
popupMenu = new JPopupMenu();
lowerCaseMI = new JMenuItem("lowercase");
upperCaseMI = new JMenuItem("UPPERCASE");
popupMenu.add(lowerCaseMI);
popupMenu.add(upperCaseMI);

lowerAndUpperCaseBtn.addActionListener(this);
lowerCaseMI.addActionListener(this);
upperCaseMI.addActionListener(this);

toolBar.setFloatable(false);
toolBar.add(lowerAndUpperCaseBtn);
toolBar.setBackground(Color.WHITE);
scrollPane.setPreferredSize(new Dimension(600,400));

textPane.setFont(new Font("Arial", Font.PLAIN, 12));
textPane.setInheritsPopupMenu(true);
panel.add(toolBar, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);

frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
new MyNotepadMini();
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == lowerAndUpperCaseBtn) {
popupMenu.show(lowerAndUpperCaseBtn, 0, lowerAndUpperCaseBtn.getBounds().height);
} else if (ae.getSource() == lowerCaseMI) {
boolean lowerCaseFlag = false;
int startPosition = 0;
int endPosition = 0;
try {
if ((textPane.getSelectionStart() != textPane.getSelectionEnd()) && (!lowerCaseFlag)) {
startPosition = textPane.getSelectionStart();
endPosition = textPane.getSelectionEnd();
System.out.println("Selected text: " + textPane.getSelectedText());
textPane.replaceSelection(textPane.getSelectedText().toLowerCase());
textPane.setSelectionStart(startPosition);
textPane.setSelectionEnd(endPosition);
textPane.getCaret().setSelectionVisible(true);
lowerCaseFlag = true;
}
lowerAndUpperCaseBtn.setFocusable(false);
textPane.requestFocus();
} catch (Exception e) {
e.printStackTrace();
}
} else if (ae.getSource() == upperCaseMI) {
boolean upperCaseFlag = false;
int startPosition = 0;
int endPosition = 0;
try {
if ((textPane.getSelectionStart() != textPane.getSelectionEnd()) && (!upperCaseFlag)) {
startPosition = textPane.getSelectionStart();
endPosition = textPane.getSelectionEnd();
System.out.println("Selected text: " + textPane.getSelectedText());
textPane.replaceSelection(textPane.getSelectedText().toUpperCase());
textPane.setSelectionStart(startPosition);
textPane.setSelectionEnd(endPosition);
textPane.getCaret().setSelectionVisible(true);
upperCaseFlag = true;
}
lowerAndUpperCaseBtn.setFocusable(false);
textPane.requestFocus();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

最佳答案

当您用 textPane.replaceSelection 替换所选内容时,所选文本将被删除,然后使用当前输入样式添加新文本。

要实现您想要的效果,您需要使用方法 setCharacterAttributes 更新所选内容的字符属性。由文档提供。

关于java - 在 JTextPane 中转换为大写或小写时,文本样式和格式发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com