2016-08-19 54 views
0

我写了两种方式来显示图像时,JPanel数组绑定,但它不显示图像。单击JPanel数组时显示图像

这是我的代码:

public class Mutil_Image_2 implements MouseListener { 

    public JPanel [][]sub=new JPanel[10][10]; 
    public JPanel screen = new JPanel(); 

    public JFrame f = new JFrame("Draw on Panel"); 
    public static int v1,v2; 
    public static int x1 =1,y1=1; 

    public Mutil_Image_2(String title) 
    { 

     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setExtendedState(JFrame.MAXIMIZED_BOTH); //Full Screen 

     screen.setBorder(BorderFactory.createLineBorder(Color.black)); 
     screen.setLayout(new GridLayout(10,10));  

     int dem =0; 
     for (int i=0; i<=9;i++) 
     { 
      for (int j=0; j<=9;j++) 
      {     
       sub[i][j]= new JPanel(); 
       sub[i][j].setBorder(BorderFactory.createLineBorder(Color.red));    
       screen.add(sub[i][j]); 

       v1=i; 
       v2=j;    
       sub[i][j].addMouseListener(this); 
       f.add(screen); 
      } 

     } 
     f.pack(); 
     f.setVisible(true); 
    } 
    public static void main(String args[]) 
    { 
     new Mutil_Image_2("Grid Layout");  
    } 

    public void mouseClicked(MouseEvent e) { 

    } 
    public void mouseEntered(MouseEvent e) { 
    } 
    public void mouseExited(MouseEvent e) { 
    } 
    public void mouseReleased(MouseEvent e) {  
    } 

    public void mousePressed(MouseEvent e) { 
     JPanel source = (JPanel) e.getSource();  
     BufferedImage img1; 

     if(e.getButton() == MouseEvent.BUTTON1)  
     {      
      x1=e.getX(); 
      y1=e.getY(); 
      //source.setBackground(Color.black); 
      source.setSize(500, 500); 

      try { // The first way to show Image 
       img1 = ImageIO.read(new File("D:\\Pict3.png")); 
       JLabel picLabel = new JLabel (new ImageIcon(img1)); 
       source.add(picLabel); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

      source.add(new draw_Image_in_MultiPanel()); //Second way to show Image   
     } 
    } 
} 

class draw_Image_in_MultiPanel extends JPanel { 

    Image img1,img2; 

    public draw_Image_in_MultiPanel() {   
     setBorder(BorderFactory.createLineBorder(Color.black)); 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     setSize(screenSize.width, screenSize.height); 
    } 

