Java - Swing库 - JLabel顶部的JComboBox,带有图像 - 隐形JLabel

时间:2018-01-28 01:40:15

标签: java swing jlabel jcombobox invisible

这是我在这里发表的第一篇文章,试试看。 我有一个带JLabel的小JFrame窗口,图像作为JFrame的背景。 JFrame还有2个JButtons。到目前为止它运作良好,直到我决定添加JComboBox。此操作的结果现在是当我运行我的JFrame时它显示空白,没有来自JLabel的背景图像,没有可见的ComboBox,显示了JButton。当我调整大小这个窗口时,JLabel背景图像出现,一切都很好,但它应该没有调整大小。我在这里错过了什么?我非常喜欢挥杆,我正在做#34;游戏"对于我的java类项目。这是截图和代码:

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);




    }
   }

1 个答案:

答案 0 :(得分:2)

这里有几个问题:

  • 您正在使用空布局,这是一件危险而又奇怪的事情
  • 您在之前在JFrame 上调用了setVisible(true),您已经完全构建了它。只在添加所有组件后调用此方法。
  • 您正在使用JLabel作为背景图像显示器,但不能将其用作容纳组件的容器。

而是考虑

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

这样的事情:

enter image description here

可以使用以下代码创建

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);
    }
}