【发布时间】:2014-11-16 12:51:28
【问题描述】:
我创建了一个 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 中不显示任何文本。请帮忙!
怀疑:我是否定义了一个新的 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>
【问题讨论】:
-
是否抛出异常?
-
@SanjayManohar 没有,这使它成为一个更大的问题。
-
@SanjayManohar 你认为 Netbeans 对我命名为“log”的 JTextPane 感到困惑吗?它认为我不是在定义一个新的 JTextPane 吗?
-
好吧,除非您发布更多代码,否则无法判断!你还没有向我们展示定义
log的代码,或者added 到jFrame。 -
我使用 NetBeans IDE。由于我使用拖放插件,它会自动生成 Swing GUI 代码。不过,这是为了您的观看乐趣。
标签: java string swing colors jtextpane