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

全屏是另一個提供全屏幀的類。 JButton上有ImageIcon。 bg1.png是一個背景圖像問題是這些JButton只有在鼠標懸停時才會顯示,否則它們不會出現。

Icon / ImageIcon直接添加到JButton而不是用於AWT的paint()或用於Swing JComponents的 paintComponent()

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

你可能有一個布局問題,你試圖將帶有絕對邊界的JButton添加到一個使用非空布局管理器的容器中。 建議

  • 不要使用setBounds和絕對定位來調整組件的大小和位置。
  • 閱讀並使用布局管理器為您完成繁重的工作: 課程:在容器中布置組件
  • 添加所有組件后,不要忘記在JFrame上調用pack()
  • 調用pack() setVisible(true)后調用setVisible(true) ,並在將所有組件添加到GUI后再次調用它們
  • 如果您絕對需要使用組件的絕對定位,則可以使用null布局,但無論如何,您應該努力避免使用它。

在添加按鈕后,您似乎永遠不會重新繪制。

添加后我會在那里添加一個重繪。

剛遇到類似問題......

我相信這個故障是由覆蓋paint()方法引起的。 默認的paint()方法自動調用所有組件上的repaint(),但是通過覆蓋paint()方法,組件不再被重新繪制。 因此,解決方案是在覆蓋paint()方法的所有組件上調用repaint()。

為我工作,希望它能為他人工作;)..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM