我正在创建一个程序,随机配对人们进行刺客游戏。在配对人之后,它应该通过电子邮件告诉他们他们配对的“目标”是谁。 我知道当你运行它时我的面板看起来很乱,但我的主要问题是我用来提醒人们他们的“目标”的JavaMail API无效。 我的IDE不接受JavaMail作为导入。我从GitHub下载了必要的文件,但它仍然无效。这是我的代码:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AGP extends JPanel {
int playCount = 0;
ArrayList<String> players = new ArrayList<String>();
ArrayList<String> Emails = new ArrayList<String>();
ArrayList<String> GMs = new ArrayList<String>();
ArrayList<String> GMEs = new ArrayList<String>();
JButton addPlayer = new JButton("add Player");
JButton startGame = new JButton("Start Game");
JLabel pC = new JLabel("Players: " + playCount);
JLabel pN = new JLabel("Name here");
JLabel eM = new JLabel("email here");
GridBagConstraints c = new GridBagConstraints();
JTextField name = new JTextField();
JTextField email = new JTextField();
GridBagLayout layout = new GridBagLayout();
JButton GM = new JButton("Add Game Master");
public AGP() {
addPlayer.addActionListener(new Listener1());
startGame.addActionListener(new Listener2());
GM.addActionListener(new Listener3());
setLayout(layout);
c.gridwidth = 300;
c.ipadx = 385;
c.ipady = 20;
c.gridx = 2;
c.gridy = 0;
add(pC, c);
c.ipadx = 275;
c.ipady = 50;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
add(pN, c);
c.gridx = 2;
c.gridy = 2;
add(name, c);
c.gridx = 2;
c.gridy = 3;
add(eM);
c.gridx = 2;
c.gridy = 4;
add(email);
c.gridx = 1;
c.gridy = 2;
add(addPlayer);
c.gridx = 2;
c.gridy = 3;
add(startGame);
c.gridx = 2;
c.gridy = 4;
add(GM);
}
private class Listener1 implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (!(name.getText() == "") && !(email.getText() == "")) {
players.add(name.getText());
Emails.add(email.getText());
playCount++;
name.setText("");
email.setText("");
pC.setText("Players: " + playCount);
} else {
pC.setText("Missing name or email");
}
}
}
private class Listener2 implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (players.size() % 2 == 0 && Emails.size() % 2 == 0) {
String[] atk = new String[players.size()];
String[] attackers = new String[players.size()];
String[] targets = new String[players.size()];
players.toArray(atk);
for (int x = 1; x < playCount; x++) {
attackers[x] = atk[(int) (Math.random() * (atk.length + 1))];
for (int y = 1; y < attackers.length; y++) {
if (attackers[x] == attackers[y]) {
attackers[x] = atk[(int) (Math.random() * (atk.length + 1))];
}
}
for (int z = 1; z < playCount; z++) {
targets[z] = atk[(int) (Math.random() * (atk.length + 1))];
for (int r = 1; r < targets.length; r++) {
if (targets[z] == targets[r]) {
targets[z] = atk[(int) (Math.random() * (atk.length + 1))];
}
}
}
String[] ems = new String[Emails.size()];
String[] gMEs = new String[GMEs.size()];
GMEs.toArray(gMEs);
Emails.toArray(ems);
for(int g =1; g<attackers.length(); g++{ for(int h =1;
h<atk.length; h++){ if(attackers[g]==players[h]){ String
host = "localhost" Properties properties =
System.getProperties(); Session session =
Session.getDefaultInstance(properties); try{ MimeMessage
message = new MimeMessage(session); message.setFrom(new
InternetAddress(gMEs[(int)(Math.random()*(gMEs.size()+1)]
)); message.addRecipient(Message.RecipientType.TO, new
InternetAddress(ems[h]));
message.setSubject("Game at CNU 'Assasins'");
message.setText("Welcome to the game "+attackers[h]
+"Your first target is: "+target[h]+"have fun!");
Transport.send(message); } message.setRecipients("");
mex.printStackTrace(); } } } } for(int p=1; p<gMEs.size;
p++){ message.addRecipient(Message.RecipientType.TO, new
InternetAddress(gMEs[p])); message.setText(for(int x=1;
x<=attackers.length()x++){attackers[x]+"is looking for"
+targets[x]}; Transport.send(message); }
catch(MessagingException mex){ mex.printStackTrace(); } }
} } }
}
}
}
}
private class Listener3 implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (!(name.getText() == "") && !(email.getText() == "")) {
GMs.add(name.getText());
GMEs.add(email.getText());
name.setText("");
email.setText("");
pC.setText("Players: " + playCount);
} else {
pC.setText("Missing name or email");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Assasins Game generator v1");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setContentPane(new AGP());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如何让Eclipse接受导入?