0

私は現在、これを使用してにJLabel埋め込まれていJTextPaneます:

import javax.swing.*;
import javax.swing.text.*;

public class MainFrame
{
    JFrame mainFrame = new JFrame("Main Frame");
    JTextPane textPane = new JTextPane();

    public MainFrame()
    {
        String[] components = {"Title", "\n"};
        String[] styles = {"LABEL_ALIGN", "LEFT_ALIGN"};

        StyledDocument sd = textPane.getStyledDocument();
        Style DEFAULT_STYLE = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

        Style LEFT_STYLE = sd.addStyle("LEFT_ALIGN", DEFAULT_STYLE);
        StyleConstants.setAlignment(LEFT_STYLE, StyleConstants.ALIGN_LEFT);

        Style CENTER_STYLE = sd.addStyle("CENTER_ALIGN", DEFAULT_STYLE);
        StyleConstants.setAlignment(CENTER_STYLE, StyleConstants.ALIGN_CENTER);

        JLabel titleLbl = new JLabel("Title");
        Style LABEL_STYLE = sd.addStyle("LABEL_ALIGN", DEFAULT_STYLE);
        StyleConstants.setAlignment(LABEL_STYLE, StyleConstants.ALIGN_CENTER);
        StyleConstants.setComponent(LABEL_STYLE, titleLbl);

        for(int i = 0; i < components.length; i++)
        {
            try
            {
                sd.insertString(sd.getLength(), components[i], sd.getStyle(styles[i]));
                sd.setLogicalStyle(sd.getLength(), sd.getStyle(styles[i]));
            }
            catch(BadLocationException e)
            {
                e.printStackTrace();
            }
        }

        mainFrame.add(textPane);
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(MainFrame::new);
    }
}

ラベルを削除できないようにするにはどうすればよいですか? バックスペースを押すたびに、ラベルがJTextPane

4

2 に答える 2