我想向我的框架添加背景图像。但是当我向框架添加图像时,它已成功添加,但框架后缀上添加的 j 标签和按钮等其他内容不会出现在框架上。我已将图像保留在桌面上。下面是我的代码
package UI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Color;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Insets;
public class EditTeamInterface extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EditTeamInterface frame = new EditTeamInterface();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//frame.getContentPane().setBackground(Color.white);
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg")))));
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public EditTeamInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 624, 356);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblNewLabel = new JLabel("EDIT OPTIONS");
lblNewLabel.setFont(new Font("SketchFlow Print", Font.BOLD, 18));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
contentPane.add(lblNewLabel, gbc_lblNewLabel);
JButton btnNewButton = new JButton("New button");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.gridx = 5;
gbc_btnNewButton.gridy = 5;
contentPane.add(btnNewButton, gbc_btnNewButton);
}
}
最佳答案
在 EditTeamInterface
的构造函数中,您使用 setContentPane(contentPane);
设置框架的内容 Pane ,但在 static void main
中,您将其替换为
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg")))));
这将放弃您之前为此新内容所做的所有操作...
不要从 JFrame
扩展 EditTeamInterface
,而是将其更改为从 JPanel
扩展
创建 JFrame
的新实例,将内容 Pane 设置为标签,然后将 EditTeamInterface
添加到框架。
由于 JLabel
默认情况下没有布局管理器,因此在将其内容 Pane 设置为标签后,应在框架上调用 setLayout(new BorderLayout())
虽然使用JLabel
,这种方式可行,但JLabel
不会根据其内容的需要来计算其首选尺寸,而是根据icon
和text
的属性来计算。这并不真正使它适合这项任务。
更好的解决方案是使用自定义的 JPanel
并覆盖它的 paintComponent
来渲染您想要的图像。如果您很聪明,您就会使该类可以重用。
类似于 this for example
关于java - JFrame背景图像设置背景图像时,其他按钮不会出现在框架上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424095/