| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3327 人关注过本帖
标题:怎么加背景图片
只看楼主 加入收藏
相思豆
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2016-10-25
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:5 
怎么加背景图片
package com.artisan.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
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 javax.swing.
import javax.swing.border.EmptyBorder;

import com.artisan.dao.AdminDao;
import com.artisan.dao.StudentDao;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import com.artisan.model.UserType;
import com.artisan.util.StringUtil;
import java.awt.Color;

public class LoginFrm extends JFrame {

    private JPanel contentPane;
    private JTextField userNameTextField;
    private JPasswordField passwordTextField;
    private JComboBox userTypeComboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrm frame = new LoginFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginFrm() {
        setTitle("\u767B\u9646\u754C\u9762");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 540, 379);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        setLocationRelativeTo(null);
        JLabel label = new JLabel(
                "\u5B66\u751F\u9009\u8BFE\u7BA1\u7406\u7CFB\u7EDF\u767B\u9646\u754C\u9762");
        label.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/logo.png")));
        label.setFont(new Font("微软雅黑", Font.BOLD, 18));

        JLabel label_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
        label_1.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/\u7528\u6237\u540D.png")));
        label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        userNameTextField = new JTextField();
        userNameTextField.setBackground(new Color(250, 240, 230));
        userNameTextField.setColumns(10);

        JLabel label_2 = new JLabel("\u5BC6   \u7801\uFF1A");
        label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        label_2.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/\u5BC6\u7801.png")));

        passwordTextField = new JPasswordField();
        passwordTextField.setBackground(new Color(255, 245, 238));
        passwordTextField.setColumns(10);

