报错信息
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.desktop/java.awt.Container.addImpl(Container.java:1120)
at java.desktop/javax.swing.JLayeredPane.addImpl(JLayeredPane.java:237)
at java.desktop/java.awt.Container.add(Container.java:997)
at FRAME.LoginFrame.iniBG(LoginFrame.java:38)
at FRAME.LoginFrame.<init>(LoginFrame.java:30)
at Main.main(Main.java:12)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LoginFrame extends JFrame implements ActionListener {
private JButton oKButton;
private JButton cancelButton;
private JLabel userJLabel;
private JLabel pwdJLabel;
private int counter = 0;
private JTextField userField;
private JPasswordField pwdField;
ImageIcon img = new ImageIcon("BGL.jpg");
public LoginFrame() {
setTitle("登录界面");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setBounds(100, 100, 400, 300);
initCom();
iniBG();
setVisible(true);
}
public void iniBG(){
JLabel imgLabel = new JLabel(img);
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(100,100,img.getIconWidth(),img.getIconHeight());
this.getLayeredPane().add(imgLabel,new Integer(Integer.MIN_VALUE));
((JPanel)this.getContentPane()).setOpaque(false);
}
public void initCom() {
userJLabel = new JLabel("用户名");
userJLabel.setBounds(100, 80, 50, 30);
add(userJLabel);
userField = new JTextField();
userField.setBounds(150, 80, 100, 30);
add(userField);
pwdJLabel = new JLabel("密 码");
pwdJLabel.setBounds(100, 130, 50, 30);
add(pwdJLabel);
pwdField = new JPasswordField();
pwdField.setBounds(150, 130, 100, 30);
add(pwdField);
pwdField.setEchoChar('$');
oKButton = new JButton("提交");
oKButton.setBounds(100, 200, 70, 40);
add(oKButton);
oKButton.addActionListener(this);
cancelButton = new JButton("重置");
cancelButton.setBounds(220, 200, 70, 40);
add(cancelButton);
cancelButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == oKButton) {
if (userField.getText().equals("") || pwdField.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
}else{
if (userField.getText().equals("dh") && pwdField.getText().equals("123")) {
new MainFrame();
this.dispose();
} else {
userField.setText("");
pwdField.setText("");
counter++;
if (counter == 5) {
System.exit(0);
}
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
} else {
if (arg0.getSource() == cancelButton) {
userField.setText("");
pwdField.setText("");
}
}
}
}