【问题标题】:How to strike through text in JTextArea in java?如何在java中的JTextArea中删除文本?
【发布时间】:2015-07-27 18:34:51
【问题描述】:

我正在尝试在 JTextArea 中添加文本。 但我需要删除一些文本,并按原样添加一些文本。 我在互联网上搜索过,但找不到任何答案。 有什么可以参考的帮助吗?

【问题讨论】:

标签: java swing jtextarea


【解决方案1】:

JTextArea 允许您设置字体样式,但您不能将样式设置为部分文本。请看下面的代码,你可以使用setFont方法来指定带有strikethru风格的字体,但它适用于JTextArea中的所有文本:

JTextArea area = new JTextArea();
Font font = new Font("arial", Font.PLAIN, 12);
Map fontAttr = font.getAttributes();
fontAttr.put (TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Font myFont = new Font(fontAttr);
area.setFont (myFont);
area.setText ("Hello");

然而,如果您希望某些文本在删除线中,而另一些则不是,那么您必须使用 JTextPaneStyledDocument,但我不建议这样做,因为您需要进行大量调整才能以特定样式显示您的内容.下面的代码可能会给你一些想法:

DefaultStyledDocument doc = new DefaultStyledDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("strikethru", null);
StyleConstants.setStrikeThrough (style,true);
doc.insertString (0, "Hello ", null);
doc.insertString (6, "strike through ", style);
JTextPane pane = new JTextPane(doc);

【讨论】:

    【解决方案2】:

    您可以使用以下代码。这里有一些罢工文本“@11@”、“@22@”和一些添加文本“@yicHFRx1nc@”、“@icHFRx1nc@”来替换罢工文本。

    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class JTextAreaExample {
    
        private JFrame mainFrame;
        private JLabel headerLabel;
        private JLabel statusLabel;
        private JPanel controlPanel;
    
        public JTextAreaExample() {
            prepareGUI();
        }
    
        public static void main(String[] args) {
            JTextAreaExample swingControlDemo = new JTextAreaExample();
            swingControlDemo.showTextAreaDemo();
        }
    
        private void prepareGUI() {
            mainFrame = new JFrame("JTextArea Example");
            mainFrame.setSize(400, 400);
            mainFrame.setLayout(new GridLayout(3, 1));
            mainFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
            headerLabel = new JLabel("", JLabel.CENTER);
            statusLabel = new JLabel("", JLabel.CENTER);
    
            statusLabel.setSize(350, 100);
    
            controlPanel = new JPanel();
            controlPanel.setLayout(new FlowLayout());
    
            mainFrame.add(headerLabel);
            mainFrame.add(controlPanel);
            mainFrame.add(statusLabel);
            mainFrame.setVisible(true);
        }
    
        private void showTextAreaDemo() {
            headerLabel.setText("JTextArea");
    
            JLabel descriptionLabel = new JLabel("Description: ", JLabel.RIGHT);
    
            final JTextArea descriptionTextArea = new JTextArea("Enter String ", 5, 20);
    
            JScrollPane scrollPane = new JScrollPane(descriptionTextArea);
    
            JButton showButton = new JButton("Show");
    
            showButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String b = descriptionTextArea.getText().replace("@11@", "@yicHFRx1nc@")
                            .replace("@22@", "@icHFRx1nc@");
                    System.out.println("b=" + b);
                    statusLabel.setText(b);
                }
            });
    
            controlPanel.add(descriptionLabel);
            controlPanel.add(scrollPane);
            controlPanel.add(showButton);
            mainFrame.setVisible(true);
        }
    
    }
    

    【讨论】:

    • Rafiq - 你的答案有什么相关性?问题是关于设置一些文本通过删除和一些文本正常。不适用于替换 textarea 中的字符串。
    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多