【问题标题】:JButton Only Works When Visibility is Set to True-JavaJButton 仅在可见性设置为 True-Java 时有效
【发布时间】:2020-04-19 17:10:00
【问题描述】:

我基本上正在尝试制作一个简单的跳棋游戏,我需要用户只能看到瓷砖和棋子,而不是按钮。当我将可见性设置为 True 时,程序通过给我一条测试消息“嘿,按下了一个按钮!”来工作。但是,当我将可见性设置为 False(我需要它)时,我不再收到测试消息。我从一般的谷歌搜索中看到的唯一与此相关的论坛问题是使用重绘和重新验证,但这些都不起作用,因此我删除了那两行代码。我通常会有一个很好用的按钮类,但由于我的代码只接受静态而不是正常的,我必须直接在我的主类中实现 jbutton。那么究竟有什么问题呢?这是我的代码,提前致谢。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.*;

@SuppressWarnings("serial")
public class CheckersMain extends JButton implements ActionListener {

    private static JFrame window;
    private static Color winBackground=Color.GRAY;
    private static Color tile1Color=Color.WHITE;
    private static Color tile2Color=Color.BLACK;
    private static int windowWidth=1000;
    private static int windowHeight=1000;
    private static int setScreenLoc=500;
    private static int tileDimention=100;
    private static Board board;
    private static ArrayList<JButton> allButtons=new ArrayList<JButton>();
    private static ArrayList<Tile> allTiles;

    public static void main(String[] args) {
        window=new JFrame();
        window.setLayout(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Checkers");
        window.setLocation(setScreenLoc,setScreenLoc);
        window.setSize(windowWidth,windowHeight);
        window.setResizable(false);
        window.getContentPane().setBackground(winBackground);
        window.setVisible(true);
        board=new Board(window,tileDimention,tile1Color,tile2Color);
        allTiles=board.setUp();
        setUpButtons();
        window.repaint();
    }

    private static void setUpButtons() {
        for (int i=0;i<allTiles.size();++i) {
            Tile currentTile=allTiles.get(i);
            JButton button=new JButton();
            button.setSize(tileDimention,tileDimention);
            button.setLocation(currentTile.getXlocation(),currentTile.getYlocation());
            window.add(button,0);
            button.addActionListener(new CheckersMain());
            button.setVisible(false);
            allButtons.add(button);
        }
    }

    private void buttonPressed() {
        System.out.println("Hey a button was pressed!");
    }
    public void actionPerformed(ActionEvent frame) {
        for (int i=0;i<allButtons.size();++i) {
            if (frame.getSource()==allButtons.get(i)) {
                buttonPressed();
            }
        }
    }
}

【问题讨论】:

  • 如果按钮是不可见的(即隐藏),您为什么希望得到消息?用户如何点击它?
  • 按钮确实在幕后,用户只是看不到它。用户会在根本不知道的情况下点击。
  • 按钮确实在幕后 这不是 Java 中“可见”的意思。在 Java 中,如果组件不可见,则不会绘制;如果它没有画出来,它真的是在那里。
  • 好的,可见性有点像一个按钮(不是双关语),您可以在其中打开和关闭它。所以我的解决方案是首先设置按钮,当然可见性设置为 true,然后继续设置板?
  • "visibility" 确定按钮是否显示在 UI 上。如果它被隐藏,则无法与之交互,并且在某些情况下布局管理器将忽略它们并且布局将相应更改

标签: java swing jbutton visibility static-methods


【解决方案1】:

这听起来像是使用 Glass Pane 的好地方: https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

玻璃窗格不可见,但仍接收鼠标移动和单击事件。在玻璃窗格上添加一个单击侦听器并获取鼠标位置,然后检查该位置是否在您的“隐藏”按钮所在的空间之上。

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 2015-07-11
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多