java 如何将 JButton 放在 JLabel 上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16456381/
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 put JButton on JLabel?
提问by Jay
I'm making app in Java Swing with Netbeans IDE. In my app I put multiple JInternalFrame
on JDesktopPane
. I put JLabel on JinternalFrame. I set Image in JLabel like
我正在使用 Netbeans IDE 在 Java Swing 中制作应用程序。在我的应用程序中,我JInternalFrame
在JDesktopPane
. 我把 JLabel 放在 JinternalFrame 上。我在 JLabel 中设置了图像
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/myImage.jpg")));
now i want to add JButton on that Label.So,
现在我想在那个 Label.So 上添加 JButton,
How to put JButton
on JLabel
(which contain Image
) in Java Swing?
如何把JButton
上JLabel
(包含Image
)中的Java Swing?
Sorry for late update. What i want to do is in below picture.
Before Image.
抱歉更新晚了。我想要做的是在下面的图片中。
在图像之前。
After Image.(What i want it here.)
在图像之后。(我在这里想要什么。)
采纳答案by Jay
Thanks to all for reply to me and helping to me. I had done and solve my problem with the help of GridBagLayOut and GridBagConstraints as per my below code :
感谢大家对我的回复和帮助。根据我的以下代码,我在 GridBagLayOut 和 GridBagConstraints 的帮助下完成并解决了我的问题:
enter code here
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author JAY
*/
public class HomePageOfApp {
private javax.swing.JLabel jLabel1;
private javax.swing.JButton aboutUsBtn;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton powerBtn;
private javax.swing.JButton presetBtn;
private javax.swing.JButton settingBtn;
static JInternalFrame internalFrame = null;
private static final GridBagConstraints gbc;
static {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
}
public HomePageOfApp() {
initComponents();
internalFrame = new JInternalFrame("Home Page", false, false, true, false);
internalFrame.setBounds(0, 0, 784, 434);
internalFrame.setLocation(0, 0);
internalFrame.setContentPane(wrapInBackgroundImage(jPanel1,
new ImageIcon(
getClass().getResource("/myImage1.jpg"))));
internalFrame.setVisible(true);
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
powerBtn = new javax.swing.JButton();
presetBtn = new javax.swing.JButton();
aboutUsBtn = new javax.swing.JButton();
settingBtn = new javax.swing.JButton();
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setLayout(new java.awt.GridBagLayout());
powerBtn.setIcon(new javax.swing.ImageIcon("E:\Image2_60.png")); // NOI18N
powerBtn.setBorderPainted(false);
powerBtn.setContentAreaFilled(false);
powerBtn.setFocusPainted(false);
powerBtn.setPressedIcon(new javax.swing.ImageIcon("E:\Image2_50.png")); // NOI18N
powerBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
powerBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = -31;
gridBagConstraints.ipady = -8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(106, 27, 0, 0);
jPanel1.add(powerBtn, gridBagConstraints);
presetBtn.setIcon(new javax.swing.ImageIcon("E:\Image2_60.png")); // NOI18N
presetBtn.setBorderPainted(false);
presetBtn.setContentAreaFilled(false);
presetBtn.setFocusPainted(false);
presetBtn.setPressedIcon(new javax.swing.ImageIcon("E:\Image2_50.png")); // NOI18N
presetBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
presetBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = -31;
gridBagConstraints.ipady = -8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(106, 26, 0, 19);
jPanel1.add(presetBtn, gridBagConstraints);
aboutUsBtn.setIcon(new javax.swing.ImageIcon("E:\Image2_60.png")); // NOI18N
aboutUsBtn.setBorderPainted(false);
aboutUsBtn.setContentAreaFilled(false);
aboutUsBtn.setFocusPainted(false);
aboutUsBtn.setPressedIcon(new javax.swing.ImageIcon("E:\Image2_50.png")); // NOI18N
aboutUsBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutUsBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = -31;
gridBagConstraints.ipady = -8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(42, 57, 32, 0);
jPanel1.add(aboutUsBtn, gridBagConstraints);
settingBtn.setIcon(new javax.swing.ImageIcon("E:\Image2_60.png")); // NOI18N
settingBtn.setBorderPainted(false);
settingBtn.setContentAreaFilled(false);
settingBtn.setFocusPainted(false);
settingBtn.setPressedIcon(new javax.swing.ImageIcon("E:\Image2_50.png")); // NOI18N
settingBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
settingBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = -31;
gridBagConstraints.ipady = -8;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(42, 27, 32, 0);
jPanel1.add(settingBtn, gridBagConstraints);
}
private void powerBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void presetBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void aboutUsBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void settingBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* Wraps a Swing JComponent in a background image. Simply invokes the overloded
* variant with Top/Leading alignment for background image.
*
* @param component - to wrap in the a background image
* @param backgroundIcon - the background image (Icon)
* @return the wrapping JPanel
*/
public static JPanel wrapInBackgroundImage(JComponent component,
Icon backgroundIcon) {
return wrapInBackgroundImage(
component,
backgroundIcon,
JLabel.TOP,
JLabel.CENTER);
}
/**
* Wraps a Swing JComponent in a background image. The vertical and horizontal
* alignment of background image can be specified using the alignment
* contants from JLabel.
*
* @param component - to wrap in the a background image
* @param backgroundIcon - the background image (Icon)
* @param verticalAlignment - vertical alignment. See contants in JLabel.
* @param horizontalAlignment - horizontal alignment. See contants in JLabel.
* @return the wrapping JPanel
*/
public static JPanel wrapInBackgroundImage(JComponent component,
Icon backgroundIcon,
int verticalAlignment,
int horizontalAlignment) {
// make the passed in swing component transparent
component.setOpaque(false);
// create wrapper JPanel
JPanel backgroundPanel = new JPanel(new GridBagLayout());
backgroundPanel.setBackground(new java.awt.Color(255, 255, 255));
// add the passed in swing component first to ensure that it is in front
backgroundPanel.add(component, gbc);
// create a label to paint the background image
JLabel backgroundImage = new JLabel(backgroundIcon);
// set minimum and preferred sizes so that the size of the image
// does not affect the layout size
backgroundImage.setPreferredSize(new Dimension(1, 1));
backgroundImage.setMinimumSize(new Dimension(1, 1));
// align the image as specified.
backgroundImage.setVerticalAlignment(verticalAlignment);
backgroundImage.setHorizontalAlignment(horizontalAlignment);
// add the background label
backgroundPanel.add(backgroundImage, gbc);
// return the wrapper
return backgroundPanel;
}
}
回答by christopher
回答by mKorbel
How to put JButton on JLabel (which contain Image) in Java Swing?
如何在 Java Swing 中将 JButton 放在 JLabel(包含图像)上?
JLabel and rest of JComponents haven't implemented any LayoutManager, required to set LayoutManager
every Swing JComponents (most of AWT Components too) can be contianers too
excluding JFrame/ContentPane - BorderLayout and JPanel - FlowLayout
JLabel 和其余 JComponents 尚未实现任何 LayoutManager,需要设置 LayoutManager
每个 Swing JComponents(大多数 AWT 组件也是如此)也可以是容器
不包括 JFrame/ContentPane - BorderLayout 和 JPanel - FlowLayout