将 JFrame ImageIcon 发送到 Frame 的后面

Send JFrame ImageIcon to the back of Frame

我正处于项目的最后部分,我无法将导入的图像 "ship.png" 发送到后面。

我在我的 class 中设置了单选按钮,我希望能够在我导入的 ship.png 之上看到这些单选按钮。

这是我迄今为止尝试过但无济于事的方法。我导入的图像似乎总是在单选按钮前面 and/or 一起摆脱它们。

import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Project2_ScottHogan
{
  public static void main(String[] args) throws IOException
  {
    String path = "D:/CSCI_1301/Project2/ship.png";
    File file = new File(path);
    BufferedImage shiplayout = ImageIO.read(file);
    JLabel label = new JLabel(new ImageIcon(shiplayout));

    JFrame main_frame = new JFrame("Welcome to Murracruise: The #1 cruise-line in the nation!"); //Sets a title for the JFrame
    main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the JFrame when exiting

    Cabin_Choice choice = new Cabin_Choice("", "", "", "", "", "", "", 0.00); //Calls upon the constructor of my Cabin_Choice class
    main_frame.getContentPane().add(choice); //Places choice into the current JFrame pane
    main_frame.setContentPane(label);

    main_frame.pack(); //Sizes the frame
    main_frame.setVisible(true); //Allows us to see the frame





  } //Ends the main method
} //Ends the class

这里是我在 Cabin_Choice class:

中设置单选按钮的地方
description = new JLabel("Choose your Cabin or Suite"); //Creates the label
description.setFont(new Font("Helonia", Font.BOLD, 28)); //Changes the font to Helonia and boldens the text for description

cabin1 = new JRadioButton("Cabin 11-1");
cabin2 = new JRadioButton("Cabin 11-2");
cabin3 = new JRadioButton("Cabin 11-3");
cabin4 = new JRadioButton("Cabin 11-4");
cabin5 = new JRadioButton("Cabin 11-5");
cabin6 = new JRadioButton("Cabin 11-6");
cabin7 = new JRadioButton("Cabin 11-7");
cabin8 = new JRadioButton("Cabin 11-8");
cabin9 = new JRadioButton("Cabin 11-9");
cabin10 = new JRadioButton("Cabin 11-10");
suite1 = new JRadioButton("Suite 11-S1");
suite2 = new JRadioButton("Suite 11-S2");

cabin1.setForeground(Color.white);
cabin2.setForeground(Color.white);
cabin3.setForeground(Color.white);
cabin4.setForeground(Color.white);
cabin5.setForeground(Color.white);
cabin6.setForeground(Color.white);
cabin7.setForeground(Color.white);
cabin8.setForeground(Color.white);
cabin9.setForeground(Color.white);
cabin10.setForeground(Color.white);
suite1.setForeground(Color.white);
suite2.setForeground(Color.white);

cabin1.setBackground(new Color(31, 21, 202)); //This sets the background to my own custom color of blue
cabin2.setBackground(new Color(31, 21, 202)); 
cabin3.setBackground(new Color(31, 21, 202));
cabin4.setBackground(new Color(31, 21, 202));
cabin5.setBackground(new Color(31, 21, 202));
cabin6.setBackground(new Color(31, 21, 202));
cabin7.setBackground(new Color(31, 21, 202));
cabin8.setBackground(new Color(31, 21, 202));
cabin9.setBackground(new Color(31, 21, 202));
cabin10.setBackground(new Color(31, 21, 202));
suite1.setBackground(new Color(31, 21, 202)); //This sets the background for the suites to a custom purple color
suite2.setBackground(new Color(31, 21, 202)); //Custom purple color for the background of suite 2

//The following block of code puts the radio buttons into a group
//so that the user can only select 1 cabin or suite at a time
ButtonGroup group = new ButtonGroup();
group.add(cabin1);
group.add(cabin2);
group.add(cabin3);
group.add(cabin4);
group.add(cabin5);
group.add(cabin6);
group.add(cabin7);
group.add(cabin8);
group.add(cabin9);
group.add(cabin10);
group.add(suite1);
group.add(suite2);

//
Cabin_Listener cabinlisten = new Cabin_Listener();
cabin1.addActionListener(cabinlisten);
cabin2.addActionListener(cabinlisten);
cabin3.addActionListener(cabinlisten);
cabin4.addActionListener(cabinlisten);
cabin5.addActionListener(cabinlisten);
cabin6.addActionListener(cabinlisten);
cabin7.addActionListener(cabinlisten);
cabin8.addActionListener(cabinlisten);
cabin9.addActionListener(cabinlisten);
cabin10.addActionListener(cabinlisten);
suite1.addActionListener(cabinlisten);
suite2.addActionListener(cabinlisten);

//
add(description);
add(cabin1);
add(cabin2);
add(cabin3);
add(cabin4);
add(cabin5);
add(cabin6);
add(cabin7);
add(cabin8);
add(cabin9);
add(cabin10);
add(suite1);
add(suite2);

//
setBackground(Color.white); //Sets the background for the entire frame
setPreferredSize(new Dimension(700,100)); //Sets the default size of the frame
  1. 在向框架添加任何新内容之前调用 setContent
  2. 设置 contentPane 的布局管理器(类似于 BorderLayout),因为 JLabel 默认没有设置布局管理器
  3. 使用 setOpaque(false) 之类的东西将 Cabin_Choice 设置为透明,否则你将看不到背景图像

例如...

main_frame.setContentPane(label);
main_frame.setLayout(new BorderLayout());
main_frame.getContentPane().add(choice); //Places choice into the current JFrame pane

此外,通常 avoid using setPreferred/Minimum/MaximumSize,但在这种情况下,它将没有任何效果,因为 JLabel 将忽略其子容器指定的值,仅计算基于 preferredSize 的值在 JLabel 本身的 icontext 属性上