簡體   English   中英

Java Swing-JTextPane中的問題插入組件

[英]Java Swing - problem insert component in JTextPane

下面給出的程序在單擊按鈕時插入一個新的JTextArea。 每次將新的TextArea插入到新行中。 該程序在Linux機器上工作正常,但在Windows機器上工作不正常。 在Windows機器上,它給了我BadLocationException。 誰能告訴我這個問題的解決方案。 提前致謝。

import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

public class TestPane extends javax.swing.JFrame {

/** Creates new form TestPane */
StyledDocument doc = null;
public TestPane() {
    initComponents();
    doc = jTextPane1.getStyledDocument();
    //BoxLayout messageLayout = new BoxLayout(jScrollPane1,BoxLayout.PAGE_AXIS);
    //jTextPane1.setLayout(messageLayout);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextPane1 = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jScrollPane1.setViewportView(jTextPane1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(164, 164, 164)
                    .addComponent(jButton1))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                 286, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButton1)
            .addContainerGap(31, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jTextPane1.insertComponent(new JTextArea("hi this is StackOverflow "));
    jTextPane1.revalidate();
    insertEmptyLable();
}


private void insertEmptyLable() {
    try {
        doc.insertString
                (jTextPane1.getText().length(), "\n",
                null);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TestPane().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
// End of variables declaration
}

您真的要在每次調用actionPerformed()時執行insertComponent()嗎? 我原本期望這樣的事情:

private void insertMessage() {
    try {
        doc.insertString(jTextPane1.getText().length(),
           "Hi, this is StackOverflow.\n", null);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

附錄:將此與上一個主題相同的問題進行比較,應考慮是要在滾動窗格中插入文本還是文本組件 同樣,NetBeans的GUI設計器已經相當成熟,但是它並不能很好地適用於簡短的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM