请问怎么在JPanel中添加图片啊

java

package PPS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class loginFram extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

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

    /**
     * Create the frame.
     */
    public loginFram() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("\u7528\u6237\u767B\u5F55\u754C\u9762");
        label.setBounds(5, 5, 422, 24);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label);

        JLabel label_1 = new JLabel("\u7528\u6237\u540D");
        label_1.setBounds(124, 79, 72, 18);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(211, 76, 86, 24);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_2 = new JLabel("\u5BC6\u7801");
        label_2.setBounds(125, 124, 72, 18);
        contentPane.add(label_2);

        textField_1 = new JTextField();
        textField_1.setBounds(211, 121, 86, 24);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.setBounds(104, 194, 113, 27);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnNewButton_1.setBounds(231, 194, 113, 27);
        contentPane.add(btnNewButton_1);

        JPanel panel = new JPanel(); 
        panel.setBounds(14, 79, 96, 79);
        contentPane.add(panel);
    }
}

1个回答

private Icon icon;
private JLabel l;
icon=new ImageIcon("C:\Users\Administrator\Pictures\cat.jpg");

l=new JLabel();
icon=new ImageIcon("C:\Users\Administrator\Pictures\cat.jpg");     //在此直接创建对象,注意目录之间的分隔符是双斜线
l.setIcon(icon);
l.setBounds(10, 10, icon.getIconWidth(),icon.getIconHeight());
JPanel panel = new JPanel();
panel.add(l,new Integer(Integer.MIN_VALUE));

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!