Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.IllegalArgumentException:无法添加到布局:约束必须是字符串_Java_Swing - Fatal编程技术网

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

“线程中的异常”;“主要”;java.lang.IllegalArgumentException:无法添加到布局:约束必须是字符串,java,swing,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.A

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

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));
    }
  }
包登录用户;
导入java.awt.*;
导入javax.swing.UIManager;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入javax.swing.*;
导入java.awt.Color;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.KeyAdapter;
导入java.awt.event.KeyEvent;
导入java.sql.Connection;
导入java.sql.ResultSet;
导入java.sql.Statement;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JPasswordField;
导入javax.swing.JTextField;
导入DBconnection.SelectTable;
导入DBconnection.TestConnection;
进口分公司;
导入GUITTG.TTGFrame;
导入javax.swing.border.LineBorder;
导入javax.swing.border.TitledBorder;
公共类LoginForm扩展JFrame实现ActionListener
{
私有静态最终长serialVersionUID=1L;
char ch;
//集装箱con1;
JPanel jpanelMain、jpanelMain、jpanelMainC;
JPanel jpanelMainC1、jpanelMainC2、jpanelMainC3;
JPanle jpanelMainC4、jpanelMainC5、jpanelMainC6;
卡片布局卡片布局;
JButton登录、注册;
JLabel id,pass;
JTextField-idtxt;
字体;
JPasswordField pw;
注册执行射频;
测试连接tb;
可选择表格;
报表st;
连接con;
结果集rs;
ttg帧ttg;
集装箱con1;
公共登录表单()
{
UIManager.getLookAndFeelDefaults().put(“类加载器”,this.getClass().getClassLoader());
this.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
挫折背景(新颜色(0,0,0));
con1=getContentPane();
cardLayout=新的cardLayout();
JPanelMain=新的JPanel(cardLayout);
字体=新字体(“标题”,字体.普通,20);
id=新的JLabel(“登录id:”);
id.setFont(字体);
id.setForeground(颜色:白色);
pass=新的JLabel(“密码:”);
pass.setFont(字体);
通过设置前景(颜色为白色);
idtxt=新的JTextField();
pw=新的JPasswordField();
login=新的JButton(“login”);
寄存器=新的JButton(“寄存器”);
jpanelMain=newjpanel(newborderlayout());
jpanelMain.setboorder(新标题边框(新线条边框(新颜色(0,0,0)),“登录表单”,0,0,字体,新颜色(0,0,0));
添加(新JLabel(),BorderLayout.SOUTH);
/*jpanelMainC1=newjpanel(newborderlayout());
jpanelMainC1.add(id);
jpanelMainC2=newjpanel(newborderlayout());
jpanelMainC2.add(pass);
jpanelMainC3=newjpanel(newborderlayout());
jpanelMainC3.add(idtxt);
jpanelMainC4=新的JPanel(新的BorderLayout());
jpanelMainC4.add(pw);
jpanelMainC5=新的JPanel(新的BorderLayout());
jpanelMainC5.add(登录);
jpanelMainC6=newjpanel(newborderlayout());
jpanelMainC6.add(寄存器);
*/
jpanelMainC=newjpanel(newborderlayout());
jpanelMainC.add(id);
jpanelMainC.add(通过);
jpanelMainC.add(idtxt);
jpanelMainC.add(pw);
jpanelMainC.add(登录);
jpanelMainC.add(寄存器);
/*jpanelMainC=newjpanel(newborderlayout());
jpanelMainC.add(jpanelMainC1);
jpanelMainC.add(jpanelMainC2);
jpanelMainC.add(jpanelMainC3);
jpanelMainC.add(jpanelMainC4);
jpanelMainC.add(jpanelMainC5);
jpanelMainC.add(jpanelMainC6)*/
添加(jpanelMainC,BorderLayout.CENTER);
jpanelMainC.setBackground(新颜色(0150150));
JPanelMain.setBackground(新颜色(0150150));
添加(id);
添加(通过);
添加(idtxt);
添加(pw);
添加(登录);
添加(注册);
con1.setLayout(空);
con1.立根地面(新颜色(0150150));
id.setBounds(100100,30);
通过立根(100150100,20);
idtxt.setBounds(210100100,20);
pw.立根(210150100,20);
login.setBounds(100200100,20);
注册.立根(210200100,20);
login.addActionListener(this);
register.addActionListener(this);
//con1.添加(jpanelMain);
jpanelMain.add(jpanelMain);
cardLayout.show(JPanelMain,“LoginForm”);
tb=新的TestConnection();
addKeyListener(新的KeyAdapter()
{
public void keyTyped(KeyEvent ke)
{
char c=ke.getKeyChar();
如果(!((c>='0')&&(c='0')&(c
线程“main”java.lang.IllegalArgumentException中的异常:无法添加到
布局:约束必须是字符串

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)
我觉得这条消息很直截了当。当您使用CardLayout向面板添加组件时,并没有指定字符串约束

  cardLayout.show(jpanelMainMain ,"LoginForm");
如果在将面板添加到CardLayout时未指定约束,CardLayout如何知道哪个面板是“LoginForm”

阅读上的Swing教程中的部分,以获得一个工作示例,该示例演示如何在向CardLayout添加面板时指定约束。它还演示了如何通过指定约束来交换面板

  cardLayout.show(jpanelMainMain ,"LoginForm");