2016-06-13 12 views
0

タイトルが示唆するように、私はクリックしたときに画像を表示するボタンがいくつかあるプログラムを作成しようとしています。 しかし、これは、hereのようにグラフィッククラスを使用せずに、コンテナをグローバルにすることなく可能かどうかを知りたいです。 これを試みましたが、私のプログラムはパネルに画像を追加していないようです。アンドリューは彼のコメントで示唆したようにJButtonをクリックして画像を表示

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.*; 

public class PhotoAlbum extends JFrame implements ActionListener{ 

private JPanel imagePanel; 
private JPanel labelPanel; 
public PhotoAlbum(){ 
    super(); 
    Container contentPane = getContentPane(); 
    setSize(1800, 1000); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    setTitle("Button Demo"); //Theme here 
    contentPane.setBackground(Color.blue); 
    contentPane.setLayout(new BorderLayout()); 

    createButtons(contentPane); 
    instruction(contentPane); 
    createImageLabel(contentPane); 
} 

public void instruction(Container contentPane){ 
    JPanel instruction = new JPanel(); 
    instruction.setLayout(new FlowLayout()); 
    instruction.setBackground(Color.yellow); 
    Font fontType1 = new Font("Comic Sans MS", Font.BOLD, 40); 
    JLabel instruction1 = new JLabel("Click on the button to view a" 
      + " photo."); 
    instruction1.setForeground(Color.BLUE); 
    instruction1.setFont(fontType1); 
    instruction.add(instruction1); 
    contentPane.add(instruction, BorderLayout.SOUTH); 
} 

public void createButtons(Container contentPane){ 
    labelPanel = new JPanel(); 
    labelPanel.setBackground(Color.pink); 
    labelPanel.setLayout(new GridLayout(9,1)); 

    String[] imageLabel = new String[9]; 
    imageLabel[0] = "Image 1"; 
    imageLabel[1] = "Image 2"; 
    imageLabel[2] = "Image 3"; 
    imageLabel[3] = "Image 4"; 
    imageLabel[4] = "Image 5"; 
    imageLabel[5] = "Image 6"; 
    imageLabel[6] = "Image 7"; 
    imageLabel[7] = "Image 8"; 
    imageLabel[8] = "Exit"; 

    Color[] color = new Color[9]; 
    color[0] = Color.cyan; 
    color[1] = new Color(242, 121, 234); 
    color[2] = Color.red; 
    color[3] = Color.green; 
    color[4] = Color.blue; 
    color[5] = new Color(1, 255, 248); 
    color[6] = Color.magenta; 
    color[7] = new Color(205, 255, 1); 
    color[8] = new Color(205, 255, 1); 

    Font fontType = new Font("Times New Roman", Font.BOLD, 30); 


    JButton[] button = new JButton[9]; 
    for (int i=0; i<button.length; i++) 
    { 
     button[i] = new JButton(imageLabel[i]); 
     button[i].addActionListener(this); 
     button[i].setBackground(color[i]); 
     button[i].setFont(fontType); 
     labelPanel.add(button[i]); 
    } 

    contentPane.add(labelPanel, BorderLayout.WEST); 
} 

public void createImageLabel(Container contentPane){ 
    imagePanel = new JPanel(); 
    imagePanel.setBackground(Color.magenta); 
    contentPane.add(imagePanel, BorderLayout.CENTER); 
} 

public void actionPerformed(ActionEvent event){ 
    String actionCommand = event.getActionCommand(); 
    if(actionCommand.equals("Image 1")) { 
     JLabel addImage = new JLabel(); 
     ImageIcon image = new ImageIcon("picture1.jpg"); 
     addImage.setIcon(image); 
     imagePanel.add(addImage); 
     imagePanel.setBackground(Color.yellow); 
    } 

    else if(actionCommand.equals("Exit")) 
     System.exit(0); 
    else System.out.println("Error in button interface."); 

} 

public static void main(String[] args) 
{ 
    PhotoAlbum buttonGui= new PhotoAlbum(); 
    buttonGui.setVisible(true); 
} 


} 
+2

実行されたアクションメソッドでラベルを作成して追加するよりも、(GUIの構築時に)行うほうが簡単です。テキストまたはイメージのないラベルは、ユーザーには見えません。ボタンをクリックすると、既存のラベルのアイコンを設定します。 –

+1

'new ImageIcon(" picture1.jpg ");'アプリケーションリソースは、デプロイ時に埋め込まれたリソースになるので、今のようにアクセスすることをお勧めします。 [タグ:埋め込みリソース]は、ファイルではなくURLでアクセスする必要があります。 [info。埋め込みリソースのページ](http://stackoverflow.com/tags/embedded-resource/info)を参照してください。 –

+0

「GUIが構築されたとき」とは、コードの冒頭にありますか?また、私はボタンをクリックしたときに画像が表示されないことに気がついたときに、ボタンを押してウィンドウを最大化/最小化すると画像が表示されることに気付いたばかりです。それが私の問題と関係があるかどうかは分かりません。 – blaze077

答えて

0

:ここ

はコードです。以下のようにコードをリファクタリングしてください。

if(actionCommand.equals("Image 1")) { 
    JLabel addImage = new JLabel(); 
    URL url = getClass().getResource("picture1.jpg"); 
    if (url != null) { 
     ImageIcon image = new ImageIcon(url); 
     addImage.setIcon(image); 
     imagePanel.add(addImage); 
     imagePanel.setBackground(Color.yellow); 
     this.revalidate(); 
    } 
} 

Photo1.jpgはPhotoAlbum.javaと同じ場所にあります。

これ以外の場合は、コードが正常に動作しています。大きな画像は正しく表示されません。

+0

これは、 '{'と '}'の間の 'if(actionCommand.equals(" Image 1 "))'にあるはずですか?私はこれを使用してみましたが、背景を黄色に変更しますが、画像は追加しません。 – blaze077

+0

@ blaze077ブレークポイントを設定し、URLがnullでないかどうかを確認できますか?あなたのコードは私のために働いており、画像を表示しています。 – Beniton

+0

私はブレークポイントに精通していません。私はそれらを追加する方法を知っているが、私はURLがnullかどうかをチェックする方法がわからない。 – blaze077

関連する問題