线程“main”中的异常java.lang.IllegalArgumentException:无法添加到布局:约束必须是字符串

时间:2017-04-27 13:43:30

标签: java swing

我在添加卡片布局时遇到了困难。我在这里分享我的代码。

package LoginUser;
import java.awt.*;
import javax.swing.UIManager;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


import DBconnection.SelectTable;
import DBconnection.TestConnection;
import GUITTG.Branch;
import GUITTG.TTGFrame;

import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class LoginForm extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    char ch;
    //Container con1;
  JPanel jpanelMainMain,jpanelMain,jpanelMainC;
  JPanel jpanelMainC1,jpanelMainC2,jpanelMainC3;
  JPanle jpanelMainC4,jpanelMainC5,jpanelMainC6;
  CardLayout cardLayout;

  JButton login,register;
  JLabel id,pass;
  JTextField idtxt;
  Font font;
  JPasswordField pw;
  RegisterForm rf;
  TestConnection tb;
  SelectTable stble;
  Statement st;
  Connection con;
  ResultSet rs;
  TTGFrame ttg;
  Container con1;
  public LoginForm()
  {

UIManager.getLookAndFeelDefaults().put("ClassLoader",       this.getClass().getClassLoader());
      this.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      setBackground(new Color(0 ,0 ,0));


       con1=getContentPane();
        cardLayout=new CardLayout();
        jpanelMainMain=new JPanel(cardLayout);

      font=new Font("title",Font.PLAIN,20);

      id=new JLabel("Login Id:");
      id.setFont(font);
      id.setForeground(Color.WHITE);
      pass=new JLabel("Password:");
      pass.setFont(font);
      pass.setForeground(Color.WHITE);

      idtxt=new JTextField();
      pw=new JPasswordField();


      login=new JButton("Login");
      register=new JButton("Register");


      jpanelMain=new JPanel(new BorderLayout());
      jpanelMain.setBorder(new TitledBorder(new LineBorder(new Color(0 ,0 ,0)), "Login Form", 0, 0, font, new Color(0 ,0 ,0)));
      jpanelMain.add(new JLabel() ,BorderLayout.SOUTH);


    /*  jpanelMainC1=new JPanel(new BorderLayout());
      jpanelMainC1.add(id);

      jpanelMainC2=new JPanel(new BorderLayout());
      jpanelMainC2.add(pass);

      jpanelMainC3=new JPanel(new BorderLayout());
      jpanelMainC3.add(idtxt);

      jpanelMainC4=new JPanel(new BorderLayout());
      jpanelMainC4.add(pw);

      jpanelMainC5=new JPanel(new BorderLayout());
      jpanelMainC5.add(login);

      jpanelMainC6=new JPanel(new BorderLayout());
      jpanelMainC6.add(register);
      */

      jpanelMainC=new JPanel(new BorderLayout());

      jpanelMainC.add(id);
      jpanelMainC.add(pass);
      jpanelMainC.add(idtxt);
      jpanelMainC.add(pw);
      jpanelMainC.add(login);
      jpanelMainC.add(register);

      /*jpanelMainC=new JPanel(new BorderLayout());
      jpanelMainC.add(jpanelMainC1);
      jpanelMainC.add(jpanelMainC2);
      jpanelMainC.add(jpanelMainC3);
      jpanelMainC.add(jpanelMainC4);
      jpanelMainC.add(jpanelMainC5);
      jpanelMainC.add(jpanelMainC6);*/
      jpanelMain.add(jpanelMainC ,BorderLayout.CENTER);
      jpanelMainC.setBackground(new Color(0,150,150));
      jpanelMainMain.setBackground(new Color(0,150,150));
     add(id);
     add(pass);
      add(idtxt);
      add(pw);
     add(login);
      add(register);


      con1.setLayout(null);
      con1.setBackground(new Color(0,150,150));

      id.setBounds(100,100,100,30);
      pass.setBounds(100,150,100,20);

      idtxt.setBounds(210,100,100,20);
      pw.setBounds(210,150,100,20);

      login.setBounds(100,200,100,20);
      register.setBounds(210,200,100,20);


      login.addActionListener(this);
      register.addActionListener(this);


    //  con1.add(jpanelMain);
      jpanelMainMain.add(jpanelMain);
      cardLayout.show(jpanelMainMain ,"LoginForm");

      tb=new TestConnection();

      idtxt.addKeyListener(new KeyAdapter()
      {
          public void keyTyped(KeyEvent ke)
           {
              char c = ke.getKeyChar();
              if (!((c >= '0') && (c <= '9') ||(c == KeyEvent.VK_BACK_SPACE) ||
              (c == KeyEvent.VK_DELETE))) 
             {
              getToolkit().beep();
               ke.consume();
            }
           }
      });
      pw.addKeyListener(new KeyAdapter()
      {
          public void keyTyped(KeyEvent ke)
           {
              char c = ke.getKeyChar();
              if (((c >= '0') && (c <= '9') ||(c == KeyEvent.VK_BACK_SPACE) ||
              (c == KeyEvent.VK_DELETE))) 
             {
              getToolkit().beep();
               ke.consume();
            }
           }
      });         
 }