        JLabel label_3 = new JLabel("\u7528\u6237\u7C7B\u578B\uFF1A");
        label_3.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/userType.png")));
        label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        userTypeComboBox = new JComboBox();
        userTypeComboBox.setModel(new DefaultComboBoxModel(new UserType[] {
                UserType.ADMIN, UserType.TEACHER, UserType.STUDENT }));
        userTypeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        JButton loginButton = new JButton("\u767B\u5F55");
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                loginAct(ae);
            }
        });
        loginButton.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/\u767B\u5F55.png")));
        loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        JButton resetButton = new JButton("\u91CD\u7F6E");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                restValue(ae);
            }
        });
        resetButton.setIcon(new ImageIcon(LoginFrm.class
                .getResource("/images/\u91CD\u7F6E.png")));
        resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane
                .setHorizontalGroup(gl_contentPane
                        .createParallelGroup(Alignment.LEADING)
                        .addGroup(
                                Alignment.TRAILING,
                                gl_contentPane
                                        .createSequentialGroup()
                                        .addGroup(
                                                gl_contentPane
                                                        .createParallelGroup(
                                                                Alignment.TRAILING)
                                                        .addGroup(
                                                                gl_contentPane
                                                                        .createSequentialGroup()
                                                                        .addContainerGap(
                                                                                118,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(
                                                                                label))
                                                        .addGroup(
                                                                Alignment.LEADING,
                                                                gl_contentPane
                                                                        .createSequentialGroup()
                                                                        .addGap(134)
                                                                        .addGroup(
                                                                                gl_contentPane
                                                                                        .createParallelGroup(
                                                                                                Alignment.LEADING)
                                                                                        .addGroup(
                                                                                                gl_contentPane
                                                                                                        .createSequentialGroup()
                                                                                                        .addGroup(
                                                                                                                gl_contentPane
                                                                                                                        .createParallelGroup(
                                                                                                                                Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                label_1)
                                                                                                                        .addComponent(
                                                                                                                                label_2))
                                                                                                        .addGap(18)
                                                                                                        .addGroup(
                                                                                                                gl_contentPane
                                                                                                                        .createParallelGroup(
                                                                                                                                Alignment.LEADING)
                                                                                                                        .addComponent(
                                                                                                                                passwordTextField,
                                                                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                                                                168,
                                                                                                                                Short.MAX_VALUE)
                                                                                                                        .addComponent(
                                                                                                                                userNameTextField,
                                                                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                                                                182,
                                                                                                                                Short.MAX_VALUE)))
                                                                                        .addGroup(
                                                                                                gl_contentPane
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                label_3)
                                                                                                        .addPreferredGap(
                                                                                                                ComponentPlacement.RELATED)
                                                                                                        .addComponent(
                                                                                                                userTypeComboBox,
                                                                                                                0,
                                                                                                                162,
                                                                                                                Short.MAX_VALUE)))))
                                        .addGap(116))
                        .addGroup(
                                gl_contentPane.createSequentialGroup()
                                        .addGap(156).addComponent(loginButton)
                                        .addGap(61).addComponent(resetButton)
                                        .addContainerGap(127, Short.MAX_VALUE)));
        gl_contentPane
                .setVerticalGroup(gl_contentPane
                        .createParallelGroup(Alignment.LEADING)
                        .addGroup(
                                gl_contentPane
                                        .createSequentialGroup()
                                        .addGap(26)
                                        .addComponent(label)
                                        .addGap(33)
                                        .addGroup(
                                                gl_contentPane
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(label_1)
                                                        .addComponent(
                                                                userNameTextField,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(28)
                                        .addGroup(
                                                gl_contentPane
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(label_2)
                                                        .addComponent(
                                                                passwordTextField,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(36)
                                        .addGroup(
                                                gl_contentPane
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(label_3)
                                                        .addComponent(
                                                                userTypeComboBox,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(
                                                ComponentPlacement.RELATED, 29,
                                                Short.MAX_VALUE)
                                        .addGroup(
                                                gl_contentPane
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(
                                                                resetButton)
                                                        .addComponent(
                                                                loginButton))
                                        .addGap(22)));
        contentPane.setLayout(gl_contentPane);

    }

    protected void loginAct(ActionEvent ae) {
        // TODO Auto-generated method stub
        String userName = userNameTextField.getText().toString();
        String password = passwordTextField.getText().toString();
        UserType selectedItem = (UserType) userTypeComboBox.getSelectedItem();
        if (StringUtil.isEmpty(userName)) {
            JOptionPane.showMessageDialog(this, "用户名不能为空!");
            return;
        }
        if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(this, "密码不能为空!");
            return;
        }
        Admin admin = null;
        if ("系统管理员".equals(selectedItem.getName())) {
            AdminDao adminDao = new AdminDao();
            Admin adminTmp = new Admin();
            adminTmp.setName(userName);
            adminTmp.setPassword(password);
            admin = adminDao.login(adminTmp);
            adminDao.closeDao();
            if (admin == null) {
                JOptionPane.showMessageDialog(this, "用户名或密码错误!");
                return;
            }
            JOptionPane.showMessageDialog(this, "欢迎【" + selectedItem.getName()
                    + "】:" + admin.getName() + "登录本系统!");
            this.dispose();
            new MainFrm(selectedItem, admin).setVisible(true);
        } else if ("教师".equals(selectedItem.getName())) {
            // 教师登录
            Teacher teacher = null;
            TeacherDao teacherDao = new TeacherDao();
            Teacher teacherTmp = new Teacher();
            teacherTmp.setName(userName);
            teacherTmp.setPassword(password);
            teacher = teacherDao.login(teacherTmp);
            teacherDao.closeDao();
            if (teacher == null) {
                JOptionPane.showMessageDialog(this, "用户名或密码错误!");
                return;
            }
            JOptionPane.showMessageDialog(this, "欢迎【" + selectedItem.getName()
                    + "】:" + teacher.getName() + "登录本系统!");
            this.dispose();
            new MainFrm(selectedItem, teacher).setVisible(true);
        } else {
            // 学生登录
            Student student = null;
            StudentDao studentDao = new StudentDao();
            Student studentTmp = new Student();
            studentTmp.setName(userName);
            studentTmp.setPassword(password);
            student = studentDao.login(studentTmp);
            studentDao.closeDao();
            if (student == null) {
                JOptionPane.showMessageDialog(this, "用户名或密码错误!");
                return;
            }
            JOptionPane.showMessageDialog(this, "欢迎【" + selectedItem.getName()
                    + "】:" + student.getName() + "登录本系统!");
            this.dispose();
            new MainFrm(selectedItem, student).setVisible(true);
        }
    }

    protected void restValue(ActionEvent ae) {
        // TODO Auto-generated method stub
        userNameTextField.setText("");
        passwordTextField.setText("");
        userTypeComboBox.setSelectedIndex(0);
    }
}
想加个图片背景,就是加不上,求大佬帮忙加个
搜索更多相关主题的帖子: import swing Student new this 
2019-04-07 14:03
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
报什么错?
我试了下你的代码,图片放在src下,可以正常访问
不过只是个登录页面应该用不到这么多代码


[此贴子已经被作者于2019-4-7 17:36编辑过]


剑栈风樯各苦辛,别时冰雪到时春
2019-04-07 17:30
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
其他模块代码量应该也很多,先改这个吧。
程序代码:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Login implements ActionListener {
    // 界面控件元素
    static JTextField nameTxt = new JTextField();//用户名
    static JTextField pwdTxt = new JTextField();//密码
    static JComboBox<String> typeComb = new JComboBox<String>();//用户类型
    //登录,重置按钮
    static JButton okBtn = new JButton("登陆");
    static JButton resetBtn = new JButton("重置");
    void setLocationOfEle(JFrame jframe) {
        int txtLeft = 220;
        int lblLeft = 160;
        //
        nameTxt.setBounds(txtLeft, 100, 180, 22);
        pwdTxt.setBounds(txtLeft, 140, 180, 22);
        typeComb.setBounds(txtLeft, 180, 180, 22);
        typeComb.addItem("教师");
        typeComb.addItem("学生");
        okBtn.setBounds(180, 280, 60, 32);
        resetBtn.setBounds(280, 280, 60, 32);
        JLabel lbl = new JLabel("学生选课管理系统登录界面");
        lbl.setFont(new Font("gbk", Font.BOLD, 22));
        lbl.setBounds(120, 40, 320, 32);
        jframe.add(lbl);
        lbl = new JLabel("用户名:");
        lbl.setBounds(lblLeft, 100, 60, 22);
        jframe.add(lbl);
        lbl = new JLabel("密   码:");
        lbl.setBounds(lblLeft, 140, 60, 22);
        jframe.add(lbl);
        lbl = new JLabel("用户类型:");
        lbl.setBounds(lblLeft, 180, 60, 22);
        jframe.add(lbl);
        jframe.add(nameTxt);
        jframe.add(pwdTxt);
        jframe.add(typeComb);
        jframe.add(okBtn);
        jframe.add(resetBtn);
        okBtn.addActionListener(Login.this);
        resetBtn.addActionListener(Login.this);
    }
    public static void main(String[] args) {
        JFrame jframe = new JFrame();
        jframe.setLayout(null);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //
        Login login = new Login();
        login.setLocationOfEle(jframe);
        jframe.setBounds(100, 100, 540, 379);
        jframe.setVisible(true);
        //加背景
        ImageIcon img = new ImageIcon("src/images/login.jpg");
        JLabel imgLabel = new JLabel(img);
        jframe.getContentPane().add(imgLabel);
        ((JPanel)jframe.getContentPane()).setOpaque(true);
        imgLabel.setBounds(0, 0, 540, 379);
        jframe.setIconImage(img.getImage());

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String btnTxt = e.getActionCommand();
        if("登陆".equals(btnTxt)) {
            System.out.println("用户名:" + nameTxt.getText());
            System.out.println("密    码:" + pwdTxt.getText());
            System.out.println("用户类型:" + typeComb.getSelectedItem());
        } else if("重置".equals(btnTxt)) {
            Arrays.asList(nameTxt, pwdTxt).forEach(ele->ele.setText(""));
            typeComb.setSelectedIndex(0);
        }
    }
}

图片附件: 游客没有浏览图片的权限,请 登录注册

剑栈风樯各苦辛,别时冰雪到时春
2019-04-07 19:00
相思豆
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2016-10-25
收藏
得分:0 
新建文件夹 (4).rar (664.31 KB)

发个项目,里面的MainFrm.java我自己添加的背景有问题,请大佬们帮忙加个背景
2019-04-08 16:23
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
desktopPane = new JDesktopPane();
        desktopPane.setBackground(new Color(0, 128, 128));
        contentPane.add(desktopPane, BorderLayout.CENTER);
                JLabel l=new JLabel();
                Icon icon=new ImageIcon("src/images/pic.png");     //在此直接创建对象,图片放到这个images文件夹下
                l.setIcon(icon);
            l.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
            desktopPane.add(l);
                setVisible(true);
        setLocationRelativeTo(null);
        setAuthority();
    }

    protected void addStudentClass(ActionEvent ae) {








好像用了什么工具开发的,后期可以优化

剑栈风樯各苦辛,别时冰雪到时春
2019-04-08 21:51
小文子09
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2020-2-27
收藏
得分:0 
回复 3楼 林月儿
非常感谢
2020-02-29 16:36
快速回复:怎么加背景图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.126361 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved