سؤال

البرنامج المعطى أدناه إدراج jtextarea جديد في الزر انقر. في كل مرة يتم إدراج نص جديد في خط جديد. يعمل البرنامج بشكل جيد على جهاز 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
}
هل كانت مفيدة؟

المحلول

هل تقصد حقا أن تفعل insertComponent() كل مرة actionPerformed() يتم استدعاء؟ كنت أتوقع شيئًا مثل هذا:

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

إضافة: مقارنة هذا مع السؤال السابق حول نفس الموضوع ، يجب أن تفكر فيما إذا كنت تريد الإدراج نص أو أ مكون النص في جزء التمرير. أيضًا ، نضج مصمم واجهة المستخدم الرسومية لـ NetBeans إلى حد كبير ، لكنه لا يضع نفسه جيدًا في أمثلة قصيرة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top