2016-01-13 46 views
2

我必須創建一個程序,我將在一個對話框中向用戶顯示一些選項以供選擇。 根據用戶選擇的選項,我必須在另一個以前爲空的對話框中顯示該圖片。想要添加圖片到JPanel點擊按鈕

實施例:

  • 兩個對話框 「一個」 和 「2」 是對用戶是可見的。對話框「one」上顯示了許多按鈕。對話框「two」爲空。
  • 用戶點擊對話框「one」上的按鈕A,然後我必須在對話框「two」上顯示該圖片。
  • 用戶在對話框「one」上點擊不同的按鈕B,然後我必須在對話框「two」和舊圖片上顯示該圖片。

這可以動態地完成,而無需創建新的對話框「two」或不創建對話框「two」的新JPanel。

到目前爲止,我已經創建了下面的程序,但它並沒有在它運行後添加圖片。

import java.awt.BorderLayout; 
import java.awt.Dialog.ModalityType; 
import java.awt.FlowLayout; 
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.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16; 


public class Launcher { 

    JDialog keyboardDialog; 
    JDialog nameViewDialog; 
    JPanel nameViewJPanel; 
    JDialog FinalNameViewDialog; 

    private final transient ActionListener keyButtonListener = 
     new ActionListener() { 
      @Override public void actionPerformed(final ActionEvent event) { 
       System.out.println(((JButton) event.getSource()).getActionCommand()); 
       String buttonType=((JButton) event.getSource()).getActionCommand(); 
       ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG")); 
       JLabel la=new JLabel(iconA); 
       nameViewJPanel.add(la); 
       nameViewJPanel.repaint(); 
      } 
     }; 

    public Launcher() 
    { 
     nameViewDialog=new JDialog(); 
     nameViewDialog.setLayout(new BorderLayout()); 
     nameViewJPanel=new JPanel(); 
     nameViewJPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); 
     nameViewDialog.setSize(430, 490); 

     ImageIcon iconA1 = new ImageIcon(this.getClass().getResource("\\Icons\\A1.PNG")); 
     JLabel la=new JLabel(iconA1); 
     nameViewJPanel.add(la); 
     ImageIcon iconA2 = new ImageIcon(this.getClass().getResource("\\Icons\\B1.PNG")); 
     JLabel lb=new JLabel(iconA2); 
     nameViewJPanel.add(lb); 
     nameViewDialog.add(nameViewJPanel); 

     keyboardDialog=new JDialog(nameViewDialog,ModalityType.MODELESS); 
     keyboardDialog.setLocationRelativeTo(nameViewDialog); 

     keyboardDialog.setSize(230,190); 
     keyboardDialog.setLayout(new GridLayout(2,3)); 
     ImageIcon iconA = new ImageIcon(this.getClass().getResource("\\JaLetters\\A.PNG")); 
     ImageIcon iconB = new ImageIcon(this.getClass().getResource("\\JaLetters\\B.PNG")); 
     ImageIcon iconC = new ImageIcon(this.getClass().getResource("\\JaLetters\\C.PNG")); 
     ImageIcon iconD = new ImageIcon(this.getClass().getResource("\\JaLetters\\D.PNG")); 
     ImageIcon iconE = new ImageIcon(this.getClass().getResource("\\JaLetters\\E.PNG")); 
     ImageIcon iconF = new ImageIcon(this.getClass().getResource("\\JaLetters\\F.PNG")); 

     JButton ba=new JButton(); 
     ba.setIcon(iconA); 
     ba.setActionCommand("A"); 
     ba.addActionListener(keyButtonListener); 

     JButton bb=new JButton(); 
     bb.setIcon(iconB); 
     bb.setActionCommand("B"); 
     bb.addActionListener(keyButtonListener); 

     JButton bc=new JButton(); 
     bc.setIcon(iconC); 
     bc.setActionCommand("C"); 
     bc.addActionListener(keyButtonListener); 

