JButton()只在鼠标悬停时才有效 [英] JButton() only working when mouse hovers

查看:551
本文介绍了JButton()只在鼠标悬停时才有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.lang.*;
    import java.io.*;
    import javax.swing.*;
    public class MainClass extends Component{
       private Image bg;
       private ImageIcon newgame;
       private ImageIcon quit;
       private ImageIcon options;
       private JButton bquit;
       private JButton boptions;
       private JButton bnewgame;
       private static Container pane; //Container

    public void loadImage() {
        try {
            bg=ImageIO.read(new File("bg1.png"));
        } catch (Exception e) {
        }
        if(bg!=null)
            repaint();

    }
    public void paint(Graphics g) {
        g.drawImage(bg,0,0,null);
    }
    public void imageButtons(JFrame f) {
        try {
            quit= new ImageIcon("quit.png");
            options=new ImageIcon("options.png");
            newgame= new ImageIcon("newgame.png");
        }catch(Exception e){}    
        bnewgame= new JButton(newgame);
        boptions= new JButton(options);
        bquit= new JButton(quit);
        bnewgame.setBounds(150,100,400,89);
        boptions.setBounds(150,200,400,89);
        bquit.setBounds(150,300,400,89);
        pane.add(bquit);
        pane.add(boptions);
        pane.add(bnewgame);
    }   


    public static void main(String args[]) {

        MainClass o=new MainClass();    
        FullScreen fs=new FullScreen(); 
        JFrame f1=new JFrame("TITLE");
        pane=f1.getContentPane();
        fs.fullScreenIt(f1);
        pane.add(o);
        f1.setVisible(true);
        o.loadImage();
        o.imageButtons(f1);
    }
}

全屏是另一个提供全屏帧的类。
JButton上有ImageIcon。 bg1.png是背景图片
问题这些JButton只有在鼠标悬停时才会显示,否则它们不会出现。

The Fullscreen is another class which gives a full screen Frame. JButton have ImageIcon on them. bg1.png is a background image Problem is these JButton become visible only when mouse hovers else they do not appear.

推荐答案

可能你有一个布局问题,你试图将带有绝对边界的JButton添加到一个使用非空布局管理器的容器中。建议

Likely you have a layout problem where you're trying to add JButtons with absolute bounds into a container that uses a non-null layout manager. Suggestions


  • 不要使用setBounds和绝对定位来调整和放置组件。

  • 阅读并使用布局管理器为您做这件繁重的工作:课程:在容器中布置组件

  • 不要忘记调用 pack()

  • 调用 pack后调用 setVisible(true) ()并在将所有组件添加到GUI后再次调用

  • 空布局如果您绝对需要使用组件的绝对定位,则可以使用,但无论如何,您应该尽量避免使用它。

  • Do not use setBounds and absolute positioning to size and place your components.
  • Read up on and use the layout managers to do this heavy lifting for you: Lesson: Laying Out Components Within a Container
  • Don't forget to call pack() on your JFrame after adding all components
  • Call setVisible(true) after calling pack() and again call both only after adding all components to your GUI.
  • A null layout is available if you absolutely need to use absolute positioning of components, but regardless, you should strive to avoid using it.

这篇关于JButton()只在鼠标悬停时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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