2017-04-01 34 views
0

我想製作一個小圖形用戶界面。它有三個複選框和一個按鈕。 (下圖)我有一個頭像的圖像,我希望其他圖像放在頂部。這種方式,當我選擇他們,並按下按鈕,他們將改變。目前在我的處理程序中,我只有一個ImageIcon。 當我運行代碼時,「coolBrows.png」圖像不顯示。我在這裏看到了一些添加JFrame到JFrame的例子,他們說我需要添加一個佈局來讓它工作。我已經做到了,它仍然不會出現...任何幫助表示讚賞! 注意:每張圖片都有相同的尺寸,但覆蓋的圖片會有透明背景。如何添加JLabel並將其移除到JPane?

對不起,如果我違反了我從未在此發佈過的任何約定。

的GUI

enter image description here

package AssignmentFace; 

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 


public class AssignmentFace extends JFrame { 

    private JPanel contentPane; 
    private ImageIcon head; 
    private JCheckBox checkBoxBrows; 
    private static ImageIcon brows; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        AssignmentFace frame = new AssignmentFace(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
    /** 
    * Create the frame. 
    */ 
    public AssignmentFace() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(1,2)); 
     setContentPane(contentPane); 

     JPanel selectionPanel = new JPanel(); 
     selectionPanel.setLayout(new GridLayout(7,1)); 
     contentPane.add(selectionPanel, BorderLayout.WEST); 

     // Contains the checkboxes 
     checkBoxBrows= new JCheckBox("brows"); 
     selectionPanel.add(checkBoxBrows); 

     JCheckBox checkBoxNose = new JCheckBox("Nose"); 
     selectionPanel.add(checkBoxNose); 

     JCheckBox checkBoxMouth = new JCheckBox("Mouth"); 
     selectionPanel.add(checkBoxMouth); 

     Handler handler = new Handler(); 

     JButton btnCycle = new JButton("Cycle ..."); 
     btnCycle.addActionListener(handler); 
     selectionPanel.add(btnCycle); 

     JPanel facePanel = new JPanel(); 
     facePanel.setOpaque(true); 
     contentPane.add(facePanel, BorderLayout.CENTER); 

     // Head 
     head = new ImageIcon(getClass().getResource("/images/head.png")); 
     JLabel headLabel = new JLabel(head, JLabel.CENTER); 
     headLabel.setLayout(new BorderLayout()); 
     facePanel.add(headLabel, BorderLayout.CENTER); 

     // Brows 
     brows = new ImageIcon(); 
     JLabel browsLabel = new JLabel(brows, JLabel.CENTER); 
     headLabel.add(browsLabel, BorderLayout.CENTER); 

     // Nose 
     //ImageIcon nose = new ImageIcon(); 
     //JLabel noseLabel = new JLabel(nose, JLabel.CENTER); 
     //facePanel.add(noseLabel, BorderLayout.CENTER); 

     // Mouth 
     //ImageIcon mouth = new ImageIcon(); 
     //JLabel mouthLabel = new JLabel(nose, JLabel.CENTER); 
     //facePanel.add(mouthLabel, BorderLayout.CENTER); 
    } 

    public class Handler implements ActionListener 
    { 

     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      if(checkBoxBrows.isSelected()) 
      { 
       brows = new ImageIcon(getClass().getResource("/images/coolBrows.png")); 
       System.out.println("here"); //For testing purposes. This does come through in the console 
      } 
     } 
    } 
} 
+0

好吧,所以你明顯知道如何將組件添加到容器,刪除它們幾乎是相同的過程,但調用'remove'。當在屏幕上實現的容器上執行此操作時,您需要調用'revalidate'並且通常在容器上調用'repaint'來生成佈局和繪製過程。在你的情況下,我想知道你是否應該用新映像更新已有的'JLabel'(即'setIcon') – MadProgrammer

+0

謝謝你使用setIcon(),它的工作方式就像一個魅力。 –

回答

1

所以好像你需要層IconsJLabels

因此,也許你可以使用:

  1. 一個JLayeredPane到層標籤。因此,您爲每個面部特徵創建一個圖層,併爲圖層添加一個標籤。然後,您可以根據複選框的值添加/刪除標籤上的圖標。請閱讀Swing教程How to Use Layered Panes中的部分以獲取更多信息和工作示例。

  2. 使用Compound Icon對圖標進行分層。這使您可以將圖標堆疊在一起,然後在一個JLabel上顯示結果Icon。因此,每次通過單擊複選框生成事件時,都可以使用所選功能重建CompoundIcon,並在JLabel上重置Icon

+0

感謝您的反饋。我看着JLayeredPane,看起來這是爲了做我正在嘗試的東西。我將重寫這個與層練習。 –