请看下面的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
public class SendEmailForm extends JDialog {
private JLabel to, cc, bcc, subject, account;
private JTextField toTxt, ccTxt, bccTxt, subjectTxt;
private JTextArea messageTxt;
private JButton send;
private JComboBox accountBox;
private JScrollPane scroll;
private GridBagLayout gbl;
private GridBagConstraints gbc;
public SendEmailForm() {
//Declaring instance variables
to = new JLabel("To: ");
cc = new JLabel("CC: ");
bcc = new JLabel("BCC: ");
subject = new JLabel("Subject: ");
account = new JLabel("Select an Account: ");
toTxt = new JTextField(20);
ccTxt = new JTextField(20);
bccTxt = new JTextField(20);
subjectTxt = new JTextField(20);
messageTxt = new JTextArea(20, 50);
messageTxt.setLineWrap(true);
scroll = new JScrollPane(messageTxt);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
accountBox = new JComboBox();
accountBox.addItem("Yahoo");
accountBox.addItem("GMail");
accountBox.addItem("MSN");
//accountBox.addItem("Yahoo");
//accountBox.addItem("Yahoo");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
send = new JButton("Send");
send.addActionListener(new SendButtonAction());
buttonPanel.add(send);
//Creating thr GUI
//GUI CREATION IS REMOVED IN THIS POST
this.setTitle("Send Emails");
this.setVisible(true);
this.pack();
this.setLocationRelativeTo(null);
this.validate();
}
private class SendButtonAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
ProgressMonitor pm = new ProgressMonitor();
//Retreiving the user name and password
List userData = new ArrayList();
EmailDBConnector emailCon = new EmailDBHandler();
userData = emailCon.getUserNameAndPassword(
accountBox.getSelectedItem().toString().trim());
String userName = userData.get(0).toString();
String password = userData.get(1).toString();
System.out.println(userName);
System.out.println(password);
pm.setVisible(true);
SendEmail sendEmail = new SendEmail(toTxt.getText(), userName.trim(),
bccTxt.getText(), ccTxt.getText(), accountBox.getSelectedItem().toString().trim(), messageTxt.getText().trim(),
password.trim(), subjectTxt.getText());
String result = sendEmail.send();
//pm.dispose();
JOptionPane.showMessageDialog(null, result);
}
}
private class ProgressMonitor extends JDialog {
public ProgressMonitor() {
this.setLayout(new BorderLayout());
JLabel text = new JLabel("Sending..Please wait...");
this.add(text, "Center");
this.pack();
this.validate();
this.setLocationRelativeTo(null);
}
}
}
首先,这是一个电子邮件程序。在这里,当调用 JDialog 时,它只是作为 100% 空白窗口打开。我添加了一个 JLabel,但它在显示时不存在。无论如何,发送电子邮件需要一些时间,发送电子邮件后,我可以在JDialog中看到JLabel。如果我把我的问题放在一句话中,我在发送电子邮件之前调用了 JDialog,但它显示为空白,发送电子邮件后,它的内容就在那里!为什么是这样?请帮忙!