简体   繁体   English

JButton()只在鼠标悬停时才有效

[英]JButton() only working when mouse hovers

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

The Fullscreen is another class which gives a full screen Frame. 全屏是另一个提供全屏帧的类。 JButton have ImageIcon on them. JButton上有ImageIcon。 bg1.png is a background image Problem is these JButton become visible only when mouse hovers else they do not appear. bg1.png是一个背景图像问题是这些JButton只有在鼠标悬停时才会显示,否则它们不会出现。

add Icon/ImageIcon directly to the JButton instead of paint() for AWT or paintComponent() for Swing JComponents Icon / ImageIcon直接添加到JButton而不是用于AWT的paint()或用于Swing JComponents的 paintComponent()

Contructor JButton(Icon) knows Icon or ImageIcon Contructor JButton(Icon)知道Icon或ImageIcon

在此输入图像描述

from code 来自代码

import java.awt.*;
import javax.swing.*;

public class ButtonsIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon =  UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon =  UIManager.getIcon("OptionPane.warningIcon");

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonsIcon t = new ButtonsIcon();
            }
        });
    }

    public ButtonsIcon() {
        setLayout(new GridLayout(0, 2, 4, 4));

        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon((errorIcon));
        button.setRolloverIcon((infoIcon));
        button.setPressedIcon(warnIcon);
        button.setDisabledIcon(warnIcon);
        add(button);

        JButton button1 = new JButton();
        button1.setBorderPainted(false);
        button1.setBorder(null);
        button1.setFocusable(false);
        button1.setMargin(new Insets(0, 0, 0, 0));
        button1.setContentAreaFilled(false);
        button1.setIcon((errorIcon));
        button1.setRolloverIcon((infoIcon));
        button1.setPressedIcon(warnIcon);
        button1.setDisabledIcon(warnIcon);
        add(button1);
        button1.setEnabled(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}

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. 你可能有一个布局问题,你试图将带有绝对边界的JButton添加到一个使用非空布局管理器的容器中。 Suggestions 建议

  • Do not use setBounds and absolute positioning to size and place your components. 不要使用setBounds和绝对定位来调整组件的大小和位置。
  • 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 添加所有组件后,不要忘记在JFrame上调用pack()
  • Call setVisible(true) after calling pack() and again call both only after adding all components to your GUI. 调用pack() setVisible(true)后调用setVisible(true) ,并在将所有组件添加到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. 如果您绝对需要使用组件的绝对定位,则可以使用null布局,但无论如何,您应该努力避免使用它。

It looks like you're never repainting after you add your buttons. 在添加按钮后,您似乎永远不会重新绘制。

I would add a repaint in there after you add them. 添加后我会在那里添加一个重绘。

Just had a similar problem... 刚遇到类似问题......

I believe that the glitch is caused by overriding the paint() method. 我相信这个故障是由覆盖paint()方法引起的。 Default paint() method automatically calls repaint() on all component, but by overriding the paint() method, components stop being repainted. 默认的paint()方法自动调用所有组件上的repaint(),但是通过覆盖paint()方法,组件不再被重新绘制。 So, the solution is to call repaint() on all components int the overriden paint() method. 因此,解决方案是在覆盖paint()方法的所有组件上调用repaint()。

Worked for me, hope it'll work for others ;).. 为我工作,希望它能为他人工作;)..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM