2016-02-29 47 views
0

我正在编写一个登录GUI,并且想单击取消按钮关闭整个程序。我是一名新生计算机科学专业,并且对Java和编程一般来说还是半新的。这里是我的代码:单击JButton时退出JFrame

主类:

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(); 

回答

3

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设计中的幻想。影响组件的个体大小的因素太多,其中没有一个可以控制。摇摆设计为核心与布局管理工作,丢弃这些会导致没有问题,问题是,你将花费更多的时间,试图结束整顿

相反,看看Laying Out Components Within a Container一些更多的想法

0

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