我想添加一個背景圖像到我的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);
}
}
圖像大小被設置爲我的筆記本電腦的屏幕的尺寸是1337水平由670個垂直像素 – 2015-02-10 04:32:08