public void setLookAndFeel(String name) 
{
    if(name == null)
      {
          return;
      }
      try
      {
          UIManager.setLookAndFeel(name);
      }
      catch(Exception ex)
      {
          javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage());
      }     

}

  public void actionPerformed(ActionEvent ae)
  {
      int cnt=0;
    if(ae.getSource()==login)
    {
        try
        {
             con=tb.getConnection();
             st=tb.getStatement();
             stble=new SelectTable(st);
             stble.getConnWithTable("login_table");
             rs=stble.getResultSet();
             String i=idtxt.getText();
             String p=pw.getText();
             while(rs.next())
             {
                 if(rs.getString(1).equals(i)&&rs.getString(2).equals(p))
                     cnt=1;
             }

        }
        catch(Exception e){}
        if(cnt==1)
        {
        JOptionPane.showMessageDialog(this,"Login Success......");
        ttg=new TTGFrame();
        ttg.setTitle("Time Table Application.TTG Frame Form");
        ttg.setVisible(true);
        setVisible(false);
        }
        else
        {
            JOptionPane.showMessageDialog(this,"Login Fail......try again?");
             idtxt.setText("");
             pw.setText("");
        }   
    }


    else
    {
        rf=new RegisterForm();
        rf.setTitle("Time Table Application.Registration Form");
        rf.setSize(1000,1000);
        rf.setVisible(true);
        setVisible(false);

    }

  }

  public JPanel getMyJPanel()
{
    return jpanelMain;
}

public String getMyName()
{
    return "LoginForm";
}


  public static void main(String args[]) throws Exception
  {
        LoginForm l=new LoginForm();
        l.setTitle("Time Table Application.Login Form");
        l.setSize(500,500);
        l.setResizable(false);
        l.setVisible(true);

        //this.setTitle("Time-Table Application.Branch");

        //setVisible(true);

  }

}  
  class WindowUtilities 
  {

    /** Tell system to use native look and feel, as in previous
        *  releases. Metal (Java) LAF is the default otherwise.
        */

    public static void setNativeLookAndFeel() 
        {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) 
        {
            System.out.println("Error setting native LAF: " + e);
        }
        }

        public static void setJavaLookAndFeel() 
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch(Exception e) 
        {
            System.out.println("Error setting Java LAF: " + e);
        }
    }

    public static void setMotifLookAndFeel() 
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        }
        catch(Exception e) 
        {
            System.out.println("Error setting Motif LAF: " + e);
        }
    }

    /** A simplified way to see a JPanel or other Container.
    *  Pops up a JFrame with specified Container as the content pane.
    */

    public static JFrame openInJFrame(Container content, int width, int height, String title, Color bgColor) 
    {
        JFrame frame = new JFrame(title);
        frame.setBackground(bgColor);
        content.setBackground(bgColor);
        frame.setSize(width, height);
        frame.setContentPane(content);
        frame.setVisible(true);
        return(frame);
    }

    /** Uses Color.white as the background color. */

    public static JFrame openInJFrame(Container content, int width, int height, String title) 
    {
        return(openInJFrame(content, width, height, title, Color.white));
    }

    /** Uses Color.white as the background color, and the
    *  name of the Container's class as the JFrame title.
    */

    public static JFrame openInJFrame(Container content, int width, int height) 
    {
        return(openInJFrame(content, width, height, content.getClass().getName(), Color.white));
    }
  }

我遇到以下异常:

  

线程“main”中的异常java.lang.IllegalArgumentException:无法添加到

layout:约束必须是字符串

at java.awt.CardLayout.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at LoginUser.LoginForm.<init>(LoginForm.java:158)
at LoginUser.LoginForm.main(LoginForm.java:277)

1 个答案:

答案 0 :(得分:0)

  

线程“main”中的异常java.lang.IllegalArgumentException:无法添加到   layout:约束必须是字符串

  jpanelMainMain.add(jpanelMain);

消息似乎很直接。使用CardLayout将组件添加到面板时,未指定String约束。

  cardLayout.show(jpanelMainMain ,"LoginForm");

如果在将面板添加到CardLayout时未指定约束,CardLayout如何知道哪个面板是“LoginForm”?

阅读How to Use CardLayout上Swing教程中的部分,了解一些工作示例,向您展示如何在向CardLayout添加面板时指定约束。它还显示了如何通过指定约束来交换面板。