java - 添加到包含缓冲图像的 JLabel 时不会出现按钮
问题描述
所以我有一个缓冲图像加载到JLabel
.
当我启动程序时,只显示图像,但不显示按钮。
我尝试将JButtons
直接添加到 中JLabel
,结果相同。但是,如果我将buttonPanel
直接添加到框架中,按钮最终是可见的。但是,如果我将JLabel
直接添加到框架然后添加buttonPanel
,则只有按钮可见。
public void setProp(){
//frame properties
mainFrame.setSize(1200, 845); //JFrame
mainFrame.setVisible(false);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//layout properties
buttonLayout.setHgap(100); //GridLayout
buttonLayout.setColumns(1);
buttonLayout.setRows(6);
//panel properties
buttonPanel.setLayout(buttonLayout);
buttonPanel.setVisible(true);
buttonPanel.setOpaque(false);
//button properties
newGame.setOpaque(false);
newGame.setContentAreaFilled(false);
newGame.setBorderPainted(false);
newGame.setForeground(Color.WHITE);
newGame.setFont(buttonFont);
newGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//launch designated method
}
});
loadGame.setOpaque(false);
loadGame.setContentAreaFilled(false);
loadGame.setBorderPainted(false);
loadGame.setForeground(Color.WHITE);
loadGame.setFont(buttonFont);
loadGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//launch designated method
}
});
credits.setOpaque(false);
credits.setContentAreaFilled(false);
credits.setBorderPainted(false);
credits.setForeground(Color.WHITE);
credits.setFont(buttonFont);
credits.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//launch designated method
}
});
exit.setOpaque(false);
exit.setContentAreaFilled(false);
exit.setBorderPainted(false);
exit.setForeground(Color.WHITE);
exit.setFont(buttonFont);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
//background properties
background = importPic("D:/programming rescources/strategy game/resources/images/mmpic.JPG");
//adding components to mainFrame
buttonPanel.add(newGame); //adding all buttons onto JPanel with GridLayout
buttonPanel.add(loadGame);
buttonPanel.add(credits);
buttonPanel.add(exit);
background.add(buttonPanel);//this is the JLabel with the buffered image
mainFrame.add(background);
}
解决方案
好吧,我解决了这个问题。我通过将 BorderLayout 添加到背景(JLabel),然后将背景添加到 JFrame 来做到这一点。
推荐阅读
- powershell - Anyway to split files to diff folder?
- c# - WPF,经常图片刷新不闪烁
- java - 错误:包不存在 JUnit 命令行 - 没有 IDE/包管理器
- google-kubernetes-engine - 子域的 gke-security-groups
- matlab - 我将如何创建一个查找表,其中包含用于增加/减少给定数量的图像亮度的传递函数
- spring-boot - Keycloak门面服务
- c# - 会话未结束时停止页面刷新 ASP.NET MVC C#
- java - 在将 pdf 文件复制到另一个文件时添加注释
- angular - Angular 库如何使用 .tgz 文件打包依赖项
- jquery - 多个选择字段将ajax数据作为字符串传递