Annonceindlæg fra FPT Software
02. marts 2005 - 18:22
#6
Here you go.. package mypackage; public class Bruger { private String brugernavn; private String password; public Bruger(String user, String psw) { brugernavn = user; password = psw; } public boolean tjeck(String psw, String user) { if(psw.equalsIgnoreCase(password) && user.equalsIgnoreCase(brugernavn)) { return true; } else return false; } } package mypackage; import java.util.ArrayList; public class Brugere { private ArrayList brugere; public Brugere() { brugere = new ArrayList(); brugere.add(new Bruger("kalp", "abcd21")); brugere.add(new Bruger("boris", "asdasdsa")); brugere.add(new Bruger("Hest", "a234wef")); } public boolean tjeck(String password, String brugernavn) { boolean test = false; for (int i = 0; i < brugere.size(); i++) { test = ((Bruger)brugere.get(i)).tjeck(password, brugernavn); if(test) { return test; } } return test; } public boolean tjeck(Bruger bruger) { for (int i = 0; i < brugere.size(); i++) { if(bruger.equals(((Bruger)brugere.get(i)))) return true; } return false; } } package mypackage; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.JPanel; import java.awt.Rectangle; import java.awt.Color; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GUI extends JFrame { private JPanel jPanel1 = new JPanel(); private JLabel jLabel1 = new JLabel(); private JPanel jPanel2 = new JPanel(); private JLabel jLabel2 = new JLabel(); private JLabel jLabel3 = new JLabel(); private JLabel jLabel4 = new JLabel(); private JTextField jTextField1 = new JTextField(); private JTextField jTextField2 = new JTextField(); private Brugere brugere = new Brugere(); private JButton jButton1 = new JButton(); private JLabel jLabel5 = new JLabel(); public GUI() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { jPanel1.setVisible(false); this.getContentPane().setLayout(null); this.setSize(new Dimension(400, 300)); this.setTitle("Login"); jPanel1.setBounds(new Rectangle(0, 0, 395, 275)); jPanel1.setForeground(new Color(107, 146, 214)); jPanel1.setBackground(new Color(124, 178, 209)); jPanel1.setLayout(null); jLabel1.setText("DU ER LOGGET IND I DEN HEMMELIGE ZONE"); jLabel1.setFont(new Font("Tahoma", 1, 16)); jLabel1.setBounds(new Rectangle(15, 110, 365, 20)); jPanel2.setBounds(new Rectangle(0, 0, 395, 275)); jPanel2.setToolTipText("null"); jPanel2.setLayout(null); jLabel2.setText("Login"); jLabel2.setBounds(new Rectangle(80, 95, 145, 25)); jLabel2.setFont(new Font("Tahoma", 1, 19)); jLabel3.setText("Brugernavn"); jLabel3.setBounds(new Rectangle(80, 135, 150, 15)); jLabel3.setFont(new Font("Tahoma", 1, 15)); jLabel4.setText("Password"); jLabel4.setBounds(new Rectangle(95, 160, 90, 15)); jLabel4.setFont(new Font("Tahoma", 1, 15)); jLabel4.setToolTipText("null"); jTextField1.setBounds(new Rectangle(175, 130, 140, 20)); jTextField2.setBounds(new Rectangle(175, 155, 140, 20)); jButton1.setText("Login"); jButton1.setBounds(new Rectangle(245, 190, 70, 30)); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jLabel5.setBounds(new Rectangle(250, 240, 125, 25)); jPanel1.add(jLabel1, null); jPanel2.add(jLabel5, null); jPanel2.add(jButton1, null); jPanel2.add(jTextField2, null); jPanel2.add(jTextField1, null); jPanel2.add(jLabel4, null); jPanel2.add(jLabel3, null); jPanel2.add(jLabel2, null); this.getContentPane().add(jPanel2, null); this.getContentPane().add(jPanel1, null); } private void jButton1_actionPerformed(ActionEvent e) { boolean test = brugere.tjeck(jTextField2.getText(), jTextField1.getText()); if(test) { jPanel2.setVisible(false); jPanel1.setVisible(true); } else { jLabel5.setText("Du findes da vist ikke!!"); } } } package mypackage; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.UIManager; import java.awt.Dimension; import java.awt.Toolkit; public class Start { public Start() { Frame frame = new GUI(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setVisible(true); } /** * * @param args */ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new Start(); } }