2011-10-31 70 views
3
package bt; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 


public class login extends javax.swing.JFrame implements ActionListener, javax.swing.RootPaneContainer { 

private static final long serialVersionUID = 1L; 
private JTextField TUserID=new JTextField(20); 
private JPasswordField TPassword=new JPasswordField(20); 
protected int role; 
public JButton bLogin = new JButton("continue"); 
private JButton bCancel = new JButton("cancel"); 


public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new login().createAndShowGUI(); 
     } 
    }); 
} 



public void createAndShowGUI() { 
    ImageIcon icon = new ImageIcon(""); 
    JLabel l = new JLabel(); 
    JLabel l2 = new JLabel("(2011)"); 
    l2.setFont(new Font("Courier New", Font.BOLD, 10)); 
    l.setIcon(icon); 
    JLabel LUserID=new JLabel("Your User ID: "); 
    JLabel LPassword=new JLabel("Your Password: "); 
    TUserID.addActionListener(this); 
    TPassword.addActionListener(this); 
    TUserID.setText("correct"); 
    TPassword.setEchoChar('*'); 
    TPassword.setText("correct"); 
    bLogin.setOpaque(true); 
    bLogin.addActionListener(this); 
    bCancel.setOpaque(true); 
    bCancel.addActionListener(this); 

    JFrame f = new JFrame("continue"); 

    f.setUndecorated(true); 
    f.setSize(460,300); 

    AWTUtilitiesWrapper.setWindowOpaque(f, false); 
     AWTUtilitiesWrapper.setWindowOpacity(f, ((float) 80)/100.0f); 
    Container pane = f.getContentPane(); 
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 
    pane.setBackground(Color.BLACK); 
    Box box0 = Box.createHorizontalBox(); 
     box0.add(Box.createHorizontalGlue()); 
     box0.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
    box0.add(l); 
    box0.add(Box.createRigidArea(new Dimension(100, 0))); 
    pane.add(box0); 

    Box box = Box.createHorizontalBox(); 
     box.add(Box.createHorizontalGlue()); 
     box.setBorder(BorderFactory.createEmptyBorder(10, 20, 20, 100)); 
    box.add(Box.createRigidArea(new Dimension(100, 0))); 
    box.add(LUserID); 
    box.add(Box.createRigidArea(new Dimension(32, 0))); 
    box.add(TUserID); 
    LUserID.setMaximumSize(LUserID.getPreferredSize()); 
    TUserID.setMaximumSize(TUserID.getPreferredSize()); 
    pane.add(box); 

    Box box2 = Box.createHorizontalBox(); 
     box2.add(Box.createHorizontalGlue()); 
    box2.setBorder(BorderFactory.createEmptyBorder(10, 20, 20, 100)); 
    box2.add(Box.createRigidArea(new Dimension(100, 0))); 
    box2.add(LPassword,LEFT_ALIGNMENT); 
    box2.add(Box.createRigidArea(new Dimension(15, 0))); 
    box2.add(TPassword,LEFT_ALIGNMENT); 
    LPassword.setMaximumSize(LPassword.getPreferredSize()); 
    TPassword.setMaximumSize(TPassword.getPreferredSize()); 
    pane.add(box2); 

    Box box3 = Box.createHorizontalBox(); 
     box3.add(Box.createHorizontalGlue()); 
     box3.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 100)); 
    box3.add(bLogin); 
    box3.add(Box.createRigidArea(new Dimension(10, 0))); 
    box3.add(bCancel); 
    pane.add(box3); 

    f.setLocation(450,300); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setBackground(Color.BLACK); 
    f.setVisible(true); 
} 


@Override 
public void actionPerformed(ActionEvent evt) { 

    String TBUsername = TUserID.getText(); 
    Object src = evt.getSource(); 
    char[] CHPassword1 = TPassword.getPassword(); 
    String TBPassword = String.valueOf(CHPassword1); 

    login mLogin = this; 

    if (src==bLogin) { 

     if (authenticate(TBUsername,TBPassword)) { 
      System.out.println(this); 
      exitApp(this); 
     } else { 
      exitApp(this); 
     } 

    } else if (src==bCancel) { 
     exitApp(mLogin); 
    } 
} 

public void exitApp(JFrame mlogin) { 
    mlogin.setVisible(false); 
} 

private boolean authenticate(String uname, String pword) { 
    if ((uname.matches("correct")) && (pword.matches("correct"))) { 
     new MyJFrame().createAndShowGUI(); 
     return true; 
    } 
    return false; 
} 

}不能用開在擺動

和MyJFrame.java

package bt; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 


public class MyJFrame extends javax.swing.JFrame implements ActionListener { 

private static final long serialVersionUID = 2871032446905829035L; 
private JButton bExit = new JButton("Exit (For test purposes)"); 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new MyJFrame().createAndShowGUI(); 
     } 
    }); 
} 


public void createAndShowGUI() { 
    JPanel p = new JPanel(); 
    p.setBackground(Color.black); 
    ImageIcon icon = new ImageIcon(""); 
    JLabel l = new JLabel("(2011)");  //up to here 
    l.setIcon(icon); 
    p.add(l); 
    p.add(bExit); 
    bExit.setOpaque(true); 
    bExit.addActionListener(this); 

    JFrame f = new JFrame("frame"); 

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    f.setUndecorated(true); 
    p.setOpaque(true); 
    f.getContentPane().add(p); 
    f.pack(); 
    f.setSize(1000,600); 
    Container pane=f.getContentPane(); 
    pane.setLayout(new GridLayout(0,1)); 
    //p.setPreferredSize(200,200); 

    AWTUtilitiesWrapper.setWindowOpaque(f, false); 
     AWTUtilitiesWrapper.setWindowOpacity(f, ((float) 90)/100.0f); 

    f.setLocation(300,300); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
} 


@Override 
public void actionPerformed(ActionEvent evt) { 
    Object src = evt.getSource(); 
    if (src==bExit) { 
     System.exit(0); 
    } 
} 

}

一個新的,我不能讓ExitApp命令()方法工作崗位按鈕關閉框架,雖然它在我擴展我的代碼之前就已經工作了,但我一直在嘗試幾個小時才能使其工作,但無濟於事!登錄按鈕會在打開新框架時結束,但不會關閉優先(登錄)框架。它先前做,直到我加入了驗證方法等....

+0

看到我的建議[工作2幀或更多](http://stackoverflow.com/questions/7889922/working-with-2-or-more-frames/7890456#7890456),基本上歸結到「不要 - 使用替代品」。 –

+0

@Pam我正確理解你想要顯示一個登錄對話框,然後,如果這樣工作,顯示一個適當的屏幕,然後退出而exitApp()方法被調用?如果是這樣,你必須稍微重新設計你的解決方案。有趣的是,它可以在我的機器上運行?我可以登錄,當我點擊退出按鈕時,窗戶關閉。 – Ewald

回答

1

只有一個JFrame父創建和另一個Top-level Containers創建只有一次JDialog(放在那裏JPanel爲基地),並重新使用,對於其他動作,然後你只能從基本的JPanel刪除所有JComponent上,並添加有其他的JPanel

開關中間人後JPanelsBase JPanel

revalidate(); 
repaint(); 

你幾乎可以給忘了,通過工具沒有爲去年行忘