将按钮放在JPanel中的图像顶部? [英] Putting button on top of image in JPanel?

查看:151
本文介绍了将按钮放在JPanel中的图像顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在主菜单屏幕上工作.我使用卡片布局是因为我在主菜单屏幕之前显示了一个初始屏幕.一旦用户单击了初始屏幕上的继续"按钮,他们就会被带到主菜单屏幕.

I have been working on a main menu screen. I use a card layout because I have a splash screen that shows up before the main menu screen. Once the user clicks the "Continue" button on the splash screen, they are brought to the main menu screen.

我无法添加屏幕截图,因为我的信誉不高,但是由于卡的布局,我认为目前为止这些按钮被推到了侧面.

I can't add a screenshot because I don't have a high enough reputation but as of now the buttons are pushed off to the side, I assume because of the card layout.

有什么方法可以将按钮放置在主菜单屏幕图像的顶部?

Is there any way that I can place the button on top of the main menu screen image?

这是我的窗口代码:

package edu.ycp.cs.Main;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Window {
    JPanel cards; // a panel that uses CardLayout
    final static String SPLASHSCREEN = "SplashScreen";
    final static String MAINMENU = "MainMenu";

    public void addComponentToWindow(Container pane) {
        // Put the JComboBox in a JPanel to get a nicer look.
        JPanel gameWindow = new JPanel(); // use FlowLayout

        // Create the "cards".
        JPanel card1 = new JPanel();
        JButton continueButton = new JButton("Continue");
        continueButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) (cards.getLayout());
                cl.show(cards, MAINMENU);
            }
        });
        card1.add(new SplashScreen());
        card1.add(continueButton);

        JPanel card2 = new JPanel();
        JButton menuButton1 = new JButton("PLAY!");
        JButton menuButton2 = new JButton("HIGH SCORES");
        card2.add(new MainMenuScreen());
        card2.add(menuButton1);
        card2.add(menuButton2);

        cards = new JPanel(new CardLayout());
        cards.add(card1, SPLASHSCREEN);
        cards.add(card2, MAINMENU);

        pane.add(gameWindow, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }
}

关于如何使按钮位于图像顶部的任何建议?

Any suggestions on how to get the button on top of the image?

推荐答案

JPanel card1 = new JPanel();
card1.add(new SplashScreen());
card1.add(continueButton);

JPanel使用FlowLayout.因此,当您向其中添加两个组件时,它们只是彼此并排绘制.

A JPanel use a FlowLayout. So when you add two components to it they are just painted beside each other.

您希望组件在彼此之间绘制,因此您需要执行以下操作:

You want the component to be painted on top of each other so you need to do something like:

JPanel card1 = new JPanel();
SplashScreen splash = new SplashScreen();
splash.setLayout( new FlowLayout() );
card1. add(splash);
splash.add( continueButton );

这篇关于将按钮放在JPanel中的图像顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