2010-11-19 74 views
1
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.JFrame; 

import class_program.NextPage; 

class Login extends JFrame implements ActionListener 
{ 
JButton SUBMIT; 
JPanel panel; 
JLabel label1,label2; 
final JTextField text1,text2; 
    Login() 
    { 
    label1 = new JLabel(); 
    label1.setText("Username:"); 
    text1 = new JTextField(15); 

    label2 = new JLabel(); 
    label2.setText("Password:"); 
     text2 = new JPasswordField(15); 

    SUBMIT=new JButton("SUBMIT"); 

    panel=new JPanel(new GridLayout(3,1)); 
    panel.add(label1); 
    panel.add(text1); 
    panel.add(label2); 
    panel.add(text2); 
    panel.add(SUBMIT); 
    add(panel,BorderLayout.CENTER); 
    SUBMIT.addActionListener(this); 
    setTitle("LOGIN FORM"); 
    } 
    public void actionPerformed(ActionEvent ae) 
    { 
    String value1=text1.getText(); 
    String value2=text2.getText(); 
     if (value1.equals("jomy") && value2.equals("jomy")) { 
    NextPage page=new NextPage(); 
    page.setVisible(true); 
    JLabel label = new JLabel("Welcome:"+value1); 
    page.getContentPane().add(label); 
    } 
    else{ 
     System.out.println("enter the valid username and password"); 
     JOptionPane.showMessageDialog(this,"Incorrect login or password", 
      "Error",JOptionPane.ERROR_MESSAGE); 
    } 
} 
} 
class jframes 
{ 
    public static void main(String arg[]) 
    { 
    try 
    { 
    Login frame=new Login(); 
    frame.setSize(300,100); 
    frame.setVisible(true); 
    } 
    catch(Exception e) 
    {JOptionPane.showMessageDialog(null, e.getMessage());} 
    } 
} 

這是用來檢查下一頁用戶名和密碼是正確的去如何關閉jframe中的當前窗口?

package class_program; 

import javax.swing.JFrame; 

public class NextPage extends JFrame 
{ 
    public NextPage() 
    { 
     setDefaultCloseOperation(javax.swing. 
      WindowConstants.DISPOSE_ON_CLOSE); 
     setTitle(""); 
     setSize(400, 200); 
     } 
} 

這是。那同時舊密碼和用戶名該窗口沒有關閉下一頁程序。你能幫助關閉那個窗口嗎?

+1

你滿意你的代碼格式嗎? :) – willcodejavaforfood 2010-11-19 09:41:55

+0

做這個傢伙一樣的CS課程? http://stackoverflow.com/questions/4223927/java-big-big-problem-closed – Qwerky 2010-11-19 11:43:13

回答

0

您可以通過調用它就像你打開它setVisible(false)通過調用setVisible(true)

+0

你好它不工作? – 2010-11-19 09:53:17

+1

@Jomy Antony - 你好,請給出代碼 – willcodejavaforfood 2010-11-19 10:05:48

0

1)您登錄類犯規定義setDefaultCloseOperation(DO_SOMETHING_ON_CLOSE)關閉的JFrame。 2)如果你把setVisible(false);之前

nextPage page=new NextPage(); 
page.setVisible(true); 

連同在第一幀上定義默認關閉操作一起工作。

5

只是使用

dispose(); inside actionPerformed

public void actionPerformed(ActionEvent ae) 
    { 
    String value1=text1.getText(); 
    String value2=text2.getText(); 
    if (value1.equals("jomy") && value2.equals("jomy")) { 

    dispose(); // this will close current login box window 

    //this will open a nextpage window. 
    NextPage page=new NextPage(); 
    page.setVisible(true); 
    JLabel label = new JLabel("Welcome:"+value1); 
    page.getContentPane().add(label); 
    } 
    else{ 
     System.out.println("enter the valid username and password"); 
     JOptionPane.showMessageDialog(this,"Incorrect login or password", 
      "Error",JOptionPane.ERROR_MESSAGE); 
    }