有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过JLabel上传背景图像(jpg)后,如何将图像缩放到它所在的面板上?

所以我已经制作了一个包含多个标签、文本字段和按钮的面板。在小组中,我放置了另一个JLabel并转到属性->;图标并上载了一幅图像,但该图像(我正试图用作背景)无法缩放到面板

如何使图像适合缩放面板尺寸? 例如,这是一个名为Sample的新项目JFrame,我添加了一个变量名为pnMainJPanel和一个变量名为lblBackgroundJLabel。带着标签,我转到属性->;图标,然后上传了一个图像

package tempo;

public class Sample extends javax.swing.JFrame {

    /**
     * Creates new form Sample
     */
    public Sample() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        pnMain = new javax.swing.JPanel();
        lblBackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnMain.setLayout(null);

        lblBackground.setIcon(new javax.swing.ImageIcon("C:\\Users\\Anderson\\Desktop\\Monkee-Boy Blurred Backgrounds\\bkg-3.jpg")); // NOI18N
        pnMain.add(lblBackground);
        lblBackground.setBounds(0, 0, 400, 280);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }                  

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sample().setVisible(true);
            }
        });
    }

    // Variables declaration                     
    private javax.swing.JLabel lblBackground;
    private javax.swing.JPanel pnMain;
    // End of variables declaration                   
}

共 (1) 个答案

  1. # 1 楼答案

    您可以使用Stretch Icon并将图标添加到JLabel,将标签添加到面板。因此,面板的布局管理器需要调整添加到面板的任何组件的大小。一种简单的方法是将面板的布局管理器设置为BorderLayout

    Icon将调整大小以填充标签可用的空间。因此,如果标签调整大小,Icon将调整大小

    您还可以控制缩放是否成比例