1

コンポーネントが本来あるべき場所にカーソルを合わせたときにのみコンポーネントが描画される理由を誰かが説明できますか?

どこにでもドラッグできるフチなしフレームを設定し、右上に終了ボタンを作成しようとしていますが、カーソルを合わせるまで描画されません。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);
    }
}
4

2 に答える 2

3
  1. 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プログラミング言語のコード規則を読み通したいと思うかもしれません。それらを無視して友達を作ることはありません;)

于 2012-11-25T21:56:33.893 に答える
1

よく覚えていれば、完全にロードされていない可能性のあるをToolKit.getImage返します。Imageカーソルを合わせると、その間にバックグラウンドで読み込まれている可能性があります。代わりにこれを行います(背景の行と同様):

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

(理解を深めるためMediaTrackerに、画像が完全に読み込まれていることを確認するために使用されたと思われるを検索することをお勧めします。)

于 2012-11-25T22:09:30.073 に答える