繁体   English   中英

可以将JLabel添加到JTextArea吗?

[英]can JLabel be added to JTextArea?

有没有一种方法可以将JLabel添加到JtextArea? 因为我尝试add但没有成功,并将其设置为visible 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);

每次添加消息时,是否可以在JTextArea内附加JLabel?

JTextArea不是在其中插入JLabels或任何其他组件的适当的swing组件。 您可以为此目的使用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();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM