JFrame背景图背景图像设置时,其他按钮不显示在图像上的图像?
问题描述:
我想添加一个背景图像到我的frame.But当我添加一个图像到我的框架它已成功添加,但其他东西,如j标签和按钮添加到框架afterwords不会出现在框架上。我在桌面上保留了图像。下面是我的代码JFrame背景图背景图像设置时,其他按钮不显示在图像上的图像?
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);
框架中的内容窗格中,但在static void main
,你
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg")))));
更换这会放弃你做的一切之前对于这个新的内容...
而不是EditTeamInterface
从JFrame
延伸,改变它,使它从JPanel
创建JFrame
的新实例,将内容窗格设置为您的标签,然后将EditTeamInterface
添加到框架。
因为JLabel
没有默认的布局管理器,你应该叫setLayout(new BorderLayout())
在框架上后,你设置它的内容窗格标签
虽然使用JLabel
,这样一来将工作,JLabel
将不计算它的首选大小是基于其内容的需求,而是基于icon
和text
的属性。这并不能真正适合这项任务。
更好的解决方案是使用自定义JPanel
并覆盖它的paintComponent
来渲染所需的图像。如果你聪明,你会做到这一点,以便这个班级可以重复使用。
喜欢的东西this for example
图像大小被设置为我的笔记本电脑的屏幕的尺寸是1337水平由670个垂直像素 – 2015-02-10 04:32:08