     JButton bd=new JButton(); 
     bd.setIcon(iconD); 
     bd.setActionCommand("D"); 
     bd.addActionListener(keyButtonListener); 

     JButton be=new JButton(); 
     be.setIcon(iconE); 
     be.setActionCommand("E"); 
     be.addActionListener(keyButtonListener); 

     JButton bf=new JButton(); 
     bf.setIcon(iconF); 
     bf.setActionCommand("F"); 
     bf.addActionListener(keyButtonListener); 

     keyboardDialog.add(ba); 
     keyboardDialog.add(bb); 
     keyboardDialog.add(bc); 
     keyboardDialog.add(bd); 
     keyboardDialog.add(be); 
     keyboardDialog.add(bf); 

     nameViewDialog.setVisible(true); 
     keyboardDialog.setVisible(true); 


    } 


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

} 
+1

這尖叫聲模型和[觀察者模式](HTTP:// WWW。 oodesign.com/observer-pattern.html)。我會猶豫是否使用兩個對話框來做到這一點,但是可以選擇使用JSplitPane中的面板或使用第二個對話框作爲彈出窗口來收集信息,當關閉時允許第一個窗口自動更新 – MadProgrammer

回答

2

我在對話框中添加了一個CustomJPanel兩個。每個actionlistener加載一個不同的圖像並將其發送到繪製圖像的CustomJPanel。

這裏是它MVC:

主類:

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JDialog; 

public class Main { 

    public static void main(String[] args) { 

     JDialog dialog = new JDialog(); 
     dialog.setSize(600, 400); 
     dialog.setLocationRelativeTo(null); 
     dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     dialog.setVisible(true); 

     CustomJPanel customJDialog = new CustomJPanel(); 
     dialog.add(customJDialog); 

     JDialog dialog2 = new JDialog(); 
     dialog2.setLayout(new FlowLayout()); 
     dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     dialog2.setLocationRelativeTo(dialog); 

     JButton button1 = new JButton("Image 1"); 
     JButton button2 = new JButton("Image 2"); 

     dialog2.add(button1); 
     dialog2.add(button2); 

     dialog2.pack(); 
     dialog2.setVisible(true); 

     button1.addActionListener(new ActionListener() { 

      BufferedImage image = null; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        image = ImageIO.read(getClass().getResource("test1.jpg")); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
       // TODO Auto-generated method stub 
       customJDialog.setImage(image); 
      } 
     }); 

     button2.addActionListener(new ActionListener() { 

      BufferedImage image = null; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        image = ImageIO.read(getClass().getResource("test2.jpg")); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
       // TODO Auto-generated method stub 
       customJDialog.setImage(image); 
      } 
     }); 
    } 

} 

CustomJPanel

import java.awt.Graphics; 
import java.awt.image.BufferedImage; 

import javax.swing.JPanel; 

public class CustomJPanel extends JPanel { 

    BufferedImage image = null; 

    public CustomJPanel() { 

    } 

    @Override 
    public void paintComponent(Graphics g) { 
     // TODO Auto-generated method stub 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this); 
     System.out.println(image); 
    } 

    public void setImage(BufferedImage image) { 
     this.image = image; 
     repaint(); 
    } 
} 
+0

is there你知道哪些機制可以用新圖片重新繪製窗口,但仍舊保留在CustomPanel上的舊圖片。你的答案適用於我,但我需要在屏幕上保留舊圖片 – Onki

+0

我可以改變你的程序,將所有圖片存儲在列表中,並且每次使用列表通過循環繪製所有圖片來重新繪製窗口。但我正在尋找是否有其他有效的方法可用,我不需要繪製所有圖片,而只應繪製最後一張圖片和較老的圖片。 – Onki

+0

@HimJEL我想不出其他的方式,只是爲列表中的每個圖像添加CustomJPanel,並添加它們並從對話框中刪除。 (你也可以用JLabel等其他組件來做到這一點,不需要成爲JPanel)但是我想這不適合你,因爲你說過這個問題。你只限於什麼樣的優化? – brainiac080195