Moncel 2022-12-07 17:09 采纳率: 86.7%
浏览 284
已结题

Java swing出现illegal component position报错如何解决

报错信息

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("");
            }
        }
    }

}




  • 写回答

2条回答 默认 最新

  • pzzhao 2022-12-07 18:05
    关注

    这一行 this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
    前面加上这个this.getLayeredPane().setLayout(null);

    另外需要注意你的图片路径,如果你是放在项目的根目录下你可以这么写,如果不是的话,相对项目根路径的路径都加上

    public void iniBG() {
            JLabel imgLabel = new JLabel(img);
            this.getLayeredPane().setLayout(null);
    //        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);
        }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月10日
  • 已采纳回答 12月7日
  • 创建了问题 12月7日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