Java 如何在 Netbeans GUI Builder 中根据父级 JLabel 的大小自动调整图像大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23322564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make the image scale its size automatically according to the parent JLabel's size, in Netbeans GUI Builder?
提问by Solace
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.
背景:-我需要使用 Netbeans GUI Builder 制作 Swing GUI。第一个子任务是在整个背景中显示图像。
I have followed a tutorialto 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)
我已经按照教程来完成这项工作。我基本上制作了一个 JFrame,将其布局设置为 GridBagLayout,然后向其添加了一个透明的(通过取消选中 opaque 属性)JPanel。(问题 1)
After that I added a JLabel to the JFrame, Removed its text and added an image to it. (Question 2)
之后,我向 JFrame 添加了一个 JLabel,删除了它的文本并添加了一个图像。(问题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 intentionis 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.
首先,当我添加 JPanel 时,它不显示其调整大小的句柄。我google了一下,找到了这个教程,在里面可以看到,当他们创建一个JPanel时,它会自动显示它的resize handles,可以拖动它来调整它的大小。
但我的没有(下面的屏幕截图)那么是否有一些属性或可以调整的东西,以便我可以调整它的大小?因为我的意图是使用这个透明面板来包含背景上的组件(按钮等),所以它应该经过整个屏幕/窗口/JFrame 父级。
其次,由于我使用的图像有一些 1024x768 尺寸,所以 它看起来比它的父组件大得多。
由于我是一个菜鸟,我不确定是否需要通过以某种方式测量父级的像素宽度和像素高度,然后在 Adobe Photoshop 等程序中将实际图像的大小转换为该大小来调整背景图像的大小。但我相信必须有更实用的方法来做到这一点。
我希望图像在最初放置在其父 JLabel 上时根据父级的大小自动调整自身大小。我怎样才能做到这一点?请告诉我最简单的方法,最好在 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.
我还想确保图像大小、其父 JLabel 的大小、JPanel 的大小都将在我稍后使用此应用程序更改窗口大小时调整到框架,或者是否有办法禁用大小完全开窗。
EDIT1 @Braj
编辑1@布拉吉
/*
* 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
}
采纳答案by Paul Samsotha
" 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
您可以将框架的 resizable 属性设置为 false。来自 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
- 从设计视图或导航器窗口突出显示/选择框架组件。
- 转到右侧的属性窗口并查找该属性
resizable
并确保未选中它
"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"
“我还想确保当我更改窗口大小时,图像大小、其父 JLabel 的大小、JPanel 的大小都会调整到框架”
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 herefor an easy way to add the JPanel
form to the JFrame
form.
一种方法是将背景绘制到背景面板上,而不是使用带有图标的标签。你可以在这里看到一个例子。对于 GUI Builder,最简单的方法(无需编辑自动生成的代码,如果您不知道自己在做什么,我不建议这样做)是使用JPanel
表单而不是JFrame
表单。在JPanel
表单上绘画,然后您可以将该JPanel
表单添加到JFrame
表单中。您可以在此处查看将JPanel
表单添加到表单的简单方法JFrame
。
See Performing Custom Paintingto learn more about painting
Can also have a look at this
StretchIcon
that you canuse with a label.
请参阅执行自定义绘画以了解有关绘画的更多信息
也可以看看这个
StretchIcon
,你可以用一个标签。
UPDATE
更新
So your JPanel
form class will ultimately look something like this
所以你的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);
}
});
}
}