我创建了一个JTextArea,以在程序运行时以多种颜色显示消息。 我使用NetBeans 8.0,在编写代码之前,我在JFrame中添加了一个名为“log”的JTextArea。代码拒绝在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?
更新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>
是否抛出任何异常? – 2014-09-22 14:21:01
@SanjayManohar没有,这使得它更成为一个问题。 – 2014-09-22 14:21:31
@SanjayManohar你认为Netbeans被我命名为“log”的JTextPane困惑了吗?当我不在的时候,它是否认为我正在定义一个新的JTextPane? – 2014-09-22 14:23:08