2011-12-14 5 views
0

私はそれがほしいと思わないときにポップアップする迷惑なメッセージボックスがあります。 ユーザーがログオンして非表示のボタンが表示された後に問題が発生しますが、クリックすると「正しい」メッセージが再び表示されますか? Iveは最初のステートメントとボトムの上に置いてみました。 EDIT:あなたはすべてのあなたのactionPerformed方法でこれを入れて迷惑なメッセージは、ログインが成功した後に来るのメッセージではない、と私はいけない何をスペルチェックする必要があり、else文のスウィング

class MyWindowListener extends WindowAdapter { 

    public void windowClosing(WindowEvent e) { 
     System.out.println("Closing window!"); 
     System.exit(0); 

    } 
} 
class LoginForm extends JFrame implements ActionListener { 

    private final String username = "user"; 
    private final String password = "pass"; 
    JFrame frame; 
    JPanel jPanel; 
    JLabel userLabel; 
    final JTextField userText; 
    JLabel passLabel; 
    final JPasswordField passText; 
    JButton loginBtn; 
    JButton shopBtn; 
    JLabel welcome; 

    { 

     frame = new JFrame(); 
     jPanel = new JPanel(); 
     userLabel = new JLabel("Login : "); 
     userText = new JTextField(10); 
     passLabel = new JLabel("Password : "); 
     passText = new JPasswordField(10); 
     loginBtn = new JButton("Login"); 
     shopBtn = new JButton("Go to Shop"); 
     welcome = new JLabel("Welcome to ECSE501 Computers"); 
     setTitle("Login Page"); 
     loginBtn.addActionListener(this); 
     shopBtn.addActionListener(this); 


     Container c1 = new Container(); 
     c1.setLayout(new GridLayout (3,2)); 
     c1.add(userLabel); 
     c1.add(userText); 
     c1.add(passLabel); 
     c1.add(passText); 
     c1.add(loginBtn); 

     Container c2 = new Container(); 
     c2.setLayout(new BoxLayout(c2, BoxLayout.Y_AXIS)); 
     c2.add(welcome); 
     c2.add(shopBtn); 
     welcome.setVisible(false); 
     shopBtn.setVisible(false); 

     add(jPanel); 
     jPanel.add(c1); 
     jPanel.add(c2); 

    } 

    public void actionPerformed(ActionEvent e) { 

     String userInput = userText.getText(); 
     char[] pass = passText.getPassword(); 
     String p = new String(pass); 



     if (userInput.equals(username) && p.equals(password)) { 
      jPanel.setVisible(true); 
      welcome.setVisible(true); 
      jPanel.setBackground(Color.green); 
      JOptionPane.showMessageDialog(null, "Correct"); 
      shopBtn.setVisible(true); 
      JButton hiddenBtn = (JButton) e.getSource(); 
     if (hiddenBtn == shopBtn) 
     { 
      SelectionForm selection = new SelectionForm(); 
      selection.select(); 
     } 
     } 
     else { 
      jPanel.setBackground(Color.red); 
      JOptionPane.showMessageDialog(null, "Wrong Login Details"); 

     } 


    } 
} 
public class LoginTester { 

    public static void main(String[] args) { 

     // register an event handler for frame events 
     LoginForm frame = new LoginForm(); 
     frame.addWindowListener(new MyWindowListener()); 
     frame.setSize(300, 200); 
     frame.setVisible(true); 

     //frame.pack(); 
     } 
    } 
+0

このfuctionをループで呼んでいるようです。 (主要部として) – StackFlowed

+5

1)より良いヘルプをもっと早く得るために、[SSCCE](http://sscce.org/)を投稿してください。 2)* '' Corerect ''*間違っています。スペルチェッカーを使用します。 3)*「迷惑なメッセージ」*の正確なテキストは何ですか? –

+1

'' Corerect ''?それが私の上に現れたら、私は迷惑になるだろう。申し訳ありませんが、皮肉に抵抗できませんでした。 –

答えて

5

。これは、何らかのアクションが実行されるたびに呼び出され、ボタンがクリックされたり、テキストフィールドが編集されたりします。

ボタンをクリックしたときにこれを実行したくない場合は、 getSource()を使用し、ソースがボタンの場合はコードを実行しないでください。あなたの方法は次のようになります:

public void actionPerformed(ActionEvent e) { 
     String userInput = userText.getText(); 
     char[] pass = passText.getPassword(); 
     String p = new String(pass); 

     if(e.getSource().equals(loginBtn)) { 

      if (userInput.equals(username) && p.equals(password)) { 

       jPanel.setVisible(true); 
       welcome.setVisible(true); 
       jPanel.setBackground(Color.green); 
       JOptionPane.showMessageDialog(null, "Correct"); 
       shopBtn.setVisible(true); 
       JButton hiddenBtn = (JButton) e.getSource(); 
      } 

      else { 
       jPanel.setBackground(Color.red); 
       JOptionPane.showMessageDialog(null, "Wrong Login Details"); 
      } 
     } 

     else if (e.getSource().equals(shopBtn)) { 

      SelectionForm selection = new SelectionForm(); 
      selection.select(); 

     } 
} 
+0

「ショップに行く」/ショップボタンをクリックするたびにメッセージが表示されます。 –

+0

これは、!equals()の代わりに "!="を使用したためかもしれません。私はそれを変更します。この変更後もまだ動作しない場合は、JOptionPane.showMessageDialog(null、 "Corerect")を置き換えます。 JOptionPane.showMessageDialog(null、e.getSource());何がそれを呼び出すかを見るために。 – eboix

+0

私は非常に疲れていたので、私はそれを正しく説明しませんでした。ユーザーがログインに成功した後で、メッセージを開くが、一度onluしたい。それから隠されたボタンが表示され、それがクリックされると新しいフレームが開き、もう一度「正しい」メッセージが表示されます。 –