0

请看下面的代码

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,但它显示为空白,发送电子邮件后,它的内容就在那里!为什么是这样?请帮忙!

4

2 回答 2

3

您的代码不是SSCCE,那么

1.SendEmailForm();

  • 消除this.validate();

  • this.setVisible(true);作为最后一个代码行移动

  • 前移this.pack();_this.setVisible(true);

  • 前移this.setLocationRelativeTo(null);_this.setVisible(true);

2.public ProgressMonitor() {可能是

代码

    private class ProgressMonitor extends JDialog {

        public ProgressMonitor() {
            setLayout(new BorderLayout());
            JLabel text = new JLabel("Sending..Please wait...");
            add(text, "Center");
            pack();
            setLocationRelativeTo(null);
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    setVisible(true);
                }
            });
        }
    }

.

3.rest 不清楚你的问题

  • JComponents添加到可见容器中,那么最后的代码行应该是(re)validate()repaint()

  • JDialog用于下一个操作,设置HIDE_on_CLOSE为默认关闭操作JDialog

  • 不要扩展JDialog,将此contianer创建为局部变量,然后您只能(my)JDialog.setVisible(true在下一个操作(包装在)中调用invokeLater()

于 2012-09-28T15:43:25.533 回答
0

您已经创建了许多组件,但尚未将它们添加到JFrame您的应用程序中。在使用GrigBagLayout时,您需要为每个组件设置约束,例如:

subject = new JLabel("Subject: ");
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbl.setConstraints(subject, gbc);
add(subject);

也不要忘记先实例化布局和约束:

private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
于 2012-09-28T15:50:38.450 回答