JFrame setVisible无法正常工作

时间:2015-02-26 12:33:08

标签: java swing user-interface jframe awt

按下登录按钮后,我正在构建登录表单,它会关闭登录框并显示主菜单。

问题是无论我做什么,它都给我一个错误,找不到符号setVisible

请帮帮我

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
 *
 * @author 
 */
public class Login extends Application {
    private JFrame frame;// frame of the interface
    private JLabel name = new JLabel ("Username:");
    private JLabel pass = new JLabel ("Password:");
    private JTextField username = new JTextField();
    private JPasswordField password = new JPasswordField(); // password text box
    private JTextField empty = new JTextField();
    private JButton login = new JButton("Login");
    private JPanel panel = new JPanel();

    public Login()
    {
        makeFrame();
    }
    private void makeFrame()
    {
       frame = new JFrame("Login");  
       frame.setSize(300,200);
       makeMenuBar(frame);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container contentPane = frame.getContentPane();
       //makes application start in the center of the screen
       Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
       //border
       UIManager.getDefaults().put("TitledBorder.titleColor", Color.BLACK);
       Border lowerEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
       TitledBorder title = BorderFactory.createTitledBorder(lowerEtched, "Login");
       Font titleFont = UIManager.getFont("TitledBorder.font");
       title.setTitleFont( titleFont.deriveFont(Font.ITALIC + Font.BOLD) );
       panel.setBorder( title );
       //border end
       frame.setVisible(true);  
       frame.add(panel);

       panel.add(name);
       panel.add(username);
       panel.add(pass);
       panel.add(password);
       password.setEchoChar('*');
       panel.add(empty);
       empty.setVisible(false);
       panel.add(login);
       login.addActionListener(new ActionListener()
        {public void actionPerformed(ActionEvent e) { try
       {
           String user = username.getText().trim();
           String pass = new String(password.getPassword());
           String sql = "select user,pass from Login where Username = '"+user+"'and pass='"+pass+"'";

           if(rs.next())
           {
               JOptionPane.showMessageDialog(null,"Welcome");
               frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

               MainScreen s = new MainScreen();
               s.setVisible(true); //<- here is the problem
           }
           else
           {
               JOptionPane.showMessageDialog(null,"Wrong username or password");
           }
       }
       catch(Exception ex){

       } }
         });
       panel.setLayout(new GridLayout(3,2));

    }

}

1 个答案:

答案 0 :(得分:0)

在MainScreen中未定义可能的setVisible()。我认为MainScreen应该扩展JFrame。最有可能是作为MainScreen成员的jframe(标题为&#34; main&#34;)是其中的候选者。