我目前正在制作一个 gui 程序,但我对如何设置程序一无所知,所以当用户输入用户名和密码,然后单击登录按钮时,它会检查数据库中的用户名和密码,并且如果用户名和密码在数据库中,他们接受,如果没有拒绝。
这是我的欢迎屏幕,在我打算整理它的那一刻它很糟糕。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author
*/
public class Welcome extends JFrame {
private JButton Existing, New, Exit;
private JLabel Welcome, Date, Version;
private JPanel WelcomeP;
private JMenuItem jmiNew, jmiExisting, jmiExit, jmiAbout;
public Welcome() {
//create menu bar
JMenuBar regMenuBar = new JMenuBar();
//set menu bar to the applet
setJMenuBar(regMenuBar);
//add menu "operation" to menu bar
JMenu optionsMenu = new JMenu("Options");
optionsMenu.setMnemonic('O');
regMenuBar.add(optionsMenu);
//add menu "help"
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
regMenuBar.add(helpMenu);
//add menu items with mnemonics to menu "options"
optionsMenu.add(jmiNew = new JMenuItem("New", 'N'));
optionsMenu.add(jmiExisting = new JMenuItem("Existing", 'E'));
optionsMenu.addSeparator();
optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));
Container c = getContentPane();
c.setLayout(new BorderLayout());
WelcomeP = new JPanel();
WelcomeP.setLayout(new GridLayout(2, 1));
Welcome = new JLabel("Welcome");
Date = new JLabel("Date: 01/10/2013");
Version = new JLabel("Version 0.1");
Exit = new JButton("Exit");
Existing = new JButton("Existing User");
New = new JButton("New User");
WelcomeP.add(Welcome);
WelcomeP.add(Date);
WelcomeP.add(Version);
WelcomeP.add(Existing);
WelcomeP.add(Exit);
WelcomeP.add(New);
Exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
New.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
registerInterface regFace = new registerInterface();
regFace.setVisible(true);
Welcome.this.dispose();
Welcome.this.setVisible(false);
}
});
Existing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log login = new log();
login.setVisible(true);
login.setSize(500, 300);
Welcome.this.dispose();
Welcome.this.setVisible(false);
}
});
jmiExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jmiNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
registerInterface regFace = new registerInterface();
regFace.setVisible(true);
Welcome.this.dispose();
Welcome.this.setVisible(false);
}
});
jmiExisting.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log shoeFace = new log();
shoeFace.setVisible(true);
shoeFace.setSize(500, 300);
Welcome.this.dispose();
Welcome.this.setVisible(false);
}
});
//listner for about menuitem
jmiAbout.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,
"Program Dedicated to researchers of eAgriculture"
+ "\n Assignment for University",
"About", JOptionPane.INFORMATION_MESSAGE);
}
});
c.add(WelcomeP, BorderLayout.CENTER);
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Welcome app = new Welcome();
}
}
当我单击现有用户时,它会将我带到登录面板:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Login {
// public static void main(String arg[])
// {
// log frame=new log();
// frame.setSize(500,500);
// frame.setLocationRelativeTo(null);
// frame.setVisible(true);
// }
}
class log extends JFrame {
private JTextField jtfUsername, jtfPassword;
private JButton backButton, loginButton;
private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;
log() {
//create menu bar
JMenuBar jmb = new JMenuBar();
//set menu bar to the applet
setJMenuBar(jmb);
//add menu "operation" to menu bar
JMenu optionsMenu = new JMenu("Options");
optionsMenu.setMnemonic('O');
jmb.add(optionsMenu);
//add menu "help"
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
jmb.add(helpMenu);
//add menu items with mnemonics to menu "options"
optionsMenu.add(jmiLogin = new JMenuItem("Login", 'L'));
optionsMenu.addSeparator();
optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));
//panel p1 to holds text fields
JPanel p1 = new JPanel(new GridLayout(2, 2));
p1.add(new JLabel("Username"));
p1.add(jtfUsername = new JTextField(15));
p1.add(new JLabel("Password"));
p1.add(jtfPassword = new JPasswordField(15));
//panel p2 to holds buttons
JPanel p2 = new JPanel(new FlowLayout());
p2.add(backButton = new JButton("Back"));
p2.add(loginButton = new JButton("Login"));
//Panel with image??????
//add panels to frame
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(p1, BorderLayout.CENTER);
panel.add(p2, BorderLayout.SOUTH);
add(panel, BorderLayout.CENTER);
setTitle("Main Page");
//listners for exit menuitem and button
jmiBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome welcome = new Welcome();
welcome.setVisible(true);
welcome.setSize(500, 500);
welcome.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
log.this.dispose();
log.this.setVisible(false);
}
});
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome welcome = new Welcome();
welcome.setVisible(true);
welcome.setSize(500, 500);
welcome.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
log.this.dispose();
log.this.setVisible(false);
}
});
//listner for about menuitem
jmiAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This is the login panel"
+ "\n Assignment for University",
"About", JOptionPane.INFORMATION_MESSAGE);
}
});
//action listeners for Login in button and menu item
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu mainmenu = new MainMenu();
mainmenu.setVisible(true);
mainmenu.setSize(500, 500);
mainmenu.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
log.this.dispose();
log.this.setVisible(false);
}
});
jmiLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu mainmenu = new MainMenu();
mainmenu.setVisible(true);
mainmenu.setSize(500, 500);
mainmenu.setLocationRelativeTo(null);
registerInterface regFace = new registerInterface();
regFace.setVisible(false);
log.this.dispose();
log.this.setVisible(false);
}
});
}
}
我已经在netbeans中设置了一个JDBC,并添加了带有用户名和密码的表,并用模拟的用户名和密码填充了这些表,我还想在他们单击新用户时制作它,他们可以向数据库添加新信息,以便他们可以长期使用新帐户。
下面是我的 JDBC
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBC {
private Connection con = null;
private String query = null;
private ResultSet rs;
private String url = "jdbc:derby://localhost:1527/Assignment";
private String usrName = "root";
private String pwd = "root";
public JDBC() throws Exception {
try {
con = DriverManager.getConnection(url, usrName, pwd);
query = "SELECT USERNAME, PASSWORD from root.PERSON";
PreparedStatement stm = con.prepareStatement(query);
rs = stm.executeQuery();
while (rs.next()) {
String USER = rs.getString("USERNAME");
String PW = rs.getString("PASSWORD");
System.out.println(" USERNAME: " +
USER + " PASSWORD: " + PW);
}
close();
} catch (Exception e) {
}
}
private void close() {
try {
if (rs != null) {
rs.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
}
}
public static void main(String[] args) throws Exception {
JDBC dao = new JDBC();
}
}