    public void paint(Graphics g) { 
     super.paintComponent(g); 
     int vt1 = Mutil_Image_2.x1; 
     int vt2 = Mutil_Image_2.y1; 

     try 
     { 
       img1 = ImageIO.read(new File("D:\\Pict3.jpg"));      
       g.drawImage(img1, vt1, vt2, img1.getWidth(this),img1.getHeight(this) , Color.darkGray, this); 
       g.drawString("Postion is:" + vt1 + " -" + vt2, vt1,vt2); 


     } catch (IOException e) {   
      e.printStackTrace(); 
     }   
    } } 

回答

0

试试这个你第一种方式:

source.add(picLabel); 
    //Followed by 
    source.revalidate(); 

对于你的第二个办法:

source.add(new draw_Image_in_MultiPanel()); 
    //Again followed by 
    source.revalidate(); 

一般来说,当您添加或在将JFrame设置为可见之后更改组件,您应该在t上调用“revalidate()”他的容器。这告诉Java更新UI布局。我很确定这是你的问题。

编辑

与您的代码玩耍后,它在我看来,调整大小和编辑JPanels将会给你带来了很多头疼的。相反,我想出了一个使用JDialog在您点击面板时显示图像的例子。这个JDialog作为一个面板,你可以在任何地方绘制,它总是在你的其他面板上方。这也意味着您的所有面板可以保持相同的尺寸。它还简化了用户在其他地方点击时关闭面板。 所有你的代码都是一样的(减去我删除的内部类)我对你的代码所做的任何编辑或删除都被注释掉了,所以你可以看到我改变了什么以及为什么。

public class Mutil_Image_2 implements MouseListener { 


//A JDialog to show your image 
public JDialog popup; 
//A JPanel to store the last panel that was clicked. 
public JPanel lastPane; 

public JPanel[][] sub = new JPanel[10][10]; 
public JPanel screen = new JPanel(); 

public JFrame f = new JFrame("Draw on Panel"); 
public static int v1, v2; 
public static int x1 = 1, y1 = 1; 

public Mutil_Image_2(String title) { 
    //Popup that is owned by your JFrame f. 
    popup = new JDialog(f); 
    //Initial location 
    popup.setLocation(0, 0); 
    //Initial size same as source in your original code. 
    popup.setSize(500, 500); 
    //Removes the window controls from the popup to make it look like a JPanel 
    //Setting this to false will make the popup look like a JFrame. 
    popup.setUndecorated(true); 


    //Your original code for drawing all the JPanels 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setExtendedState(JFrame.MAXIMIZED_BOTH); // Full Screen 

    screen.setBorder(BorderFactory.createLineBorder(Color.black)); 
    screen.setLayout(new GridLayout(10, 10)); 

    int dem = 0; 
    for (int i = 0; i <= 9; i++) { 
     for (int j = 0; j <= 9; j++) { 
      sub[i][j] = new JPanel(); 
      sub[i][j].setBorder(BorderFactory.createLineBorder(Color.red)); 
      screen.add(sub[i][j]); 

      v1 = i; 
      v2 = j; 
      sub[i][j].addMouseListener(this); 
      f.add(screen); 
     } 

    } 
    f.pack(); 
    f.setVisible(true); 
} 

public static void main(String args[]) { 
    new Mutil_Image_2("Grid Layout"); 
} 

public void mouseClicked(MouseEvent e) {} 
public void mouseEntered(MouseEvent e) {} 
public void mouseExited(MouseEvent e) {} 
public void mouseReleased(MouseEvent e) {} 

public void mousePressed(MouseEvent e) { 
    JPanel source = (JPanel) e.getSource(); 
    BufferedImage img1; 



    if (e.getButton() == MouseEvent.BUTTON1) { 

     //Close the popup if the same panel was clicked twice 
     if(lastPane != null){ 
      if(lastPane.equals(source)){ 
       popup.setVisible(false); 
       lastPane = null; 
       return; 
      } 
     } 
     //If the above test was false we are clicking a different panel 
     //assign this panel to lastPane so we can close the popup if 
     //this pane is clicked again 
     lastPane = source; 

     //We now get the position of the click in the frames coordinates 
     //Using e.getX/Y will return the click in panel coordinates, we don't want 
     //that as we are positioning the popup relative to the JFrame. 
     x1 = f.getMousePosition().x; 
     y1 = f.getMousePosition().y; 

     //Position the popup on the mouse click 
     popup.setLocation(x1, y1); 

     //Old code 
     //x1 = e.getX(); 
     //y1 = e.getY(); 
     // source.setBackground(Color.black); 
     //source.setSize(500, 500); 

     try { // The first way to show Image 
      img1 = ImageIO.read(new File("D:\\Pict3.png")); 
      JLabel picLabel = new JLabel(new ImageIcon(img1)); 

      //Remove any components in the popups content pane 
      //this essemntially gives up a fresh panel inside 
      //the popup. 
      popup.getContentPane().removeAll(); 
      //add picture to popup 
      popup.add(picLabel); 
      //show popup 
      popup.setVisible(true); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 
} 

}

给一个尝试,我相信这是你在找什么,我能想到的最简单的方法来做到这一点。

回应您关于定位图像的其他问题: 您可以在弹出窗口的内容窗格上使用布局管理器,也可以将弹出窗口的布局管理器设置为空,并在您的JLabel上使用设置位置。

如果您有任何其他问题,请不要犹豫,问:) :)

+0

非常感谢我给你。我试着和它适用于第一种方式,但在第二个方法的Java说: 在java.security.AccessController.doPrivileged(本机方法) \t在java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(来源不明) \t在java.awt.EventQueue.dispatchEvent(未知来源) \t在java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) \t在java.awt.EventDispatchThread.pumpEventsForFilter(未知来源) \t在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源) –

+0

这很有趣,我似乎无法复制那个错误。 –

+0

您是否需要执行第一种方式和第二种方式,或者是否希望只有在第一种方式失败时才执行第二种方式。如果是后者,尝试在catch块中粘贴第二个.add调用。如果两种方法都打算做同样的事情,那么使用这两种方法有点多余。 –