如何使我的按钮在BoxLayout中做某事?

时间:2017-04-27 20:21:15

标签: java swing

我制作了一个BoxLayout GUI,我想知道我是如何使用actionlistener使按钮关闭窗口的。如果我尝试输入RegisterNew.setVisible(false);在actionlistener中,它给了我一个错误

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegisterNew extends JFrame
{
public RegisterNew(int axis){
    // creates the JFrame
    super("BoxLayout Demo");

    Container con = getContentPane();

    con.setLayout(new BoxLayout(con, axis));

    con.add(new JLabel("Enter your desired username"));
    con.add(new JTextField());
    con.add(new JLabel("Enter your password"));
    con.add(new JTextField());
    con.add(new JButton("Create Account"));

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    pack();

    setVisible(true);
}

public static void main(String args[])
{
    RegisterNew newDemo = new RegisterNew(BoxLayout.Y_AXIS);
}

}

我也试图将它链接到另一个GUI,这样当你按下一个按钮时,会出现这个,但它给了我同样的错误,就好像我把它放在一起 RegisterNew.setVisible(真);进入动作监听器

1 个答案:

答案 0 :(得分:2)

  1. 如果那是一个从属对话窗口,那么使用JDialog,而不是JFrame。
  2. 如果您的ActionListener是内部类,则使用RegisterNew.this.close();
  3. 否则,您可以使用SwingUtilities.getWindowAncestor(button)获取JButton的窗口祖先,并在返回的窗口上调用close()或更好dispose()
  4. 请注意,BoxLayout和布局管理器通常与您当前的问题无关。
  5. 如,

    显示新注册对话框并从中提取信息的测试类。

    import java.awt.BorderLayout;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    import javax.swing.*;
    
    public class TestRegistration extends JPanel {
        private JTextArea textArea = new JTextArea(30, 60);
    
        public TestRegistration() {
            JPanel bottomPanel = new JPanel();
            bottomPanel.add(new JButton(new ShowRegisterNewAction()));
            textArea.setFocusable(false);
            textArea.setEditable(false);
    
            setLayout(new BorderLayout());
            add(new JScrollPane(textArea, 
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
            add(bottomPanel, BorderLayout.PAGE_END);
        }
    
        private class ShowRegisterNewAction extends AbstractAction {
            private RegisterNew registerNew = null;
    
            public ShowRegisterNewAction() {
                super("Show Register New Dialog");
                putValue(MNEMONIC_KEY, KeyEvent.VK_S);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                if (registerNew == null) {
                    JButton btn = (JButton) e.getSource();
                    Window window = SwingUtilities.getWindowAncestor(btn);
                    registerNew = new RegisterNew(window, BoxLayout.PAGE_AXIS);
                }
                registerNew.setVisible(true);
                String userName = registerNew.getUserName();
                String password = new String(registerNew.getPassword());
    
                textArea.append("User Name: " + userName + "\n");
                textArea.append("Password: " + password + "\n");
                textArea.append("\n");
            }
        }
    
        private static void createAndShowGui() {
            JFrame frame = new JFrame("TestRegistration");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new TestRegistration());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    }
    

    新的Register类,它包含对话框并具有显示它和从中提取信息的代码。使用BoxLayout。

    import java.awt.Container;
    import java.awt.Window;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    
    public class RegisterNew {
        private JDialog dialog = null;
        private JTextField nameField = new JTextField(10);
        private JPasswordField passField = new JPasswordField(10);
    
        public RegisterNew(Window window, int axis) {
            dialog = new JDialog(window, "Register New", ModalityType.APPLICATION_MODAL);
            Container con = dialog.getContentPane();
    
            con.setLayout(new BoxLayout(con, axis));
    
            con.add(new JLabel("Enter your desired username"));
            con.add(nameField);
            con.add(new JLabel("Enter your password"));
            con.add(passField);
            con.add(new JButton(new AcceptAction()));
            dialog.pack();
            dialog.setLocationRelativeTo(window);
    
        }
        public char[] getPassword() {
            return passField.getPassword();
        }
        public String getUserName() {
            return nameField.getText();
        }
        public void setVisible(boolean b) {
            dialog.setVisible(b);
        }
    
        private class AcceptAction extends AbstractAction {
            public AcceptAction() {
                super("Accept");
                putValue(MNEMONIC_KEY, KeyEvent.VK_A);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        }
    
    }