【问题标题】:Adding a JButton to a JTextPane将 JButton 添加到 JTextPane
【发布时间】:2013-04-04 18:35:14
【问题描述】:

我在尝试使用字符串将 JButton 添加到 JTextPane 时遇到问题。所以我想做的是在for循环中添加每个字符串,然后在添加的字符串之后添加广告JButton。 下面的代码是我想要完成的。

ArrayLst<String> data = new ArrayList();
data.add("Data here");
data.add("Data here 2");
data.add("Data here 3");
data.add("Data here 4");

Container cp = getContentPane();

JTextPane pane = new JTextPane();
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setBold(set, true);
pane.setBackground(Color.BLUE);
pane.setEditable(false);

Document doc = pane.getStyledDocument();

for(int i=0; i<data.size(); i++)
{
    doc.insertString(doc.getLength(), data.get(i)+ "\n", set);
    pane.insertComponent(new JButton("View Info"));
}

谁能告诉我如何在同一行的每个字符串中添加一个 JButton?

非常感谢

【问题讨论】:

    标签: java swing jbutton jtextpane


    【解决方案1】:

    你可以这样试试:

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    
    class TextPaneDemo extends JFrame
    {
        public void createAndShowGUI()throws Exception
        {
            JTextPane tp = new JTextPane();
            ArrayList<String> data = new ArrayList();
            data.add("Data here");
            data.add("Data here 2");
            data.add("Data here 3");
            data.add("Data here 4");
            getContentPane().add(tp);
            setSize(300,400);
            StyledDocument doc = tp.getStyledDocument();
            SimpleAttributeSet attr = new SimpleAttributeSet();
            for (String dat : data )
            {
                doc.insertString(doc.getLength(), dat, attr );
                tp.setCaretPosition(tp.getDocument().getLength());
                tp.insertComponent(new JButton("Click"));
                doc.insertString(doc.getLength(), "\n", attr );
            }
    
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    TextPaneDemo tpd = new TextPaneDemo();
                    try
                    {
                        tpd.createAndShowGUI(); 
                    }
                    catch (Exception ex){}
                }
            });
        }
    }
    

    【讨论】:

    • 非常感谢您的回复。它就像一个魅力。非常感谢
    【解决方案2】:

    谁能告诉我如何在每个字符串上添加一个 JButton 同一行?

    • doc.insertString(doc.getLength(), data.get(i)+ "\n", set); 中删除LineSeparator ("\n")

    伪代码可以是

    for (int i = 0; i < data.size(); i++) {
        try {
            doc.insertString(doc.getLength(), data.get(i), set);
            textPane.insertComponent(new JButton("View Info"));
            doc.insertString(doc.getLength(), "\n", set);
        } catch (BadLocationException ex) {
        }    
    }
    
    • 带输出

    【讨论】:

    • 感谢您的回复。输出有点乱,但我解决了。非常感谢
    猜你喜欢
    • 2011-05-06
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多