簡體   English   中英

JFrame沒有顯示圖片

[英]JFrame not showing a picture

以下是我到目前為止的代碼:所有導入都是正確的。 我確定。 :d

當我運行程序時,我得到的只是一個空白幀,沒有圖片。 它應該顯示出來。

public class WindowPractice extends JFrame {

   final static int width= 800;
   final static int height= 400;
   int x;
   int y;
   Image steve;
   Dimension gamesize= new Dimension (width, height);
    public WindowPractice(){
        setTitle ("Hangman");
        setSize (gamesize);
        setVisible (true);
        setResizable (false);
        setLocationRelativeTo (null);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);


    }
    public static void main (String[] args) {
        new WindowPractice();
        ImageIcon steve= new ImageIcon ("Libraries/Pictures/ba190cd951302bcebdf216239e156a4.jpg");
        JLabel imageLabel = new JLabel(steve);

    }
    public void paint(Graphics g){

        g.setColor(Color.red);
        //g.fillRect(  x, y, 100, 20);
        g.drawImage(steve, x, y,this);


        x= 150;
        y= 250;
    }

}

這有很多問題,我不確定從哪里開始...

讓我們從頭開始...

問題1

您可以在WindowPractice類中聲明一個名為steve的實例字段,這很好,但是在您的主方法中,您聲明了一個名為steve ANOTHER變量,您將使用對已加載圖像的引用。

public static void main(String[] args) {
    new WindowPractice();
    ImageIcon steve = new ImageIcon("C:/Users/shane/Dropbox/issue459.jpg");
    JLabel imageLabel = new JLabel(steve);
}

這意味着該類實例變量永遠不會初始化,並且保持為null

問題二

雖然沒有直接關系,但是您永遠不會從paint方法中調用super.paint 這是一個很大的不,不。 您有義務維護油漆鏈。 繪制方法很復雜,而且非常重要。

問題三

您永遠不要覆蓋頂級容器(例如JFrame ),也不要覆蓋它的任何paint方法。 造成這種情況的原因很多,但是在最上面的兩個容器中,大多數頂級容器實際上包含許多組件( JRootPane ,用於容納玻璃窗格,內容窗格,層窗格和菜單欄),這些組件可以覆蓋繪畫工作並且,通常,它們不是雙重緩沖的,這意味着您繪制的更新會閃爍並看起來很可怕;)

您還應該避免使用paint ,而是應該在可用的地方使用paintComponent

問題四

ImageIcon不是加載圖像的最佳選擇。 我不使用它們的主要原因是,您不知道何時加載圖像會真正可用(實際上有很多方法,但坦率地說, ImageIO更為簡單)。 這是1999年的一項重要功能,當時撥號速度約為14.4k,但現在...

ImageIO支持更廣泛的圖片格式,支持圖像的讀取和寫入,並保證在方法成功返回時,圖像像素數據可用於您的應用程序。

這是一種更好的(IMHO)方法...

在此處輸入圖片說明

public class BetterDrawing {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private BufferedImage background;

        public PaintPane() {
            try {
                background = ImageIO.read(new File("/path/to/image"));
                // Use this instead to load embedded resources instead
                //background = ImageIO.read(getClass().getResource("/path/to/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (background != null) {

                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;

                g.drawImage(background, x, y, this);

            }

        }
    }
}

閱讀

有關更多信息。

暫無
暫無

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

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