You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助开发Java GUI邮件编辑界面(非代写作业)

Java GUI Email Composition Window for Beginners

Hey there! Since you're new to Java GUI development, I'll share a complete, clean Swing implementation that checks all your boxes. Swing is the standard beginner-friendly toolkit for Java GUIs, and this code will give you a functional, polished starting point.

Full Working Code

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

public class EmailComposer extends JFrame {
    // Core UI components
    private JTextField emailToField;
    private JTextField subjectField;
    private JTextArea contentArea;
    private JCheckBox importantCheckBox;

    public EmailComposer() {
        // Set up main window
        setTitle("Compose Email");
        setSize(600, 450);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centers window on screen
        setLayout(new BorderLayout(10, 10)); // Add padding between sections

        // Add input panel and button panel to the window
        add(createInputPanel(), BorderLayout.CENTER);
        add(createButtonPanel(), BorderLayout.SOUTH);
    }

    // Builds the panel with all input fields and labels
    private JPanel createInputPanel() {
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // Padding around each component
        gbc.anchor = GridBagConstraints.WEST;

        // "To:" Label & Text Field
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("To:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        emailToField = new JTextField(30);
        inputPanel.add(emailToField, gbc);

        // "Subject:" Label & Text Field
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        inputPanel.add(new JLabel("Subject:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        subjectField = new JTextField(30);
        inputPanel.add(subjectField, gbc);

        // "Content:" Label & Text Area (with scroll support)
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        inputPanel.add(new JLabel("Content:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        contentArea = new JTextArea(15, 30);
        contentArea.setLineWrap(true);
        contentArea.setWrapStyleWord(true);
        inputPanel.add(new JScrollPane(contentArea), gbc);

        // "Flag as Important" Checkbox
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        gbc.weighty = 0;
        importantCheckBox = new JCheckBox("Flag as Important");
        inputPanel.add(importantCheckBox, gbc);

        return inputPanel;
    }

    // Builds the panel with action buttons
    private JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));

        JButton sendButton = new JButton("Send");
        JButton saveButton = new JButton("Save for later");
        JButton discardButton = new JButton("Discard");

        // Add placeholder logic for button actions
        sendButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(null, "Sending email to: " + emailToField.getText());
            // Replace this with real email-sending logic (e.g., JavaMail API) later
        });

        saveButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(null, "Email saved for later!");
            // Add code here to save content to a file/database
        });

        discardButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(
                null,
                "Are you sure you want to discard this email?",
                "Confirm Discard",
                JOptionPane.YES_NO_OPTION
            );
            if (confirm == JOptionPane.YES_OPTION) {
                // Clear all fields
                emailToField.setText("");
                subjectField.setText("");
                contentArea.setText("");
                importantCheckBox.setSelected(false);
            }
        });

        buttonPanel.add(sendButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(discardButton);

        return buttonPanel;
    }

    public static void main(String[] args) {
        // Run GUI on Swing's Event Dispatch Thread (required for safety)
        SwingUtilities.invokeLater(() -> new EmailComposer().setVisible(true));
    }
}

Key Details You’ll Care About

  • Input Components: Uses JTextField for single-line inputs (email/subject) and JTextArea with a scroll pane for multi-line content (so users can scroll long messages).
  • Clear Labels: Each input has a descriptive label aligned neatly with its field.
  • Action Buttons: All three required buttons are included with basic placeholder logic—you can swap out the JOptionPane calls with real functionality later.
  • Important Checkbox: A simple JCheckBox lets users mark emails as high-priority.
  • Clean Layout: GridBagLayout ensures components are properly aligned and resize nicely when the window is adjusted, while BorderLayout keeps the input area and button bar separated logically.

How to Get Started

  1. Save this code as EmailComposer.java.
  2. Compile it with: javac EmailComposer.java
  3. Run it with: java EmailComposer

Next Steps to Expand

  • Add input validation (e.g., check if the email address follows a valid format).
  • Implement actual email sending using the JavaMail API (you’ll need to add the necessary libraries to your project).
  • For "Save for later", write the email content to a text file or lightweight database.
  • Add error handling for edge cases (like empty required fields).

内容的提问来源于stack exchange,提问作者Michael Hansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:55:26