java - 将JFrame ImageIcon发送到Frame的后面

标签 java swing jframe jlabel

我正在进行项目的最后部分,但在将导入的图像“ship.png”发送到后面时遇到问题。

我在类(class)内设置了单选按钮,我希望能够在导入的 Ship.png 顶部看到这些单选按钮。

这是我迄今为止尝试过的方法,但没有成功。我导入的图像似乎总是位于单选按钮前面和/或将它们全部删除。

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 类中设置单选按钮的位置:

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 将忽略其子容器指定的值,仅根据 icon 计算 preferredSize JLabel 本身的 text 属性

关于java - 将JFrame ImageIcon发送到Frame的后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33469140/

相关文章:

java - java中的paint()没有显示

java - 更改 JBorderLayout 内 JTextField 的大小

Java JFrame 项目位置/大小

java - 如何在 Java 中绑定(bind) hstore[] 值

java - 设置按钮和 Action 监听器的大小

Java 从另一个类获取选定的组合框

java - 如何在调用另一个 JFrame 时删除一个 JFrame

JavaFx 启动应用程序并继续

java - 代码优化建议

java - 如何在使用 hibernate search/lucene 建立索引期间去除空格和特殊字符