Hallo Breadfish,
ich hab wie oben schon gesagt ein Variablen Problem,
da ich ein Afänger bin in Java, frage an euch wie bekomm ich dass hin.
Bild:
ss+(2015-11-27+at+05.15.25).png
Code: Für die beiden butten Namens Registrieren und Login.
Java
//Dieser teil sool nach dem login aktuallisiert werden.
if(login.isStateloggedin() == true){
waslogged = new JLabel(login.getuser);//Dass lädt er nichts raus.
clientPanel.add(waslogged);
}else{
button_Register = new JButton("Registrieren");
button_Register.addActionListener(new regibutten());
button_Login = new JButton("Login");
button_Login.addActionListener(new logibutton());
button_SendMessage.setEnabled(false);
textField_ClientMessage.setEditable(false);
clientPanel.add(button_Register);
clientPanel.add(button_Login);
}
Alles anzeigen
Login Abteilung:
Java
package de.chat.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
//import com.sun.security.auth.login.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class login_gui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 3L;
public static final JPanel pane = new JPanel();
public static final JTextField user = new JTextField();
public static final JTextField pass = new JTextField();
public static final JButton logon = new JButton();
public static boolean stateloggedin;
public static String getuser = null;
public void LoginClass() {
this.setTitle("Login Frame");
this.setLocationRelativeTo(null);
this.setSize(300, 90);
this.setResizable(false);
this.setVisible(true);
logon.setPreferredSize(new Dimension(90, 50));
logon.setText("Login");
user.setPreferredSize(new Dimension(90, 20));
pass.setPreferredSize(new Dimension(90, 20));
user.setToolTipText("Username");
pass.setToolTipText("Password");
this.add(pane);
pane.add(user, BorderLayout.WEST);
pane.add(pass, BorderLayout.WEST);
pane.add(logon, BorderLayout.CENTER);
logon.addActionListener(new ActionListener() {
@SuppressWarnings("unused")
public void actionPerformed(ActionEvent arg0) {
try {
Connection conn;
String dbuser = "root";
String dbpassw = "";
String databaseName = "chat";
String url = "jdbc:mysql:///"+databaseName;
Class.forName("com.mysql.jdbc.Driver"); //Very important!
conn = DriverManager.getConnection(url, dbuser, dbpassw);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM accounts WHERE uid='"+user.getText()+"' AND passw='"+pass.getText()+"'");
if(res.next()) {
JOptionPane.showMessageDialog(null, "You have Logged in!");
setStateloggedin(true);
} else {
JOptionPane.showMessageDialog(null, "Wrong user/password!");
}
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
public static boolean isStateloggedin() {
return stateloggedin;
}
public void setStateloggedin(boolean stateloggedin) {
login_gui.stateloggedin = stateloggedin;
}
}
Alles anzeigen
Register Abteilung:
Java
package de.chat.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import de.chat.gui.login_gui;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class regi_gui extends JFrame{
/**
*
*/
login_gui login = new login_gui();
private static final long serialVersionUID = 2L;
public static final JPanel pane = new JPanel();
public static final JTextField user = new JTextField();
public static final JTextField pass = new JTextField();
public static final JButton reg = new JButton();
public void RegisterClass() {
setTitle("Register Frame");
setLocationRelativeTo(null);
setSize(300, 90);
setResizable(false);
setVisible(true);
reg.setPreferredSize(new Dimension(90, 50));
reg.setText("Register");
user.setPreferredSize(new Dimension(90, 20));
pass.setPreferredSize(new Dimension(90, 20));
user.setToolTipText("Username");
pass.setToolTipText("Password");
this.add(pane);
pane.add(user, BorderLayout.NORTH);
pane.add(pass, BorderLayout.NORTH);
pane.add(reg, BorderLayout.SOUTH);
reg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Connection conn;
String dbuser = "root";
String dbpassw = "";
String databaseName = "chat";
String url = "jdbc:mysql:///"+databaseName;
Class.forName("com.mysql.jdbc.Driver");//Again very important!
conn = DriverManager.getConnection(url, dbuser, dbpassw);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM accounts WHERE uid='"+user.getText()+"'");
if(res.next()) {
if(res.getString("uid").equalsIgnoreCase(user.getText())) {
JOptionPane.showMessageDialog(null, "The username is already registered!");
}
} else {
PreparedStatement resReg = conn.prepareStatement("INSERT INTO accounts (uid, passw) VALUES (?, ?)");
resReg.setString(1, user.getText());
resReg.setString(2, pass.getText());
resReg.executeUpdate();
JOptionPane.showMessageDialog(null, "Registered Username: " + user.getText());
login.setStateloggedin(true);
}
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
/**
* @return the stateloggedin
*/
/**
* @param stateloggedin the stateloggedin to set
*/
}
Alles anzeigen
ich hoffe dass es Java Programmierer gibt und mir weiter helfen können.
Es wird aber noch ein mit Programmierer gesucht.
Könnt auch optional im Teamspeak Kommen.
IP: yourelitesystems.de:9987