创建用于撰写电子邮件消息的GUI

时间:2016-04-21 15:56:11

标签: java email user-interface jframe

我正在尝试创建用于撰写电子邮件的应用程序。当按下发送按钮时,应用程序打印出来自用户输入的To,Cc,Bcc,主题和消息。由于某些原因,当按下按钮时,它给了我一个错误"线程中的异常" AWT-EventQueue-0" 这是我的代码:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class EmailWindow extends JPanel {
    private JTextField to, cc, bcc, subject;
    private JTextPane content;
    private JButton send;
    public EmailWindow() {
        // Construct and add text fields for to, cc, bcc, and subject, followed
        // by a Send button. You may use the createComponentWithLabel(...)
        // utility method to construct a panel that includes a label and
        // a text field, which can then be added to the EmailWindow panel.
        // For example,
        // add(createComponentWithLabel("Text", new JTextField(30));
        // would add a text field next to a label with the word "Text".

        add(createComponentWithLabel("to", new JTextField(30)));
        add(createComponentWithLabel("cc", new JTextField(30)));
        add(createComponentWithLabel("bcc", new JTextField(30)));
        add(createComponentWithLabel("subject", new JTextField(30)));

        // The JTextPane class supports multi-line text. For a single line
        // of content text, you could use another JTextField instead.

        setBackground(Color.cyan);
        content = new JTextPane();
        content.setPreferredSize(new Dimension(375, 200));
        send = new JButton("Send");
        send.addActionListener(new SendListener());
        add(content);
        add(send);
    }

    // -----------------------------------------------------------------------
    // Utility method (which you may use in the constructor) that creates
    // and returns a <code>JPanel</code> containing a <code>JLabel</code>
    // next to an arbitrary component, such as a <code>JTextField</code>.
    // -----------------------------------------------------------------------
    private JPanel createComponentWithLabel(String label, Component comp) {
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(new JLabel(label, JLabel.RIGHT), BorderLayout.WEST);
        p.add(comp, BorderLayout.CENTER);
        return p;
    }

    // -----------------------------------------------------------------------
    // Listener class to be attached to the Send button. When the button
    // is pressed, the contents of the to, cc, bcc, subject, and contents
    // fields will be printed to standard out.
    // -----------------------------------------------------------------------
    private class SendListener implements ActionListener {

        public void actionPerformed(ActionEvent evt) {
            System.out.println("To: " + to.getText());
            System.out.println("Cc: " + cc.getText());
            System.out.println("Bcc: " + bcc.getText());
            System.out.println("Subject: " + subject.getText());
            System.out.println("Message content: "+content.getText());
            System.out.println(content.getText());

        }
    }
}

主要课程:

   import javax.swing.JFrame;

    public class email {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // Create a frame
        JFrame frame = new JFrame("Compose Message");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create an instance of EmailWindow and add it to the frame.
        EmailWindow email = new EmailWindow(); 
        frame.getContentPane().add(email); 
        // Set a reasonable starting size for the frame. Note that we 
        // do not use pack() here, since doing so with the default layout 
        // manager would produce a very long frame. Other layout managers 
        // (which will be discussed in Chapter 6) would solve this problem 
        // in a more flexible way.
        frame.setSize(425, 400); 
        // Show the frame. 
        frame.setVisible(true); 
        } 

}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hw6.EmailWindow$SendListener.actionPerformed(EmailWindow.java:59)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:4)

cc bcc 主题 JTextFieldnull

你永远不会初始化它们,你可能想把它们作为参数传递给这部分:

add(createComponentWithLabel("to", new JTextField(30)));
add(createComponentWithLabel("cc", new JTextField(30)));
add(createComponentWithLabel("bcc", new JTextField(30)));
add(createComponentWithLabel("subject", new JTextField(30)));

我建议:

   to = new JTextField(30);
   cc = new JTextField(30);
   bcc = new JTextField(30);
   subject = new JTextField(30);


   add(createComponentWithLabel("to", to));
   add(createComponentWithLabel("cc", cc));
   add(createComponentWithLabel("bcc", bcc));
   add(createComponentWithLabel("subject", subject));