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

Har*_*dhu 2 java swing mouseevent jbutton null-layout-manager

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

mKo*_*bel 6

添加图标/ ImageIcon的直接到JButton的,而不是paint()用于AWT或paintComponent()用于摇摆JComponents

Contructor JButton(Icon)知道Icon或ImageIcon

在此输入图像描述

来自代码

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Hov*_*els 5

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

  • 不要使用 setBounds 和绝对定位来调整和放置组件。
  • 继续阅读并使用布局管理器为您完成这项繁重的工作:课程:在容器内布置组件
  • pack()添加所有组件后不要忘记调用JFrame
  • 呼叫setVisible(true)通话后pack(),再次呼吁双方只有将所有的组件到GUI之后。
  • 如果您绝对需要使用组件的绝对定位,则可以使用空布局,但无论如何,您应该尽量避免使用它。