java - 在Java中使用JLabel创建背景后无法将其他组件添加到窗口

标签 java swing

我想创建一个像小部件一样的java应用程序。下面是我的代码

package newpackage;


public class MainFrame extends JFrame {
    JLabel imageLabel = new JLabel();

    public MainFrame() {
        try {
            this.setUndecorated(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(new Dimension(360, 360));
            ImageIcon ii = new ImageIcon(this.getClass().getResource("imageexcel.gif"));
            imageLabel.setIcon(ii);
            add(imageLabel, java.awt.BorderLayout.CENTER);
            this.setVisible(true);

            Shape shape=new Ellipse2D.Float(0,0,360,360);
            AWTUtilities.setWindowShape(this, shape);
            AWTUtilities.setWindowOpaque(this, false);

            imageLabel.add(new JButton("START"));

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }

}

在上面的代码中,我做了以下事情:

  1. 创建了一个框架
  2. 删除了标题栏
  3. 使用 JLabel 添加背景
  4. 根据图像形状将窗口形状改为圆形

现在我想向其中添加一些组件并用它们执行一些操作,但添加后没有任何组件可见。

我尝试添加到 Frame 和 JLabel,但都没有用。

This is the image i used for background 请帮助我继续前进......

谢谢

最佳答案

JLabels 默认情况下使用空布局,因此您的按钮将默认大小为 0,0。尝试给它一个像样的布局管理器,甚至 FlowLayout 也可能起作用。另一种解决方案是保留空布局并设置添加组件的大小和位置,但这条路线是一条危险的路线,我不推荐。

实际上,GridBagLayout 可以很好地使组件居中。还要在调用setVisible(true)之前添加所有组件:

imageLabel.setLayout(new GridBagLayout());
this.setUndecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(360, 360));
ImageIcon ii = new ImageIcon(this.getClass().getResource("imageexcel.gif"));
imageLabel.setIcon(ii);
add(imageLabel, java.awt.BorderLayout.CENTER);
imageLabel.add(new JButton("START"));
this.setVisible(true);

或者更好?

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Shape;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.net.URL;
import javax.swing.*;
import com.sun.awt.AWTUtilities;

@SuppressWarnings("serial")
public class MainPanelOvalFrame extends JPanel {
    private static final String RESOURCE_PATH = "imageexcel.gif";
    private Window window;
    private Image img;

    public MainPanelOvalFrame(Window window, Image image) {
        this.window = window;
        this.img = image;

        setLayout(new GridBagLayout());
        add(new JButton(new StartAction("Start", KeyEvent.VK_S)));

        int w = image.getWidth(this);
        int h = image.getHeight(this);
        Shape shape = new Ellipse2D.Float(0, 0, w, h);
        AWTUtilities.setWindowShape(window, shape);
        AWTUtilities.setWindowOpaque(window, false);
    }

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

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet() || img == null) {
            return super.getPreferredSize();
        }
        int w = img.getWidth(this);
        int h = img.getHeight(this);
        return new Dimension(w, h);
    }

    private class StartAction extends AbstractAction {
        public StartAction(String name, int mnemonic) {
            super(name);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            window.dispose();
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        URL imgUrl = MainPanelOvalFrame.class.getResource(RESOURCE_PATH);
        Image image = new ImageIcon(imgUrl).getImage();
        MainPanelOvalFrame mainPanel = new MainPanelOvalFrame(frame, image);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

关于java - 在Java中使用JLabel创建背景后无法将其他组件添加到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33615250/

相关文章:

java - 为什么我的 JLabels 和 JTextFields 没有出现在 JPanel 中?

java - 扩展 JTable 并在 JFrame 中显示

Python pool.map/Multiprocessing 的 Java 等价物

java - 访问 URL 时在 Tomcat 7 上按 IP 地址范围阻止访问

java - 在一框中添加多个不同尺寸的面板

java - JTable 行编辑器

java - 在 swing 项目中使用 JavaFx 应用程序

java - 使用 Java 重放 post 请求

java - 找不到符号方法 setAnimationListener

java - 如何在 Java 中创建一个简单的状态机