可以将 JLabel 添加到 JTextArea 吗?

Pol*_*erk 0 java swing jlabel jtextarea

有没有办法将 JLabel 添加到 JtextArea?因为我尝试add但它不起作用并将其设置为可见 true,是否允许在 JTextArea 内添加 JLabel?通过追加?

这是我当前没有用的代码

jta = new JTextArea();
jta.setEditable(false);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setFont(new Font("calibri", Font.PLAIN,16));
jlArray = new JLabel("radsjhkaljk sadf");
jta.add(jlArray);
jta.setVisible(true);
jspTextField = new JScrollPane(jta);
Run Code Online (Sandbox Code Playgroud)

每次添加消息时,我可以在 JTextArea 中附加 JLabel 吗?

Vis*_*l K 5

JTextArea不是swing要插入的适当组件JLabels或其中的任何其他组件。您可以JTextPane为此目的使用。例如,考虑下面给出的代码: 在此处输入图片说明

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;

class JLabelToTextPane extends JFrame implements ActionListener
{
    JTextField tf;
    JTextPane tp ;
    JButton click;
    StyledDocument doc;
    SimpleAttributeSet attr;
    public void createAndShowGUI()
    {
        setTitle("Add JLabel to JTextPane");
        tf = new JTextField(10);
        tp = new JTextPane();
        click = new JButton("Click");
        doc = tp.getStyledDocument();
        attr = new SimpleAttributeSet();
        JScrollPane pane = new JScrollPane(tp);
        JPanel nPanel = new JPanel();
        nPanel.add(tf);nPanel.add(click);
        tf.addActionListener(this);
        click.addActionListener(this);
        Container c = getContentPane();
        c.add(nPanel,BorderLayout.NORTH);
        c.add(pane);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,200);setLocationRelativeTo(null);setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent evt)
    {
        String text =  tf.getText();
        if (text!=null && !"null".equals(text) && !"".equals(text.trim()))
        {
            JLabel label = new JLabel(text);
            label.setOpaque(true);
            label.setBackground(Color.gray);
            label.setBorder(BorderFactory.createLineBorder(Color.black,1));
            tp.setCaretPosition(tp.getDocument().getLength());
            tp.insertComponent(label);
            label.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    String text = ((JLabel)evt.getSource()).getText();
                    JOptionPane.showMessageDialog(JLabelToTextPane.this,"Hi, My text is "+text,"Information",JOptionPane.INFORMATION_MESSAGE);
                }
            });
            try
            {
                doc.insertString(doc.getLength(), " ", attr );  
            }
            catch (BadLocationException ex)
            {
                ex.printStackTrace();
            }
        }
    }
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                JLabelToTextPane lta = new JLabelToTextPane();
                lta.createAndShowGUI();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)