qq_42039299
璀璨00
采纳率100%
2018-12-20 09:23

请问怎么在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条回答

  • weixin_43547591 weixin_43547591 3年前

    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));

    点赞 评论 复制链接分享

相关推荐