【问题标题】:Cannot close frame using button after opening a new one in swing在摇摆中打开新框架后无法使用按钮关闭框架
【发布时间】:2011-10-31 22:25:34
【问题描述】:
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() 方法工作,虽然它在我扩展我的代码之前工作,但我已经尝试了几个小时让它工作但无济于事!登录按钮成功打开新框架,但不会关闭前一个(登录)框架。在我添加验证方法等之前它确实如此......

【问题讨论】:

  • Working with 2 or more frames 上查看我的建议,基本上可以归结为“不要 - 使用替代品”。
  • @Pam 我理解你想显示一个登录对话框是否正确,然后,如果可行,显示一个适当的屏幕,然后在调用 exitApp() 方法时退出?如果是这样,您必须稍微重新设计您的解决方案。有趣的是它可以在我的机器上运行吗?我可以登录,当我点击退出按钮时,窗口会关闭。

标签: java swing user-interface standards swing-app-framework


【解决方案1】:

只创建一个JFrame 作为父级,另一个Top-level Containers 只创建一次JDialog(将JPanel 作为基础),然后将其重新用于另一个Action,然后您只需从中删除所有JComponents基 JPanel 并在那里添加另一个 JPanel

不要忘记在JPanelsBase JPanel 之间切换后的最后一行

revalidate();
repaint(); 

你可以通过工具CardLayout忘记这一点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多