我创建了一个带有jLabel和jList的Netbeans GUI。我试图将Gmail中的邮件主题显示到jList中,但没有任何内容出现。这是我的代码:
public class FilterExistingUI extends javax.swing.JFrame {
public FilterExistingUI() {
initComponents();
}
static FilterExistingUI gmail = new FilterExistingUI();
@SuppressWarnings("unchecked")
//program-generated private void initComponents() code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
gmail.read();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FilterExistingUI().setVisible(true);
}
});
gmail.read();
}
public void read() {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
//props.load(new FileInputStream(new File("C:\\smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "gmail_id@gmail.com","password");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages:- " + messageCount);
jLabel4.setText(Integer.toString(messageCount));
Message[] messages = inbox.getMessages();
System.out.println("------------------------------");
DefaultListModel listmodel = (DefaultListModel)inboxList.getModel();
listmodel.clear();
for (int i = 0; i < messageCount; i++) {
System.out.println("Mail Subject:- " + messages[i].getSubject());
listmodel.addElement("Subject:- " + messages[i].getSubject());
}
inboxList.setModel(listmodel);
inbox.close(true);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println
打印输出窗口中的列表,但是当我运行程序时,邮件主题列表不会显示在inboxList(我的jList)中,并且消息计数不显示在jLabel4中。我还在模型属性的自定义模式中添加了inboxList.setModel(new DefaultListModel());
。我是Java的新手,也是Netbeans的新手,所以我很困惑我在这里做错了什么。它在DefaultListModel()
部分吗?或者在jList部分?或者在其他地方?