2016-10-10 16 views
-1

プログラムの画像(現在のコード)にリンクするとプログラムが動作します。それをURL( "http://www.digitalphotoartistry.com/rose1.jpg")で置き換えると、プログラムは画像が表示されずに実行されます。私は運がないと多くのバリエーションを試しました。誰もそれが動作していない理由を見ることができますか?画像はファイルディレクトリで表示されますが、URLは表示されません

public class ImageViewer extends JFrame { 

public ImageViewer() { 
    //create panel of actions 
    JPanel actionPanel = new JPanel(); 
    actionPanel.setLayout(new GridLayout(1, 4)); 

    actionPanel.add(new JButton("Prev")); 
    actionPanel.add(new JButton("Add")); 
    actionPanel.add(new JButton("Del")); 
    actionPanel.add(new JButton("Next")); 


    //Create panel to hold pictures 
    JLabel label= new JLabel(new ImageIcon("C:/Users/Madison/Desktop/capture.png"), JLabel.CENTER); 
    JPanel imagePanel = new JPanel(new BorderLayout()); 
    imagePanel.add(label, BorderLayout.CENTER); 

    //Add contents to frame 
    add(imagePanel, BorderLayout.NORTH); 
    add(actionPanel, BorderLayout.SOUTH); 
} 

public static void main (String args []){ 
    ImageViewer frame = new ImageViewer(); 
    frame.setTitle("Title"); 
    frame.setSize(1000, 500); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 

}

答えて

1

それに応じてコードを編集していますか? パスをURLに置き換えることはできません。

これを試してみてください:

URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg"); //set url 
ImageIcon image = new ImageIcon(ImageIO.read(url)); //read image and create ImageIcon 
JLabel label = new JLabel(image, JLabel.CENTER);  

IOMalformedURL例外をチェックすることを忘れないでください。

+1

私はこれに新しいので、私はそれを知らなかった。ありがとうございました! –

関連する問題