Java Swing - JTextPane 中插入组件的问题
下面给出的程序在单击按钮时插入一个新的 JTextArea。每次新的 TextArea 都会插入新的 Line 中。该程序在 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
}
The below given program insert a new JTextArea on button click. Everytime new TextArea is inserted in new Line. The program is working fine on Linux machine but not on windows machine. On windows machine it is giving me BadLocationException . Can anyone please tell me the solution of this problem. Thanks in advance.
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真的打算在每次调用
actionPerformed()
时执行insertComponent()
吗?我本来期望更像这样的内容:附录:将此与您的 进行比较关于同一主题的上一个问题,您应该考虑是否要在滚动窗格中插入文本或文本组件。此外,NetBeans 的 GUI 设计器已经相当成熟,但它不太适合简短的示例。
Do you really mean to do
insertComponent()
each timeactionPerformed()
gets called? I'd have expected something more like this:Addendum: Comparing this with your previous question on the same topic, you should consider whether you want to insert text or a text component in the scroll pane. Also, NetBeans' GUI designer has matured considerably, but it doesn't lend itself well to short examples.