【问题标题】:removing recently add JLabel on JTextPane删除最近在 JTextPane 上添加 JLabel
【发布时间】:2013-05-15 02:39:54
【问题描述】:

您好,我正在使用 JTextPane 中的 JLabel 制作在线用户名列表。

我使用 JLabel 是因为我希望名称可以点击我能够使用 styleddocument 水平对齐它们现在我的问题是

如何删除最近插入的 JLabel ?我尝试了 JTextPane 的 remove 方法,但没有用。当用户下线时,我需要删除 JLabel。

我的代码:

public static void getUsernames()
{
    try{
    String query = "SELECT username FROM members WHERE status = 'offline'";
    ps3 = con.prepareStatement(query);
    rs2 = ps3.executeQuery();

    }catch(Exception ex){ex.printStackTrace();}

}
public static void resultGetUsername(JTextPane jtp,StyledDocument sd)
{
    try {
        while (rs2.next())
        {

            final JLabel jl = new JLabel(rs2.getString("username"));
            final String username = rs2.getString("username");
            Border d = BorderFactory.createEmptyBorder(1,10,1,10);
            Border d2 = BorderFactory.createLineBorder(Color.BLACK);
            Border d3 = BorderFactory.createCompoundBorder(d2,d);
            jl.setFont(new Font("Calibri",Font.BOLD,16));
            jl.setBorder(d3);
            jl.setOpaque(true);
            jl.setBackground(Color.ORANGE);
            jl.addMouseListener(new MouseListener(){

                public void mouseClicked(MouseEvent arg0) {
                }

                public void mouseEntered(MouseEvent arg0) {
                    jl.setForeground(new Color(30,144,255));
                }

                public void mouseExited(MouseEvent arg0) {
                    jl.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent e) {
                    jl.setForeground(new Color(210,105,30));
                    jl.setBackground(new Color(154,205,50));

                }

                public void mouseReleased(MouseEvent e) {
                    jl.setBackground(Color.ORANGE);
                    jl.setForeground(Color.BLACK);
                    if(e.getClickCount() ==2)
                        new OneToOneChat(username);
                }


            });

            Cursor c = new Cursor(Cursor.HAND_CURSOR);
            jl.setCursor(c);
            jtp.insertComponent(jl);
            sd.insertString(sd.getLength(), "\n", SubPanel1.sas);           
        }
    } catch (SQLException e) {

    } catch (BadLocationException e) {

    }
    finally{
        if (rs2 != null) {
            try {
                rs2.close();
            } catch (SQLException sqlEx) { } 

            rs2 = null;
        }

    if (ps3 != null) {
            try {
                ps3.close();
            } catch (SQLException sqlEx) { } 

            ps3 = null;
        }
    }
}

【问题讨论】:

  • 让我吃惊的是,使用滚动窗格行标题来实现同样的事情会更容易
  • 我怎样才能做一个滚动窗格的行标题?请给个链接
  • 请参阅如何使用滚动窗格
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • @trashgod 提到的article 非常好。他通常会添加链接。通过谷歌很容易找到(热门),但我只想添加链接是docs.oracle.com/javase/tutorial/uiswing/components/…

标签: java swing jlabel jtextpane


【解决方案1】:

您可以通过使用从 JTextPane 中删除标签 setText("")getStyledDocument().remove(0, doc.getLength())

如果您需要获取标签,这篇文章可能会有所帮助:Get a component from a JTextPane through javax.swing.text.Element?

然后将你想要的标签添加回 JTextPane

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多