簡體   English   中英

組件僅在懸停時繪制

[英]Components Only get drawn when hovered over

有人可以解釋為什么我的組件只有在我將它們懸停在它們應該的位置時才會被繪制出來嗎?

我設置了一個無邊框,而不是可以在任何地方拖動,我正在嘗試在右上角創建一個退出按鈕但是在我將鼠標懸停在它上面之前它不會被繪制。 我在JFrame上繪制一個背景圖像,然后繪制我的按鈕並將整個事物設置為可見。

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GUI extends JFrame
{
    private Image Background = null;
    private static Point Offset = new Point();

    public GUI() {
        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        AddListeners();
        SetCustomTheme();
        LoadBackground();
        Layout();
        pack();
        this.setSize(300, 300);
        this.setVisible(true);
    }

    private void Layout() {
        GroupLayout Info = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(Info);
        JButton Button = new JButton();

        Info.setHorizontalGroup(
            Info.createSequentialGroup()
               .addComponent(Button)
         );

        Info.setVerticalGroup(
            Info.createParallelGroup()
                .addComponent(Button)
        );
    }

    private void SetCustomTheme() {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        }
    }

    private void LoadBackground() {
        try {
            Background = ImageIO.read(getClass().getResource("Images/meh.png"));
        } catch (Exception Ex) {

        }
    }

    private void SetCustomIcon() {
        Image Icon = Toolkit.getDefaultToolkit().getImage("Images/lol.jpg");
        setIconImage(Icon);
    }

    private void AddListeners() {
        this.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) {
              Offset.x = e.getX();
              Offset.y = e.getY();
            }
          });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override public void mouseDragged(MouseEvent e) {
              Point p = getLocation();
              setLocation(p.x + e.getX() - Offset.x, p.y + e.getY() - Offset.y);
            }
          });
    }

    @Override public void paint(Graphics g) {
        g.drawImage(Background, 0,0,this.getWidth(),this.getHeight(), null);
    }
}
  1. 必須在Event Dispatching Thread中執行與UI的所有交互
  2. 您應該避免從頂級容器(如JFrame擴展,而是使用JPanel
  3. 未能遵守paint鏈合同是防止任何兒童組件開始繪畫
  4. 覆蓋執行自定義繪制的首選方法是paintComponent

您可能希望閱讀

嘗試這樣的事情;

public class BadPaint01 {

    public static void main(String[] args) {
        new BadPaint01();
    }

    public BadPaint01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                Image Icon = Toolkit.getDefaultToolkit().getImage("Images/lol.jpg");
                frame.setIconImage(Icon);
                frame.setUndecorated(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new GUI());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class GUI extends JPanel {

        private Image Background = null;
        private static Point Offset = new Point();

        public GUI() {
            AddListeners();
            SetCustomTheme();
            LoadBackground();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }

        private void Layout() {
            GroupLayout Info = new GroupLayout(this);
            setLayout(Info);
            JButton Button = new JButton();

            Info.setHorizontalGroup(
                    Info.createSequentialGroup()
                    .addComponent(Button));

            Info.setVerticalGroup(
                    Info.createParallelGroup()
                    .addComponent(Button));
        }

        private void SetCustomTheme() {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }
        }

        private void LoadBackground() {
            try {
                Background = ImageIO.read(getClass().getResource("Images/meh.png"));
            } catch (Exception Ex) {
            }
        }

        private void AddListeners() {
            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    Offset.x = e.getX();
                    Offset.y = e.getY();
                }
            });

            this.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    Point p = getLocation();
                    setLocation(p.x + e.getX() - Offset.x, p.y + e.getY() - Offset.y);
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            g.drawImage(Background, 0, 0, this.getWidth(), this.getHeight(), null);
        }
    }
}

您也可能希望閱讀Java編程語言的代碼約定,而不是通過忽略它們來建立任何朋友;)

如果我記得很清楚, ToolKit.getImage返回一個可能無法完全加載的Image 當您將鼠標懸停在它上面時,它可能已同時加載到后台。 而是這樣做(類似於你的背景線):

ImageIcon Icon = new ImageIcon(ImageIO.read(getClass().getResource("Images/lol.png")));
setIconImage(Icon);

(為了更好地理解,您可能希望搜索MediaTracker ,我相信這是用來確保圖像完全加載的。)

暫無
暫無

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

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