【问题标题】:JButton is only appearing on MouseEnterJButton 仅出现在 MouseEnter
【发布时间】:2014-08-03 21:01:49
【问题描述】:

JButton 只出现在 MouseEnter 我已经尝试了多种方法,但我似乎无法弄清楚如何让我的 JButton 覆盖背景图形。 .有人知道我如何在这个背景上绘画并让我的按钮留在那里吗?我会在这里粘贴代码,但它会有点乱,所以这里有一些 pastebin 链接! :)

我的主班:http://pastebin.com/DvVfCU03

我的 MenuPanel 类:http://pastebin.com/uht8cabX

这是主类的副本,以防万一:

public class Main {

final static JFrame window = new JFrame();
final static ImageIcon imageIconOne = new ImageIcon(getConnectImage());
final static JButton connectB = new JButton(imageIconOne);
public static void main(String[] args) throws IOException {
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setContentPane(new MenuPanel());
    window.addKeyListener(new MenuPanel());
    window.setMinimumSize(new Dimension(1024,640));
    window.pack();
    window.setTitle("Realm of Pixels");
    window.setVisible(true);
    window.addMouseListener(new MouseHandler());
    window.setVisible(true);
    window.add(connectB);
     connectB.setVisible(true);
}

public static Image getConnectImage(){
    Image connectImage = null;
    try {
        connectImage = ImageIO.read(new File("Resources/Buttons/Connect.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return connectImage;
}
public static JFrame getWindow() {
    return window;
}
}

【问题讨论】:

    标签: java swing graphics jframe jbutton


    【解决方案1】:

    最后调用JFrame 中的setVisible,在构建UI 之后,在调用setVisible 之前调用pack,这样你就可以得到更好的尺寸计算基于内容。

    请注意,KeyListener 众所周知,它仅在其注册的组件具有焦点且具有焦点时才响应击键,通常最好使用键绑定 API。此外,如果组件层次结构中较高的任何组件也具有MouseListeners

    ,则可以忽略直接向窗口添加MouseListener

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 2015-01-01
      相关资源
      最近更新 更多