qq_36765799 2019-04-14 21:45 采纳率: 57.1%
浏览 593
已采纳

我swing了一个登陆界面,在eclipse中运行时显示正常,但是生成jar并运行jar后,背景不显示。

图片说明图片说明

我swing了一个登陆界面,在eclipse中运行时显示正常,但是生成jar并运行jar后,背景不显示。

public class Login extends JFrame {

    JPanel contentPane;
    JPasswordField passwordField;

    public static void main(String[] args) {
                Login frame = new Login();
    }

    public  Login() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(600, 300, 600, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //组件
        JLabel label1 = new JLabel("\u6B22\u8FCE\u767B\u9646\u7A3B\u7530\u7EFC\u5408\u79CD\u517B\u4FE1\u606F\u670D\u52A1\u7CFB\u7EDF");
        label1.setForeground(Color.BLACK);
        label1.setFont(new Font("微软雅黑", Font.BOLD, 30));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        label1.setBounds(34, 51, 511, 53);
        contentPane.add(label1);

        JLabel label2 = new JLabel("\u7528\u6237\u540D");
        label2.setFont(new Font("宋体", Font.PLAIN, 20));
        label2.setForeground(Color.BLACK);
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setBounds(77, 136, 72, 32);
        contentPane.add(label2);

        JLabel label3 = new JLabel("\u5BC6\u7801");
        label3.setForeground(Color.BLACK);
        label3.setFont(new Font("宋体", Font.PLAIN, 20));
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        label3.setBounds(77, 208, 72, 32);
        contentPane.add(label3);

        setVisible(true);
        setTitle("登陆");

        //登陆界面布局
        JButton button1=new JButton("登陆");
        button1.setBounds(147, 284, 113, 27);
        contentPane.add(button1);

        JTextField textField=new JTextField();
        textField.setBounds(177, 140, 302, 24);
        contentPane.add(textField);
        textField.setFont(new Font("宋体",Font.PLAIN,20));
        textField.setColumns(10);

        JPasswordField passwordField=new JPasswordField();
        passwordField.setBounds(177, 214, 302, 24);
        passwordField.setFont(new Font("宋体",Font.PLAIN,20));
        contentPane.add(passwordField);

        JButton button2 = new JButton("清除");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
                passwordField.setText("");
            }
        });
        button2.setBounds(330, 284, 113, 27);
        contentPane.add(button2);

        //设置背景
        ImageIcon imageIcon=new ImageIcon("src/picture/1.jpg");
        JLabel label=new JLabel(imageIcon);
        label.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        JPanel panel=(JPanel)getContentPane();
        panel.setOpaque(false);
        JPanel panel2=new JPanel();
        panel2.setOpaque(false);
        panel2.setLayout(null);

        //登陆功能

        textField.setText("admin");
        passwordField.setText("123");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String str=textField.getText();
                char ch[]=passwordField.getPassword();
                String pass=new String(ch);

                /*
                boolean bool1= str=="admin";
                boolean bool2= pass=="123";

                System.out.println(bool1);
                System.out.println(bool2);
                */
                if (str.equals("admin") && pass.equals("123") ){
                    mainpage p=new mainpage();
                    dispose();
                }
                else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误!","登陆失败!",JOptionPane.PLAIN_MESSAGE);
                }
            }
        });
    }
}
  • 写回答

2条回答 默认 最新

  • 拂晓的猫头鹰 2019-04-15 17:28
    关注

    资源位置引入有问题,可以把图片放在包外面,启动时资源路径给main方法传入

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献