如何在Netbeans GUI Builder中根据父JLabel的大小自动缩放图像大小?

背景: –我需要使用Netbeans GUI Builder制作Swing GUI。 第一个子任务是在整个背景中显示图像。

我已经按照教程完成了这项工作。 我基本上已经创建了一个JFrame,将其布局设置为GridBagLayout,然后添加透明(通过取消选中opaque属性)JPanel。 (问题1)

之后我将JLabel添加到JFrame,删除了它的文本并添加了一个图像。 (问题2)

问题: –

  1. 首先,当我添加JPanel时, 它不显示其resize句柄。 我用Google搜索了一下这个教程 ,在其中可以看到,当他们创建JPanel时,它会自动显示其resize句柄,可以拖动它来resize。

    但是我没有 (截图如下) 那么是否有一些属性或某些东西可以调整以便我可以resize? 因为我的意图是使用这个透明面板来包含背景上的组件(按钮等),所以它应该经过整个屏幕/窗口/ JFrame父级。 在此处输入图像描述

  2. 其次,由于我使用的图像有一些1024×768的尺寸,所以它看起来比它的父组件大。 在此处输入图像描述

    由于我是菜鸟,我不确定是否需要通过某种方式测量父级的像素宽度和像素高度来调整背景图像的大小,然后在某些程序(如Adobe Photoshop)中将实际图像的大小转换为该大小。 但我相信必须有更实际的方法来做到这一点。

    我希望图像在最初放置在父JLabel上时根据父级的大小自动resize。 我怎样才能做到这一点? 请告诉我最简单的方法,最好是在GUI Builder中。

我还想确保图像大小,其父JLabel的大小,JPanel的大小都将在我稍后使用此应用程序时更改窗口大小时调整到帧,或者是否有办法禁用大小调整完全是窗户。


EDIT1 @Braj

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dev_nna.dbp; public class JFrameParent extends javax.swing.JFrame { /** * Creates new form JFrameParent */ public JFrameParent() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //  private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.GridBagLayout()); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); getContentPane().add(jPanel1, new java.awt.GridBagConstraints()); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/dev_nna/dbp/scheduler/resources/Abstract-white-and-blue-backgrounds.jpg"))); // NOI18N jLabel1.setText("jLabel1"); getContentPane().add(jLabel1, new java.awt.GridBagConstraints()); pack(); }//  /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ 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(JFrameParent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(JFrameParent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(JFrameParent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(JFrameParent.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 JFrameParent().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; // End of variables declaration } 

“或者是否有办法完全禁用窗户的尺寸。”

您可以将框架的resizable属性设置为false。 来自NetBeans GUI Builder

  1. 从设计视图或导航器窗口突出显示/选择框架组件。
  2. 转到右侧的属性窗口,查找resizable的属性并确保其未选中

“我还想确保当我改变窗口的大小时,图像大小,其父JLabel的大小,JPanel的大小都会调整到帧”

一种方法是将背景绘制到背景面板上,而不是使用带有图标的标签。 你可以在这里看到一个例子。 对于GUI Builder,最简单的方法(无需编辑自动生成的代码,我不建议,如果您不知道自己在做什么)是使用JPanel表单而不是JFrame表单。 在JPanel表单上绘制,然后您可以将该JPanel表单添加到JFrame表单。 您可以在此处看到将JPanel表单添加到JFrame表单的简单方法。

  • 有关绘画的详细信息,请参阅执行自定义绘画

  • 还可以看看这个可以与标签一起使用的StretchIcon


UPDATE

所以你的JPanel表单类最终会看起来像这样

 public class PanelForm extends javax.swing.JPanel { private BufferedImage image; public PanelForm() { try { image = ImageIO.read(getClass().getResource("/path/to/image/png")); } catch (IOException ex) { ex.printStackTrace(); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, getWidth(), getHeight(), this); } @Override public Dimension getPreferredSize() { return new Dimension(500, 500); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { JFrame frame = new JFrame(); frame.add(new PanelForm()); // <--- add it here frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }