hallo,
ich bin ein swing anfanger. In meinen GUI sollte der nutzer sein Username aus ein combobox wählen und then sein passwort eingeben.
ich habe probleme den passwort überprufen zu können.
dass haupt layout der GUI is cardlayout von ienem page nach anderem zu springen.
und ich kriege ein nullpointerExpection...verstehe nicht warm, kann jemand bitte helfen?
Edit Illuvatar: Codetags berichtigt
ich bin ein swing anfanger. In meinen GUI sollte der nutzer sein Username aus ein combobox wählen und then sein passwort eingeben.
ich habe probleme den passwort überprufen zu können.
dass haupt layout der GUI is cardlayout von ienem page nach anderem zu springen.
Code:
hier ist mein code.
import java.awt.*;
import java.util.Date;
import javax.swing.*;
import java.awt.event.*;
public class PCGUI extends JPanel implements ActionListener {
JPanel cards, pane;
private JFrame controllingFrame;
private JPasswordField pf;
final static String lp = "login page";
final static String p1 = "page one";
JButton ok1;
JComboBox cb;
CardLayout cl;
PCGUI(JFrame f) {
controllingFrame = f;
JPanel pane = new JPanel(new BorderLayout());
f.setContentPane(pane);
pane.setOpaque(true);
JPanel c1 = new JPanel();
GridBagLayout gb = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c1.setLayout(gb);
JLabel l1 = new JLabel("Login Type:", JLabel.LEFT);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 10;
c.weighty = 40;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
gb.setConstraints(l1, c);
c1.add(l1);
String[] cbitems = { "Developer", "User" };
JComboBox cb = new JComboBox(cbitems);
cb.setSelectedIndex(1);
cb.setEditable(false);
cb.addActionListener(this);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.9;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
gb.setConstraints(cb, c);
c1.add(cb);
JLabel l2 = new JLabel("Password:", JLabel.CENTER);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0;
c.weighty = 0.4;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
gb.setConstraints(l2, c);
c1.add(l2);
pf = new JPasswordField(9);
pf.addActionListener(this);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0;
c.weighty = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
gb.setConstraints(pf, c);
c1.add(pf);
JButton ok1 = new JButton("OK");
ok1.addActionListener(this);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.gridheight = 1;
c.weightx = 0;
c.weighty = 20;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
gb.setConstraints(ok1, c);
c1.add(ok1);
JPanel c2 = new JPanel(new BorderLayout());
Boolean[] status = new Boolean[9];
JPanel rb = new JPanel(new GridLayout(0, 3));
JButton b1 = new JButton("Fenster 1");
JButton b2 = new JButton("Fenster 2");
JButton b3 = new JButton("Fenster 3");
JButton b4 = new JButton("Fenster 4");
JButton b5 = new JButton("Fenster 5");
JButton b6 = new JButton("Fenster 6");
JButton b7 = new JButton("Fenster 7");
JButton b8 = new JButton("Fenster 8");
JButton b9 = new JButton("Fenster 9");
rb.add(b1);
rb.add(b2);
rb.add(b3);
rb.add(b4);
rb.add(b5);
rb.add(b6);
rb.add(b7);
rb.add(b8);
rb.add(b9);
c2.add(rb, BorderLayout.LINE_START);
JPanel text = new JPanel();
text.setLayout(new BoxLayout(text, BoxLayout.PAGE_AXIS));
JLabel ins1 = new JLabel("Clicken Sie individuale tasten, bestimmte rolladen hoch\\hunter zu fahren.\n Oder Schedule taste, ein profile anzulegen.", JLabel.CENTER);
text.add(ins1);
c2.add(text, BorderLayout.PAGE_END);
JPanel shp = new JPanel();
shp.setLayout(new BoxLayout(shp, BoxLayout.PAGE_AXIS));
JButton sched = new JButton("SCHEDULE");
shp.add(sched);
c2.add(shp, BorderLayout.LINE_END);
JPanel cards = new JPanel(new CardLayout());
cards.add(lp, c1);
cards.add(p1, c2);
pane.add(cards);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if ( src == ok1) {
checkPass();
}
if ( src == pf ) {
checkPass();
}
}
public void checkPass () {
String sel;
char[] input;
//String sel = new String();
sel = (String)cb.getSelectedItem();
//char[]input = new char[9];
input = pf.getPassword();
if (isPasswordCorrect(input, sel) == true) {
CardLayout cl = (CardLayout)cards.getLayout();
cl.show(cards, p1);
pane.repaint();
}
else {
JOptionPane.showMessageDialog(controllingFrame, "Invalid password.Try Again.", "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
private static boolean isPasswordCorrect(char[] input, String sel) {
Boolean isCorrect = true;
char[] correct1 = { 'Y', 'A', 'H', 'O', 'O', 'M', 'E' };
char[] correct2 = { 'a', 'c', 'k', 'e', 'r', 'm', 'a', 'n', 'n' };
if (input.length != correct2.length ) {
if ( input.length != correct1.length ) {
isCorrect = false;
}
} else {
if ( sel == "User" ) {
for (int i = 0; i<input.length; i++) {
if (input[i] != correct2[i]) {
isCorrect = false;
}
}
} else {
for (int i = 0; i<input.length; i++) {
if (input[i] != correct1[i]) {
isCorrect = false;
}
}
}
}
for (int i = 0; i < correct1.length; i++) {
correct1[i] = 0;
}
for (int i = 0; i < correct2.length; i++) {
correct2[i] = 0;
}
return isCorrect;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("PCGUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PCGUI demo = new PCGUI(frame);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Edit Illuvatar: Codetags berichtigt