簡體   English   中英

單擊JButton時退出JFrame

[英]Exiting JFrame when JButton is clicked

我正在編寫一個登錄GUI,並希望單擊取消按鈕以關閉整個程序。 我是計算機科學專業的大一新生,對Java和程序設計總體還是半新的。 這是我的代碼:

主類:

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

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)
        {

        }
    }
}

我在取消按鈕ActionListener中始終收到“找不到符號”錯誤:

frame.dispose();

frame僅具有static方法gui上下文。 首先擺脫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()
    {
        //...

您也不需要內部類上的static聲明...

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

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

    }
}

您可能會發現,從類構造函數而不是gui方法中初始化UI更為容易,而將其顯示在窗口中

您還應該避免使用null布局,像素完美布局是現代ui設計中的一種幻覺。 有太多因素會影響組件的單個大小,您無法控制。 Swing旨在與布局經理為核心一起工作,舍棄這些問題不會導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正

相反,請看一下在容器布置組件的更多想法

gui方法中的代碼必須在構造函數中,並且您的JFrame對象必須在任何方法之外定義為類的字段:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM