如何在Netbeans GUI Builder中根据父JLabel的大小自动缩放图像的大小? [英] How to make the image scale its size automatically according to the parent JLabel's size, in Netbeans GUI Builder?
问题描述
背景:- 我需要使用Netbeans GUI Builder制作Swing GUI.第一个子任务是在整个背景中显示图像.
我已按照教程完成.我基本上已经制作了一个JFrame,将其布局设置为GridBagLayout,然后向其中添加了一个透明的(通过取消选中opaque属性)JPanel. (问题1)
此后,我向JFrame添加了JLabel,删除了其文本并为其添加了图像. (问题2)
问题:-
-
首先,当我添加JPanel时,它不会显示其调整大小的句柄. 用Google搜索了一下,发现本教程,其中可以被看见 当他们创建一个JPanel时,它会自动显示其调整大小 手柄,可以拖动以调整其大小.
但是我的却不是(如下图所示)那么,是否有一些财产或某些可以 可以调整以便调整大小? 因为我的意图是为了使用 此透明面板上包含组件(按钮等) 背景,因此它应该经过整个屏幕/窗口/JFrame 父母.
-
第二,由于我使用的图像具有1024x768的尺寸,因此 它似乎比其父组件大得多. >
由于我是菜鸟,所以不确定背景的大小 需要通过某种方式测量像素宽度和 父像素的像素高度,然后转换实际图像的 在Adobe Photoshop之类的程序中将大小调整为该大小.但我是 确保必须有一种更实际的方法来做到这一点.
我希望图像在最初放置在其父对象上时会根据父对象的大小自动调整其大小 JLabel.我怎样才能做到这一点?请告诉我最简单的方法, 最好在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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>
/* 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
}
",或者是否有一种方法可以完全禁用窗口的大小."
您可以将框架的可调整大小属性设置为false.从NetBeans GUI Builder
- 从设计视图或导航器窗口中突出显示/选择框架组件.
- 转到右侧的属性窗口,查找属性
resizable
并确保未选中
我还想确保当我更改窗口的大小时,图像大小,其父JLabel的大小,JPanel的大小都将适应于框架"
一种方法是将背景绘制到背景面板上,而不是使用带有图标的标签.您可以在此处看到一个示例.对于GUI Builder,最简单的方法(无需编辑自动生成的代码,如果您不知道自己在做什么,我不建议这样做)是使用JPanel
表单而不是JPanel
窗体上绘画,然后可以将该JPanel
窗体添加到JFrame
窗体.您可以在此处中查看有关将JPanel
表单添加到JFrame
表单的简便方法.
-
请参见执行自定义绘画以进行学习有关绘画的更多信息
-
也可以看看此
StretchIcon
您可以 与标签一起使用.
更新
所以您的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);
}
});
}
}
BACKGROUND:- I am required to make a Swing GUI using Netbeans GUI Builder. The first sub-task is to display an image in the entire background.
I have followed a tutorial to get that done. I have basically made a JFrame, set its layout to GridBagLayout and then added a transparent (by unchecking the opaque property) JPanel to it. (Question 1)
After that I added a JLabel to the JFrame, Removed its text and added an image to it. (Question 2)
QUESTIONS:-
First, when I add the JPanel, it does not show its resize handles. I Googled a bit and found this tutorial, in which it can be seen that when they create a JPanel, it automatically shows its resize handles, which can be dragged to resize it.
But mine doesn't (screenshot below) So is there some property or something which can be adjusted so that I can resize it? Because my intention is to use this transparent panels to contain components (buttons etc.) on the background, so it should elapse the entire screen/window/JFrame parent.
Second, since the image I am using has some 1024x768 dimensions, so it appears to be way bigger than its parent components.
Since I am a noob and I am not sure if the size of the background image needs to be adjusted by somehow measuring the pixel width and pixel height of the parent and then converting the actual image's size to that size in some program like Adobe Photoshop. But I am sure there must a more practical way to do that.
I want the image to automatically resize itself according to the size of the parent when it is initially placed on on its parent JLabel. How can I do that? Please tell me the easiest way, preferably in the GUI Builder.
I also want to ensure that the image size, its parent JLabel's size, JPanel's size will all adjust to the frame when the I change the size of the window later when using this application, or if there is a way to disable the sizing of the window completely.
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>
/* 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
}
" or if there is a way to disable the sizing of the window completely."
You can set the resizable property of the frame to false. From NetBeans GUI Builder
- Highlight/select the frame component from the design view, or from the navigator window.
- Go to the properties window on the right and look for the property
resizable
and make sure it's unchecked
"I also want to ensure that the image size, its parent JLabel's size, JPanel's size will all adjust to the frame when the I change the size of the window"
One way is to paint the background onto the background panel, instead of using a label with an icon. You can see an example of that here. For the GUI Builder, the easiest way (without having to edit the auto-generated code, which I don't recommend, if you don't know what you are doing) is to use a JPanel
form instead of a JFrame
form. Paint on the JPanel
form, then you can add that JPanel
form to the JFrame
form. You can see here for an easy way to add the JPanel
form to the JFrame
form.
See Performing Custom Painting to learn more about painting
Can also have a look at this
StretchIcon
that you can use with a label.
UPDATE
So your JPanel
form class will ultimately look something like this
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);
}
});
}
}
这篇关于如何在Netbeans GUI Builder中根据父JLabel的大小自动缩放图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!