我创建了一个登录applet,我想在用户登录后打开另一个主applet,但我似乎无法让它正常工作。我尝试制作主applet的对象,然后调用它但是没有用,所以我尝试为新applet创建另一个窗格并将其设置在登录applet中但是没有用。有人能告诉我我做错了什么吗?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.applet.Applet;
public class login extends Applet implements ActionListener{
FundRaiserApplet secondApplet;
JLabel uname = new JLabel("Username");
JLabel pname = new JLabel("Password");
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JButton login = new JButton("Login");
static final String USER = "identityone";
static final String PASS = "test";
JLabel status = new JLabel("");
JPanel fundraiserPanel;
public void init(){
setSize(250,200);
add(uname);
add(username);
add(pname);
add(password);
add(login);
add(status);
login.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == login){
//status.setText("Password or Username is incorrect");
String user = username.getText();
String pass = password.getText();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Credentials;user=test;password=test");
Statement stmt = conn.createStatement();
Statement credR = conn.createStatement();
String credentialSet = "INSERT INTO Credentials" +"( username, password)" + " VALUES('" + USER +"' , '" + PASS +"' )";
stmt.executeUpdate(credentialSet);
ResultSet ret = credR.executeQuery("SELECT username , password FROM Credentials");
while (ret.next()) {
if(user.equals(ret.getString("username")) && pass.equals(ret.getString("password"))) {
//didnt work
FundRaiserApplet runme = new FundRaiserApplet();
runme.init();
JPanel container = new JPanel();
container.setLayout(new GridLayout(1,0));
container.add(secondApplet);
add(container);
secondApplet.init();
secondApplet.start();
secondApplet.setVisible(true);
status.setText("Logged in");
// this.dispose();
//FundRaiserApplet.init();
}else{
status.setText("Wrong Username or Password");
}
}
ret.close();
} catch(Exception ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
}
}
}
// MAIN CLASS BELOW ////////////////////
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.applet.Applet;
public class FundRaiserApplet extends Applet {
//database creation
// static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//static final String DB_URL = "jdbc:derby://localhost/";
//end database creation
ArrayList<String> resultsHold = new ArrayList<>();
private JLabel DonorNamelabel;
private JLabel AmountLabel;
private JLabel charityName;
private JLabel emptycell;
static final String USER = "identityone";
static final String PASS = "test";
private JTextField donorNameTexfield;
private JTextField Amounttextfield;
private JTextField singletextfield;
private JTextArea resultsscreen;
public JButton seeAll;
public JButton myExitButton;
public JButton Enter;
public JButton ClearButton;
JPanel centerPanel;
JPanel buttonPanel;
JPanel textfieldPanel;
JPanel myresultsPanel;
JPanel results1Panel;
//JPanel container = new JPanel();
public void init() {
setSize(400, 325);
DonorInfo donor = new DonorInfo();
String[] charityselections = { "Wounded Warrior Project","American Red Cross", "Doctors Without Borders","American Cancer Society","PETA"};
final JComboBox<String> chairtycombo = new JComboBox<>(charityselections);
chairtycombo.setVisible(true);
//creating text fields, labels and buttons
emptycell = new JLabel("");
DonorNamelabel = new JLabel("Donor Name",10);
AmountLabel = new JLabel("Pledge Amount",10);
charityName = new JLabel ("Charity Name",10);
donorNameTexfield = new JTextField(8);
Amounttextfield = new JTextField(8);
singletextfield = new JTextField(35);
resultsscreen = new JTextArea(7, 35);
final JScrollPane myScroll = new JScrollPane(resultsscreen);
myExitButton = new JButton("Exit");
myExitButton.setSize(new Dimension(10, 10));
Enter = new JButton("Enter Info");
Enter.setSize(new Dimension(10, 10));
ClearButton= new JButton("Clear");
ClearButton.setSize(new Dimension(10, 10));
seeAll = new JButton("See All Results");
seeAll.setSize(new Dimension(10, 10));
seeAll.setMargin(new Insets(2, 0, 2, 0));
//adding panels
results1Panel = new JPanel();
centerPanel = new JPanel();
buttonPanel = new JPanel();
textfieldPanel = new JPanel();
myresultsPanel = new JPanel();
//adding labels
centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 67, 0));
centerPanel.add(DonorNamelabel);
centerPanel.add(AmountLabel);
centerPanel.add(charityName);
// adding text fields
textfieldPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 3));
textfieldPanel.add(donorNameTexfield);
textfieldPanel.add(Amounttextfield);
textfieldPanel.add(chairtycombo);
//adding the buttons to the panel
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
buttonPanel.add(seeAll);
buttonPanel.add(myExitButton);
buttonPanel.add(Enter);
buttonPanel.add(ClearButton);
myresultsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
myresultsPanel.add(emptycell);
myresultsPanel.add(myScroll);
myresultsPanel.add(emptycell);
myresultsPanel.add(emptycell);
results1Panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 2));
results1Panel.add(singletextfield);
add(centerPanel, BorderLayout.NORTH);
add(textfieldPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(results1Panel, BorderLayout.NORTH);
add(myresultsPanel, BorderLayout.SOUTH);
//clear button action event listener
ClearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
donorNameTexfield.setText("");
Amounttextfield.setText("");
singletextfield.setText("");
resultsscreen.setText("");
resultsHold.clear();
}
});
//end clear listener
Enter.addActionListener(new ActionListener() {
int counter=1;
@Override
public void actionPerformed(ActionEvent e)
{
singletextfield.setText("");
double amountc = Double.parseDouble(Amounttextfield.getText());
String charityselection = (String) chairtycombo.getSelectedItem();
donor.setName(donorNameTexfield.getText());
donor.setCharityname(charityselection);
donor.setAmount(amountc);
String name = donor.getName();
Double amount = donor.getAmount();
String charity = donor.getCharityName();
String text ="Donor Name is:" +name +"\n"+"Pledge Amount: $" + amount + "\n"+"Charity Name: "+charity+"\n"+"-----------------------------------------------";
//Ading the text variable into array
resultsHold.add(text);
//displaying results in textarea
singletextfield.setText("Donor Name is:" +name +" Pledge Amount: $" + amount + "..." + "Charity Name: "+charity);
try
{
// Create a connection to the database.
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Fundraiser;user=test;password=test");
Statement stmt = conn.createStatement();
String sqlStatement = "INSERT INTO Fundraiser" +"( NAME, PLEDGE_AMOUNT,CHARITY)" + " VALUES('" + name +"' , " + amount +" , '" + charity +"' )";
stmt.executeUpdate(sqlStatement);
System.out.println("Records Updated.");
// Close the connection.
conn.close();
System.out.println("Connection closed.");
}
catch(Exception ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
// resetting text fields
donorNameTexfield.setText("");
Amounttextfield.setText("");
counter++;
}
});
//event listeners
myExitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//exit program
System.exit(0);
}
});
seeAll.addActionListener((ActionEvent e) -> {
try{
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Fundraiser;user=test;password=test");
Statement stmt = conn.createStatement();
String sqlStatement = "SELECT * FROM Fundraiser";
ResultSet result = stmt.executeQuery(sqlStatement);
while (result.next())
{
resultsscreen.append("Pledge Name: " + result.getString("NAME") + "\n"+
"Amount Given: "+result.getDouble("PLEDGE_AMOUNT") +"\n"+
"Donor Charity: " + result.getString("CHARITY")+"\n"+ "........................."+ "\n");
}
// Close the connection.
conn.close();
} catch(Exception ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
//resultsscreen.append(text.toString());
});
}
public static void main(String[] args) {
//database initialization
Connection conn = null;
Connection conn2 = null;
Statement stmt = null;
Statement start = null;
try{
//downloaded driver "mysql-connector-java-5.1.18-bin.jar" and added into project library
//downloaded "derbyclient.jar" and added to library
Class.forName("com.mysql.jdbc.Driver").newInstance ();
//connection time!
System.out.println("Connecting to database...");
//if a folder already exist in "C:\Users\User\.netbeans-derby" called "Fundraiser" then delete it or it will throw an exception
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Fundraiser;create=true;user=test;password=test");
conn2 = DriverManager.getConnection("jdbc:derby://localhost:1527/Credentials;create=true;user=test;password=test");
//Create database
System.out.println("Fundraiser Database Created!");
stmt = conn.createStatement();
start = conn2.createStatement();
try{
String sql1 = "DROP TABLE Fundraiser";
String dropcreds = "DROP TABLE Credentials";
stmt.executeUpdate(sql1);
System.out.println("Fundraiser Table Deleted...");
start.executeUpdate(dropcreds);
System.out.println("Credentials Table Deleted...");
}catch(Exception e){System.out.println("No Table to Delete...");}
String sql = "CREATE TABLE Fundraiser" +
"(NAME VARCHAR(255), " +
" PLEDGE_AMOUNT DOUBLE, " +
" CHARITY VARCHAR(255) )";
String sql2 = "CREATE TABLE Credentials" +
"(username VARCHAR(255), " +
" password VARCHAR(255) )";
stmt.executeUpdate(sql);
System.out.println("Fundraiser Table created successfully...");
start.executeUpdate(sql2);
System.out.println("Credential Table Created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException my2){
System.out.println(my2);
}
try{
if(conn!=null)
conn.close();
}catch(SQLException my){
my.printStackTrace();
}
}//end try
FundRaiserApplet applet = new FundRaiserApplet();
applet.init();
JFrame frame = new JFrame("Identity One");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( applet );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
applet.start();
//login.main(args);
}
public void ShowWindow() {
FundRaiserApplet applet = new FundRaiserApplet();
applet.init();
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( applet );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
applet.start();
}
}
答案 0 :(得分:0)
您可以尝试使用javascript在网页中打开第二个小程序。