我创建了一个JTextArea,以便在程序运行时以多种颜色显示消息。 我使用NetBeans 8.0,在编写代码之前,我在jFrame中添加了一个名为“log”的jTextArea。
首先,我定义了一个名为apppane
的类:
private void apppane(JTextPane log, String msg, Color c)
{
/*This allows multi-colour inside the logging pane*/
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = log.getDocument().getLength();
log.setCaretPosition(len);
log.setCharacterAttributes(aset, false);
log.replaceSelection(msg);
}
接下来,我编写了代码来显示颜色文本:
/*time1 gets the current system time and it works perfectly, no errors there*/
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Thread t1;
FTPClient cli=new FTPClient();
FTPClientConfig conf=new FTPClientConfig();
boolean err=false;
try{
String ServAddress="195.191.24.202";
int reply;
TimeNow time1=new TimeNow();
apppane(log,time1.whatsthetime()+": Connecting to "+ServAddress+"\n",Color.RED);
System.out.println(time1.whatsthetime()+": Connecting to "+ServAddress+"\n");
cli.connect(ServAddress);
cli.configure(conf);
TimeNow time2=new TimeNow();
apppane(log,time2.whatsthetime()+": Connected to "+ServAddress,Color.BLUE);
System.out.println(time2.whatsthetime()+": Connected to "+ServAddress+"\n");
System.out.println(time2.whatsthetime()+": "+cli.getReplyString());
reply=cli.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
cli.disconnect();
TimeNow time3=new TimeNow();
apppane(log,time3.whatsthetime()+": Connection rejected. \n", Color.RED);
System.out.println(time3.whatsthetime()+": Connectiion failed \n");
}
log.setText(ServAddress);
}
catch (Exception e){
e.printStackTrace();
}
}
但是,JTextPane中没有文字出现。求救!
SUSPICION:我不定义新的JTextPane吗?
UPDATE 1:这是NetBeans生成的Swing GUI代码:
private void initComponents() {
dochello = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
log = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("MayvilFTP");
getContentPane().setLayout(null);
dochello.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
dochello.setText("Welcome, Dr. ");
getContentPane().add(dochello);
dochello.setBounds(10, 30, 350, 30);
jButton1.setText("Connect");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(370, 30, 110, 23);
log.setEditable(false);
log.setOpaque(false);
jScrollPane2.setViewportView(log);
getContentPane().add(jScrollPane2);
jScrollPane2.setBounds(10, 230, 480, 110);
pack();
}// </editor-fold>
答案 0 :(得分:2)
在整个jButton1ActionPerformed
方法返回之前,您的文本不会显示。
您的代码在Swing事件线程中运行。这是实际绘制组件的相同线程...并处理所有其他事件。因此,当FTP正在进行时,您的GUI将基本上被冻结。
如果您正在执行需要一些时间的操作,则应该在新线程中启动它。您不应在按钮按下事件中执行FTP操作。
您创建变量Thread t1
但从不使用它。为什么不在这个帖子中使用FTP,并在适当的时候使用java.awt.EventQueue.invokeLater
来呼叫apppane
?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Thread t1 = new Thread(new Runnable() { public void run(){
apppane_threadsafe( ... );
// do your ftp stuff
apppane_threadsafe( ... );
}});
t1.start();
}
和
private void apppane_threadsafe(JTextPane log, String msg, Color c){
EventQueue.invokeLater(new Runnable() { public void run() {
apppane(log, msg, c);
}
}}
答案 1 :(得分:1)
简短回答: setEditable(false)
和replaceSelection()
无法合作。
答案很长:检查replaceSelection()
中的JTextPane
方法。它仅适用于可编辑的JTextPane。
@Override
public void replaceSelection(String content) {
replaceSelection(content, true);
}
private void replaceSelection(String content, boolean checkEditable) {
if (checkEditable && !isEditable()) {
UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this);
return;
}
...
}
最简单的解决方案是将窗格设置为可编辑,进行更改并设置回不可编辑。但这很难看
更好的解决方案是操纵基础Document
。