在摇摆中打开一个新框架后,无法使用按钮关闭框架


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()方法工作,虽然它在我扩展我的代码之前工作,我已经尝试了几个小时让它工作,但无济于事!登录按钮成功打开新框架,但不会关闭前一个(登录)框架。直到我添加验证方法等....

只创建一个JFrame作为父级,另一个顶级容器只创建一个JDialog(把那里的JPanel作为基础),并重新使用另一个动作,然后你只需要从基础JPanel中删除所有的JComponents,并添加另一个JPanel

不要忘记在Base JPanelJPanels之间切换的最后一行

revalidate();
repaint(); 

你可以通过实现CardLayout

来忘记这个

相关内容

  • 没有找到相关文章

最新更新