Java - Swing 库 - JComboBox 在带有图像的 JLabel 之上 - 不可见的 JLabel

Java - Swing library - JComboBox on top of JLabel with image - invisible JLabel

这是我第一次 post 来这里,快点。 我有一个带有 JLabel 的小 JFrame window,图像作为 JFrame 的背景。 JFrame 也有 2 个 JButton。到目前为止它运行良好,直到我决定添加 JComboBox。现在,此操作的结果是当我 运行 我的 JFrame 显示空白时,没有来自 JLabel 的背景图像,没有可见的 ComboBox,但显示了 JButton。当我稍微调整这个 window 的大小时,JLabel 背景图像出现,一切都很好,但它应该没有调整大小。我在这里错过了什么?我对 Swing 非常熟悉,正在为我的 java class 项目做 "game"。这是截图和代码:

Running

After resizing

public class View_Startup extends JFrame {
    JLabel lBackground;
    JButton bStart,bExit;
    JComboBox cbResolutions;
    ImageIcon iBackground,iStart,iExit;
    Image icon;
    String resolutions[] = {"1280x720 HD","1366x768 WXGA","1600x900HD+","1920x1080 fullHD"};

    public View_Startup() {

        iBackground = new ImageIcon("xdddddddddd/resolution_background.jpg");
        iStart = new ImageIcon("xddddddddddd/iStart2.png");
        iExit = new ImageIcon("xdddddddddd/iExit2.png");    

        this.setSize(656,399);
        this.setTitle("xddddddddd");
        this.icon = Toolkit.getDefaultToolkit().getImage("C:xdddddddddd\images.jpg"); 
        this.setIconImage(icon);
        this.setVisible(true);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
        Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();   
        int x=(int) rect.getMaxX();
        int y=(int) rect.getMaxY();
        this.setLocation(x/2-328,y/2-199);

        bStart = new JButton(iStart);
        bStart.setBounds(490,240,150,50);
        bStart.setOpaque(false);
        bStart.setContentAreaFilled(false);
        //bStart.setBorderPainted(false);
        add(bStart);

        bExit = new JButton(iExit);
        bExit.setBounds(490,300,150,50);
        bExit.setOpaque(false);
        bExit.setContentAreaFilled(false);
        //bExit.setBorderPainted(false);
        add(bExit);

        cbResolutions = new JComboBox(resolutions);
        cbResolutions.setBounds(490,180,150,50);
        add(cbResolutions);

        lBackground = new JLabel(iBackground);
        lBackground.setBounds(0,0,640,360);
        add(lBackground);




    }
   }

这里有几个问题:

  • 您正在使用空布局,这是一件危险而笨拙的事情
  • 您在 JFrame 上调用 setVisible(true) 之前 您已完全构建它。只有在添加所有组件后才调用它。
  • 您将 JLabel 用作背景图像显示器,但并未将其用作容纳组件的容器。

改为考虑

  • 使用JPanel的paintComponent方法显示背景图片
  • 将此 JPanel 添加到 JFrame 的 contentPane 或使其成为 contentPane
  • 给它一个体面的布局(如果需要,添加嵌套的非不透明 JPanel,如果您需要复杂的布局,每个 JPanel 都有自己的布局)
  • 在添加所有组件之后在您的 JFrame 上调用 .setVisible(true)

像这样:

可以使用此代码创建:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class LayoutExample extends JPanel {
    private static final long serialVersionUID = 1L;
    private BufferedImage background;
    private JButton startButton = new JButton("Start");
    private JButton exitButton = new JButton("Exit");
    private JComboBox<String> combo = new JComboBox<>(new String[] {"1280 x 780 HD"});

    public LayoutExample(BufferedImage background) {
        this.background = background;

        JPanel rightLowerPanel = new JPanel(new GridLayout(0, 1, 5, 5));
        rightLowerPanel.setOpaque(false);
        rightLowerPanel.add(combo);
        rightLowerPanel.add(startButton);
        rightLowerPanel.add(exitButton);

        JPanel rightPanel = new JPanel(new BorderLayout());
        rightPanel.setOpaque(false);
        rightPanel.add(rightLowerPanel, BorderLayout.PAGE_END);

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout());
        add(rightPanel, BorderLayout.LINE_END);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (background != null) {
            g.drawImage(background, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (background != null) {
            int w = background.getWidth();
            int h = background.getHeight();
            return new Dimension(w, h);
        }
        return super.getPreferredSize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        String imgPath = "https://pbs.twimg.com/media/DRHUe_tV4AA96G4.jpg";
        BufferedImage img = null;
        try {
            URL imageUrl = new URL(imgPath);
            img = ImageIO.read(imageUrl);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        LayoutExample mainPanel = new LayoutExample(img);
        JFrame frame = new JFrame("LayoutExample");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}