📄 firstmail.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.mail.*;
public class FirstMail extends JFrame implements ActionListener{
static FirstMail pop;
JPanel pPanel, radioPanel, rightPanel, upperPanel, lowerPanel, mailcontentPanel, inputPanel, mailPanel, deletePanel;
JPanel alogoffPanel,connectingPanel;
JTextField userField, hostField, maincontent, mailnoField;
JPasswordField passField;
JRadioButton inputButton, latestButton, viewButton, disconnectButton;
JButton okButton, logoutButton, mailnoButton, delemailButton;
JLabel label, userlabel, passlabel, hostlabel, connectlabel, maillabel, disconnectlabel, dslabel;
JLabel enternolabel, connlabel, connhostlabel, connuserlabel, latestlabel;
JTextArea textArea;
ButtonGroup group;
String hostname, username, password;
int n;
public FirstMail(){
//Create and set up the window.
setTitle("POP3 Client");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
setLocation(250,150);
//Create and set up the panel.
pPanel = new JPanel(new BorderLayout());
addWidgets();
add(pPanel);
setVisible(true);
}
public void addWidgets(){
radioB();
pPanel.add(radioPanel, BorderLayout.WEST);
rightPanel = new JPanel();
rightPanel.setLayout(new BorderLayout());
upperP();
lowerP();
rightPanel.add(upperPanel, BorderLayout.NORTH);
rightPanel.add(lowerPanel, BorderLayout.CENTER);
pPanel.add(rightPanel, BorderLayout.CENTER);
}
public void upperP(){
upperPanel = new JPanel();
upperPanel.setLayout(new BorderLayout());
upperPanel.setBackground(Color.pink);
label = new JLabel("Please choose your action.");
upperPanel.add(label);
}
public void lowerP(){
lowerPanel = new JPanel();
lowerPanel.setLayout(new BorderLayout());
lowerPanel.setBackground(new Color(255,255,180));
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imageURL = FirstMail.class.getResource(path);
if (imageURL == null) {
System.err.println("Resource not found: "
+ path);
return null;
} else {
return new ImageIcon(imageURL);
}
}
public void radioB(){
inputButton = new JRadioButton("Input Information");
inputButton.setBackground(new Color(100,200,255));
viewButton = new JRadioButton("View Email");
viewButton.setBackground(new Color(100,200,255));
latestButton = new JRadioButton("Latest Email");
latestButton.setBackground(new Color(100,200,255));
disconnectButton = new JRadioButton("Disconnect");
disconnectButton.setBackground(new Color(100,200,255));
group = new ButtonGroup();
group.add(inputButton);
group.add(viewButton);
group.add(latestButton);
group.add(disconnectButton);
inputButton.addActionListener(this);
viewButton.addActionListener(this);
latestButton.addActionListener(this);
disconnectButton.addActionListener(this);
ImageIcon images = createImageIcon("mail.jpg");
JLabel icon = new JLabel(images);
icon.setBorder(BorderFactory.createEmptyBorder(20,20,30,10));
//Put the radio buttons in a column in a panel.
radioPanel = new JPanel();
radioPanel.setPreferredSize(new Dimension(150,500));
radioPanel.setBackground(new Color(100,200,255));
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
radioPanel.add(icon);
radioPanel.add(inputButton);
radioPanel.add(viewButton);
radioPanel.add(latestButton);
radioPanel.add(disconnectButton);
}
public void inputP(){
rightPanel.remove(upperPanel);
inputPanel = new JPanel();
inputPanel.setLayout(null);
hostlabel = new JLabel("Hostname: ");
hostlabel.setBounds(20,5,140,20);
hostField = new JTextField(40);
hostField.setBounds(100,5,180,20);
userlabel = new JLabel("Username: ");
userlabel.setBounds(20,32,140,20);
userField = new JTextField(20);
userField.setBounds(100,32,140,20);
passField = new JPasswordField(20);
passField.setBounds(100,60,140,20);
passlabel = new JLabel("Password: ");
passlabel.setBounds(20,60,140,20);
connectlabel = new JLabel("");
connectlabel.setBounds(300,5,140,20);
okButton = new JButton("Connect");
okButton.setBounds(30,90,90,20);
okButton.addActionListener(this);
inputPanel.add(hostlabel);
inputPanel.add(userlabel);
inputPanel.add(passlabel);
inputPanel.add(hostField);
inputPanel.add(userField);
inputPanel.add(passField);
inputPanel.add(okButton);
inputPanel.add(connectlabel);
inputPanel.setPreferredSize(new Dimension(150,120));
upperPanel = inputPanel;
rightPanel.add(upperPanel, BorderLayout.NORTH);
this.show();
}
public void connectingP(){
rightPanel.remove(upperPanel);
connectingPanel = new JPanel();
connectingPanel.setLayout(null);
connlabel = new JLabel("<html><font color=grey size=5>You are currently connecting to a POP server...</font></html>");
connlabel.setBounds(20,10,500,40);
connhostlabel = new JLabel("Host: " + hostname);
connhostlabel.setBounds(20,50,200,20);
connuserlabel = new JLabel("Username: " + username);
connuserlabel.setBounds(20,70,200,20);
connectingPanel.add(connlabel);
connectingPanel.add(connhostlabel);
connectingPanel.add(connuserlabel);
connectingPanel.setPreferredSize(new Dimension(150,120));
upperPanel = connectingPanel;
rightPanel.add(upperPanel, BorderLayout.NORTH);
this.show();
}
public void viewP(){
rightPanel.remove(upperPanel);
mailPanel = new JPanel();
mailPanel.setLayout(null);
if (Mail1.con == true){
n = Mail1.stat();
maillabel = new JLabel("<html><b><u><font color=blue size=4>Inbox: " + n + " messages (Please click here to view)</font></u></b></html>");
maillabel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
mailcontentP();
}
});
}else{
maillabel = new JLabel("<html><b><u><font color=red size= 5>You have not logged in!!!</font></u></b></html>");
}
maillabel.setBounds(20,32,350,40);
mailPanel.add(maillabel);
mailPanel.setPreferredSize(new Dimension(150,120));
upperPanel = mailPanel;
rightPanel.add(upperPanel, BorderLayout.NORTH);
this.show();
}
public void deleteP(){
rightPanel.remove(upperPanel);
deletePanel = new JPanel();
deletePanel.setLayout(null);
disconnectlabel = new JLabel("Are you sure you want to logout?");
disconnectlabel.setBounds(20,32,200,20);
logoutButton = new JButton("disconnect");
logoutButton.setBounds(40,70,120,20);
logoutButton.addActionListener(this);
dslabel = new JLabel("");
dslabel.setBounds(120,70,120,20);
deletePanel.add(disconnectlabel);
deletePanel.add(logoutButton);
deletePanel.add(dslabel);
deletePanel.setPreferredSize(new Dimension(150,120));
upperPanel = deletePanel;
rightPanel.add(upperPanel, BorderLayout.NORTH);
this.show();
}
public void mailcontentP(){
rightPanel.remove(lowerPanel);
mailcontentPanel = new JPanel();
mailcontentPanel.setLayout(null);
mailcontentPanel.setBackground(new Color(255,255,180));
enternolabel = new JLabel("Enter email no: ");
enternolabel.setBounds(20,32,200,20);
mailnoField = new JTextField(5);
mailnoField.setBounds(150,32,200,20);
mailnoButton = new JButton("View");
mailnoButton.setBounds(400,32,100,20);
mailnoButton.addActionListener(this);
delemailButton = new JButton("Delete");
delemailButton.setBounds(500, 32, 100, 20);
delemailButton.addActionListener(this);
textArea = new JTextArea("");
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Email Content"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));
areaScrollPane.setLocation(50,80);
areaScrollPane.setSize(550,350);
mailcontentPanel.add(enternolabel);
mailcontentPanel.add(mailnoField);
mailcontentPanel.add(mailnoButton);
mailcontentPanel.add(delemailButton);
mailcontentPanel.add(areaScrollPane);
lowerPanel = mailcontentPanel;
rightPanel.add(lowerPanel, BorderLayout.CENTER);
this.show();
}
public void alogoffP(){
rightPanel.remove(upperPanel);
rightPanel.remove(lowerPanel);
lowerP();
alogoffPanel = new JPanel();
alogoffPanel.setLayout(null);
maillabel = new JLabel("<html><b><u><font color=red size= 5>You have not logged in!!!</font></u></b></html>");
maillabel.setBounds(20,32,400,40);
alogoffPanel.add(maillabel);
alogoffPanel.setPreferredSize(new Dimension(150,120));
upperPanel = alogoffPanel;
rightPanel.add(upperPanel, BorderLayout.NORTH);
rightPanel.add(lowerPanel, BorderLayout.CENTER);
this.show();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == inputButton){
if (Mail1.con == false)
inputP();
else connectingP();
}else if (e.getSource() == viewButton){
System.out.println("good");
viewP();
}else if (e.getSource() == okButton){
hostname = hostField.getText();
username = userField.getText();
password = new String(passField.getPassword());
Mail1.connect(hostname, username, password, connectlabel, pop);
if (Mail1.con == true)
connectingP();
}else if (e.getSource() == disconnectButton){
if (Mail1.con == true)
deleteP();
else alogoffP();
}else if (e.getSource() == logoutButton){
Mail1.disconnect();
alogoffP();
maillabel.setText("<html><b><u><font color=red size= 5>You have already logoff!!!</font></u></b></html>");
}else if (e.getSource() == mailnoButton){
boolean check = Mail1.viewm(mailnoField.getText());
textArea.setText(Mail1.sb.toString());
}else if (e.getSource() == delemailButton){
boolean check2 = Mail1.delem(mailnoField.getText());
textArea.setText(Mail1.sb.toString());
}else if (e.getSource() == latestButton){
viewP();
if (Mail1.con == true){
maillabel.setText("<html><b><u><font color=blue size=4>Latest Email</font></u></b></html>");
mailcontentP();
mailnoField.setText("" + n);
boolean check = Mail1.viewm("" + n);
textArea.setText(Mail1.sb.toString());
maillabel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
mailcontentP();
mailnoField.setText("" + n);
boolean check = Mail1.viewm("" + n);
textArea.setText(Mail1.sb.toString());
}
});
this.show();
}
}
}
public static void createAndShowGUI(){
pop = new FirstMail();
}
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -