2017-09-09 4 views
0

(他のフレームにprocedeするためのユーザー名とパスワードを取得する必要があります)私はいつもあなたがでログインコードを配置する必要があり

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 


public class Cuizon07 { 

public static void main(String[] args) { 

    JFrame frames = new JFrame(); 
    frames.setVisible(true); 
    frames.setSize(700, 500); 
    frames.setResizable(false); 
    frames.setLocation(170, 100); 
    JPanel panels = new JPanel(); 
    frames.add(panels); 
    panels.setBackground(new Color(56, 3, 96)); 
    panels.setLayout(null); 
    JTextField tf1 = new JTextField(); 
    panels.add(tf1); 
    tf1.setBounds(200, 50, 100, 25); 
    JLabel label1 = new JLabel("USERNAME:"); 
    panels.add(label1); 
    label1.setBounds(100, 50, 150, 30); 
    JLabel label2 = new JLabel("PASSWORD:"); 
    panels.add(label2); 
    JTextField tf2 = new JTextField(); 
    panels.add(tf2); 
    tf2.setBounds(200, 100, 100, 25); 
    label2.setBounds(100, 100, 150, 30); 

    JButton button = new JButton("NEXT FRAME"); 
    panels.add(button); 
    button.setBounds(500, 250, 150, 30); 

    String user=tf1.getText(); 
    String pass=tf2.getText(); 

    //getting the username and password to the user i think is correct.. 

    if (user.equals("Dominic") && (pass.equals("1234"))) 
    { 
     button.addActionListener(new copro()); 
     JOptionPane.showMessageDialog(null, "LOGIN SUCCESSFULL"); 

    } 
    else 
    { 
     tf1.setText(" "); 
     tf2.setText(" "); 

     JOptionPane.showMessageDialog(null, "INCORRECT USERNAME OR PASSWORD"); 

    /*the else statement, joptionpane always popup after i run the program, i dont know how to correct this problem.. */ 
    } 
} 

    public static class copro implements ActionListener{ 

     public void actionPerformed (ActionEvent e){ 

     JFrame frame2 = new JFrame("NEW FRAME"); 
     frame2.setVisible(true); 
     frame2.setSize(700, 500); 
     frame2.setLocation(170, 100); 
     JLabel label = new JLabel("YOU CLICKED ME"); 
     JPanel panel2 = new JPanel(); 
     frame2.add(panel2); 
     panel2.add(label); 
     panel2.setLayout(null); 
     panel2.setBackground(new Color(13, 97, 150)); 
     label.setBounds(500, 250, 150, 30); 
     } 
     /*this is the 2nd frame after you enter the correct username and password*/ 

    } 
} 
+0

見る(http://stackoverflow.com/q/9554636/418556) –

答えて

0
  1. ポップアップテキストフィールドから値、他でのJOptionPaneを得ることができませんActionListenerのは、私は

(それはのActionListener自体である必要はありません)別々の方法における第二のフレームを呼び出すコードを置く

  • ボタンにそれを追加します

    はここで働い例です:[?複数JFrames、グッド/バッドプラクティスの使用]

    JButton button = new JButton("NEXT FRAME"); 
        button.addActionListener(new ActionListener() { 
         @Override 
         public void actionPerformed(ActionEvent arg0) { 
          String user = tf1.getText(); 
          String pass = tf2.getText(); 
    
          if (user.equals("Dominic") && (pass.equals("1234"))) { 
           openFrame2(); 
           JOptionPane.showMessageDialog(null, "LOGIN SUCCESSFULL"); 
    
          } else { 
           tf1.setText(""); 
           tf2.setText(""); 
    
           JOptionPane.showMessageDialog(null, "INCORRECT USERNAME OR PASSWORD"); 
          } 
         } 
        }); 
        panels.add(button); 
        button.setBounds(500, 250, 150, 30); 
    
    } 
    
    protected static void openFrame2() { 
        JFrame frame2 = new JFrame("NEW FRAME"); 
        frame2.setVisible(true); 
        frame2.setSize(700, 500); 
        frame2.setLocation(170, 100); 
        JLabel label = new JLabel("YOU CLICKED ME"); 
        JPanel panel2 = new JPanel(); 
        frame2.add(panel2); 
        panel2.add(label); 
        panel2.setLayout(null); 
        panel2.setBackground(new Color(13, 97, 150)); 
        label.setBounds(500, 250, 150, 30); 
    } 
    
  • 関連する問題