问题描述
我想在框架中添加背景图像,但是当我在框架中添加图像时,它已成功添加,但是在框架后缀上添加的j标签和按钮等其他内容未显示在框架上.我已将图像保留在桌面上.下面是我的代码
I want to add a background image to my frame.But when i add an image to my frame it was added successfully but other things like j label and buttons added on frame afterwords don't appear on frame. i have kept image on Desktop. below is my code
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
中,将其替换为
In the EditTeamInterface
's constructor, you set the content pane for the frame using setContentPane(contentPane);
, but in the static void main
, you replace it with
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg")))));
这会丢弃您之前为该新内容所做的所有操作...
This discards everything you did before for this new content...
更改从JPanel
延伸的EditTeamInterface
,而不是从JFrame
延伸的EditTeamInterface
Instead of EditTeamInterface
extending from JFrame
, change it so that it extends from JPanel
创建JFrame
的新实例,将内容窗格设置为标签,然后将EditTeamInterface
添加到框架中.
Create a new instance of a JFrame
, set the content pane to your label and then add EditTeamInterface
to the frame.
由于默认情况下JLabel
没有布局管理器,因此在将其内容窗格设置为标签后,应该在框架上调用setLayout(new BorderLayout())
Because JLabel
has no layout manager by default, you should call setLayout(new BorderLayout())
on the frame after you set it's content pane to the label
虽然使用JLabel
可以使用这种方式,但JLabel
不会根据内容的需要来计算其首选大小,而是根据icon
和text
的属性来计算.这实际上并不适合该任务.
While, using a JLabel
, this way will work, JLabel
will NOT calculate it's preferred size based on the needs of it's contents, but rather based on the properties of the icon
and text
. This doesn't really make it suitable for this task.
更好的解决方案是使用自定义JPanel
并覆盖它的paintComponent
来呈现所需的图像.如果您很聪明,则可以做到这一点,以便此类可以重用.
A better solution is to use a custom JPanel
and override it's paintComponent
to render the image you want. If you're clever, you will make it so that this class is re-usable.
例如类似
这篇关于JFrame背景图像设置背景图像时,其他按钮没有出现在框架上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!