Java无法关闭JFrame

时间:2017-06-16 07:57:56

标签: java swing user-interface

单击按钮后,我无法关闭框架。棘手的部分是我不需要退出整个应用程序,只需关闭GUI(不用系统退出)。 你能帮助我吗? 提前谢谢!!!

Main.java

public class Main {
public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://my.emerchantpay.com/");
    eMerchantPay emp = PageFactory.initElements(driver, eMerchantPay.class);
    SwingUtilities.invokeLater(new Runnable() {
       public void run() {
            JFrame frame = new MainFrame("Please enter your credentials");
            frame.setSize(500, 400);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
       }
    });

MainFrame.java

public class MainFrame extends JFrame {

private DetailsPanel detailsPanel;

public MainFrame(String title) {
    super(title);

    // Set layout manager
    setLayout(new BorderLayout());

    // Create Swing components
    final JTextArea textArea = new JTextArea();

    detailsPanel = new DetailsPanel();

    detailsPanel.addDetailsListener(new DetailListener() {


        public void detailEventOccured(DetailEvent event) {
            String text = event.getText();

            textArea.append(text);
        }
    });

    // Add Swing components to content pane
    Container c = getContentPane();

    //c.add(textArea, BorderLayout.CENTER);
    c.add(detailsPanel, BorderLayout.CENTER);
}

DetailsPanel.java

public class DetailsPanel  extends JPanel{

private static final long serialVersionUID = 1234567891;

private EventListenerList listenerList = new EventListenerList();

public DetailsPanel() {
    Dimension size = getPreferredSize();
    size.width = 250;
    setPreferredSize(size);

    setBorder(BorderFactory.createTitledBorder("Personal Details"));

    JLabel nameLabel = new JLabel("Name: ");
    JLabel passwordLabel = new JLabel("Password: ");

    final JTextField nameField = new JTextField(10);
    final JPasswordField passwordField = new JPasswordField(10);

    JButton addBtn = new JButton("Submit");

    addBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            String name = nameField.getText();
            String password = passwordField.getText();

            String text = name + ": " + password + "\n";

            System.out.println (text);
        }
    });

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    /// First column ///
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;

    gc.gridx = 0;
    gc.gridy = 0;
    add(nameLabel, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    add(passwordLabel, gc);

    /// Second column ///
    gc.anchor = GridBagConstraints.LINE_START;

    gc.gridx = 1;
    gc.gridy = 0;
    add(nameField, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    add(passwordField, gc);

    /// Final row ///
    gc.weighty = 10;

    gc.anchor = GridBagConstraints.FIRST_LINE_START;

    gc.gridx = 1;
    gc.gridy = 2;
    add(addBtn, gc);
}

public void fireDetailEvent(DetailEvent event) {
    Object[] listeners = listenerList.getListenerList();

    for (int i=0; i < listeners.length; i += 2) {
        if (listeners[i] == DetailListener.class) {
            ((DetailListener)listeners[i+1]).detailEventOccured(event);
        }
    }
}

public void addDetailsListener(DetailListener listener) {
    listenerList.add(DetailListener.class, listener);
}

public void removeDetailListener(DetailListener listener) {
    listenerList.remove(DetailListener.class, listener);

}

点击这段代码中的登录按钮后,我需要关闭框架:

addBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            String name = nameField.getText();
            String password = passwordField.getText();

            String text = name + ": " + password + "\n";

            System.out.println (text);
        }
    });

1 个答案:

答案 0 :(得分:2)

actionPerformed中,添加以下行以获取并关闭父框架:

JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(addBtn);
frame.dispose();// or frame.setVisible(false), depending on your needs

请注意,您必须声明按钮final才能在匿名侦听器中使用它:

final JButton addBtn = new JButton("Submit");