java1996
java1996
采纳率1.4%
2016-08-30 15:10 阅读 2.5k

JFrame怎么添加背景图片

package edu.jmi.xyh.view;

import java.awt.BorderLayout;

public class LoginView extends JFrame {
private JTextField txID;
private JTextField txPw;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;

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

/**
 * Create the frame.
 */
public LoginView() {
    setTitle("\u6B22\u8FCE\u767B\u5F55\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 445, 306);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setForeground(Color.BLACK);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
    lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 12));
    lblNewLabel.setBounds(114, 98, 54, 15);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
    lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 12));
    lblNewLabel_1.setBounds(114, 135, 54, 15);
    contentPane.add(lblNewLabel_1);

    textField = new JTextField();
    textField.setBounds(204, 95, 118, 21);
    contentPane.add(textField);
    textField.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setBounds(204, 132, 118, 21);
    contentPane.add(passwordField);

    JButton btnNewButton = new JButton("\u767B\u5F55");
    btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //登陆代码,将用户输入的内容打印在控制台中
            String id=txID.getText();
            String pw=txPw.getText();
            StudentDao studentDao=new StudentDao();
            System.out.println("id="+id+"pw="+pw);
        }
    });
    btnNewButton.setBounds(75, 198, 93, 23);
    contentPane.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
    btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12));
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //单击事件
            System.exit(0);
        }
    });
    btnNewButton_1.setBounds(260, 198, 93, 23);
    contentPane.add(btnNewButton_1);

    JLabel label = new JLabel("\u6B22 \u8FCE \u767B \u5F55 \u5B66 \u751F");
    label.setFont(new Font("宋体", Font.PLAIN, 17));
    label.setBounds(131, 21, 186, 23);
    contentPane.add(label);

    JLabel label_1 = new JLabel("\u7BA1 \u7406 \u7CFB \u7EDF");
    label_1.setFont(new Font("宋体", Font.PLAIN, 17));
    label_1.setBounds(156, 54, 130, 23);
    contentPane.add(label_1);

    JLabel lblAdmin = new JLabel("\u5982\u679C\u60A8\u9996\u6B21\u767B\u9646\u7528\u6237\u540D\u4E3A  admin  \u5BC6\u7801\u4E3A\u7A7A\uFF01");
    lblAdmin.setForeground(Color.RED);
    lblAdmin.setBounds(86, 173, 267, 15);
    contentPane.add(lblAdmin);


}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • begpro TO_A_DAY 2016-08-30 15:39

    创建一个类,继承JPanel,然后重写paintComponent(Graphics g),在JPanel上利用画笔Graphics g画出图片
    代码大致如下:图片说明
    ImageIcon imageIcon = new ImageIcon(priturePath);
    Image image = imageIcon.getImage();
    g.drawImage(image, 20, 20, 450, 600, this);

    点赞 评论 复制链接分享

相关推荐