0

这对我来说毫无意义。我有一个从我的主类调用的完整登录屏幕,问题是用于保存文本区域等的 JPanel 在我单击窗口边缘之前不会显示(展开它)

这是我的代码:

主要的

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

public class SchoolSystem
{

public static void main (String [] args)
{

     Login lg = new Login();

}
}

登录

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

 public class Login extends JFrame implements ActionListener
 {


 JFrame frame;

 JButton SUBMIT;
 JPanel panel;
 JLabel label1,label2;
 public Menus m = new Menus();
 final JTextField  text1,text2;

  {
      frame = new JFrame("Welcome");
      frame.setSize(310,110);
      frame.setVisible(true);
      label1 = new JLabel();
      label1.setText("User ID:");
      text1 = new JTextField(15);

      label2 = new JLabel();
      label2.setText("Password:");
      text2 = new JPasswordField(15);

      SUBMIT=new JButton("LOGIN");

      panel=new JPanel(new GridLayout(3,1));
      panel.add(label1);
      panel.add(text1);
      panel.add(label2);
      panel.add(text2);
      panel.add(SUBMIT);
      add(panel,BorderLayout.CENTER);
      SUBMIT.addActionListener(this);
      text2.addActionListener(this);
      setTitle("Welcome");
      frame.add(panel);
  }


 public void actionPerformed(ActionEvent ae)
      {
          //Gets the text inside the User ID and Password Panels
          String value1=text1.getText();
          String value2=text2.getText();

          //Compare text to actual ID and password, Act accordingly
          //In this case show a Menu for the Head Teacher
          if ((value1.equals("Admin") && value2.equals("1234"))){
          m.adminMenu();
          frame.setVisible(false);
          }
          else{
          if (value1.equals("Teach") && value2.equals("0000")) {
          m.teacherMenu();
          frame.setVisible(false);
          }

          else{
          JOptionPane.showMessageDialog(this,"Incorrect login or password",
          "Error",JOptionPane.ERROR_MESSAGE);
          }
          }
      }

 }
4

2 回答 2

3
  • frame.setVisible(true);必须是最后一行代码,毕竟JComponents都加到JFrame

  • 最后的代码行可能是frame.pack();frame.setVisible(true);

  • 用于JDialog这项工作,而不是JFrame,

  • 最好的方法是使用CardLayoutJFrame.pack()从 切换logon panel到后调用another panel(s)

于 2012-12-18T15:43:25.470 回答
0

正如其他人所说,frame.setVisible(true)应该是最后一行代码。但是,如果您想保留代码原样,那么您应该frame.revalidate()在 GUI 代码的末尾调用,就在初始化程序块的右大括号之前。

于 2015-08-18T22:48:15.200 回答