简体   繁体   English

单击JButton时退出JFrame

[英]Exiting JFrame when JButton is clicked

I am writing a Login GUI and want the cancel button to close the entire program when clicked. 我正在编写一个登录GUI,并希望单击取消按钮以关闭整个程序。 I'm a freshman computer science major and still semi-new to java and programming in general. 我是计算机科学专业的大一新生,对Java和程序设计总体还是半新的。 Here is my code: 这是我的代码:

Main Class: 主类:

public class loginGui
{
    public static void main(String[] args)
    {
        lGui gui = new lGui();
        lGui.gui();
    }
}

GUI class: GUI类:

public class lGui
{
    public static void gui()
    {
        JFrame frame;
        JTextField field;
        JLabel l;
        JPasswordField p;
        JButton login, cancel;
        JCheckBox check;

        frame = new JFrame("Login");
        frame.setSize(300, 150);
        frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        frame.setLocation(300, 200);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        l = new JLabel("Username: ");
        l.setLocation(15, 14);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        field = new JTextField("Username");
        field.setColumns(15);
        field.setSize(field.getPreferredSize());
        field.setBackground(Color.DARK_GRAY);
        field.setForeground(Color.LIGHT_GRAY);
        field.setLocation(90, 10);
        field.setToolTipText("Enter User Name");
        frame.add(field);

        l = new JLabel("Password: ");
        l.setLocation(15, 54);
        l.setSize(l.getPreferredSize());
        frame.add(l);

        p = new JPasswordField("Password");
        p.setColumns(15);
        p.setSize(p.getPreferredSize());
        p.setBackground(Color.DARK_GRAY);
        p.setForeground(Color.LIGHT_GRAY);
        p.setLocation(90, 50);
        p.setToolTipText("Enter Password");
        frame.add(p);

        login = new JButton("Login");
        login.setSize(login.getPreferredSize());
        login.setLocation(195, 78);
        login.setToolTipText("Login");
        frame.add(login);
        login.addActionListener(new loginAction());

        cancel = new JButton("Cancel");
        cancel.setSize(cancel.getPreferredSize());
        cancel.setLocation(95, 78);
        cancel.setToolTipText("Cancel");
        frame.add(cancel);
        cancel.addActionListener(new cancelAction());

        check = new JCheckBox("Remember me?");
        check.setSize(check.getPreferredSize());
        check.setLocation(120, 100);
        check.setToolTipText("Remember your username for next time");
        frame.add(check);

        frame.setVisible(true);
    }

    static class cancelAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            frame.dispose();            
        }
    }

    static class loginAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

        }
    }
}

I keep getting a "cannot find symbol" error here in the cancel button ActionListener: 我在取消按钮ActionListener中始终收到“找不到符号”错误:

frame.dispose();

frame only has context from within your static method gui . frame仅具有static方法gui上下文。 Start by getting rid of the static declaration and make frame an instance field of the class 首先摆脱static声明,然后将frame设为该类的实例字段

public class lGui
{
    private JFrame frame;
    private JTextField field;
    private JLabel l;
    private JPasswordField p;
    private JButton login, cancel;
    private JCheckBox check;

    public void gui()
    {
        //...

You also won't need the static declarations on the inner classes... 您也不需要内部类上的static声明...

protected class cancelAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        frame.dispose();            
    }
}

protected class loginAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {

    }
}

You might find it easier to make initialise the UI from within the classes constructor instead of the gui method, leaving it to show the window 您可能会发现,从类构造函数而不是gui方法中初始化UI更为容易,而将其显示在窗口中

You should also avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 您还应该避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

Instead, have a look at Laying Out Components Within a Container for some more ideas 相反,请看一下在容器布置组件的更多想法

gui方法中的代码必须在构造函数中,并且您的JFrame对象必须在任何方法之外定义为类的字段:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM