【问题标题】:Putting button on top of image in JPanel?将按钮放在JPanel中的图像顶部?
【发布时间】:2013-02-28 09:14:15
【问题描述】:

我一直在处理主菜单屏幕。我使用卡片布局,因为我有一个显示在主菜单屏幕之前的闪屏。一旦用户单击初始屏幕上的“继续”按钮,他们就会被带到主菜单屏幕。

我无法添加屏幕截图,因为我没有足够高的声誉,但现在按钮被推到一边,我猜是因为卡片布局。

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

这是我的窗口代码:

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

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

【问题讨论】:

  • 您可以在屏幕截图中添加链接,以便其他人可以编辑您的帖子并为您插入屏幕截图。
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing user-interface layout jpanel


【解决方案1】:
JPanel card1 = new JPanel();
card1.add(new SplashScreen());
card1.add(continueButton);

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

您希望组件相互叠加,因此您需要执行以下操作:

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

【讨论】:

  • 那么您将如何在初始屏幕上定位按钮?你的想法很完美。截至目前,按钮位于 JPanel 的上部中心。 @camickr
  • 您可以使用适当的布局管理器来定位按钮。
【解决方案2】:

您的 JPanel 包含您的 2 个 menuButtonMenuScreen 没有布局。使用GridBagLayout 之类的并将按钮设置在顶部。您可以使用其他布局,这取决于您。

我使用按钮仅向您展示GridBagLayout 的简单示例,如果您想使用它,建议您查看Oracle's page on that layout

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

JButton menuButton3 = new JButton("UNDER THE 2 OTHERS");

GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.weightx = 1;
gbc2.weighty = 1;
gbc2.gridwidth = 2;
gbc2.fill = GridBagConstraints.HORIZONTAL;

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;

GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 1;
gbc3.gridy = 0;
gbc3.weightx = 1;
gbc3.weighty = 1;
gbc3.fill = GridBagConstraints.HORIZONTAL;
card2.add(menuButton1, gbc);
card2.add(menuButton2, gbc3);
card2.add(menuButton3, gbc2);

【讨论】:

    【解决方案3】:

    如果您的初始屏幕只有一个按钮,您可以尝试将整个初始图像设为一个按钮,如下例所示:Java: using an image as a button

    您可以编辑现有的初始屏幕图像以在图像本身内添加一个继续按钮。当然,用户可以点击任意位置(因为它是一个巨大的图像按钮)。

    听起来很俗气,我知道。但这可能是一种快速而肮脏的方式来足够接近你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2021-12-23
      • 2011-02-12
      • 2013-05-25
      • 2015-10-26
      • 2012-10-11
      • 1970-01-01
      相关资源
      最近更新 更多