2016-06-13 115 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」);'應用程序資源在部署時將成爲嵌入式資源,所以現在就開始訪問它們是明智的做法。 [tag:embedded-resource]必須通過URL而不是文件訪問。請參閱[信息。頁面爲嵌入式資源](http://stackoverflow.com/tags/embedded-resource/info)如何形成的URL。 –

+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(); 
    } 
} 

和picture1.jpg是在我有PhotoAlbum.java相同的地方。

否則你的代碼工作正常。儘管大圖像顯示不正確。

+0

這應該在'{'和'}'之間的if(actionCommand.equals(「Image 1」))中?我嘗試過使用這個,但它將背景更改爲黃色,但不添加圖像。 – blaze077

+0

@ blaze077你可以放一個斷點並檢查url是否爲空。您的代碼正在爲我工​​作並顯示圖像。 – Beniton

+0

我不熟悉斷點。我知道如何添加它們,但我不知道如何檢查url是否爲null。 – blaze077