2012-12-11 9 views
7

Come modificare l'immagine di sfondo jdesktoppane in MDI (interfaccia Documenti multipli) utilizzando java netbeans? Significa che ho aggiunto jdesktoppane a Java MDI, quindi ora voglio cambiare l'immagine di sfondo predefinita di quel jdesktoppane che sto usando in Java MDI. Qualche via facile?Come modificare l'immagine di sfondo predefinita di jdesktoppane?

Controllare che il collegamento di snapshot allegato possa essere che farai meglio a capire la mia domanda cosa voglio.

http://i50.tinypic.com/iml1e9.jpg

+2

Il "vecchio" modo avrebbe estendere un nuovo 'JDesktopPane' e sovrascrivere il metodo' paintComponent' – MadProgrammer

+0

Grazie per l'aiuto e può darmi po 'più di aiuto come il codice sorgente per questo coz io sono nuovo con questo utilizzando .. –

risposta

12

+1 per MadProgrammers commento.

Basta eseguire l'override JDesktopPanepaintComponent(..) e chiamare drawImage(Image img,int x,int y,ImageObserver io) per disegnare un'immagine.

Non dimenticare di onorare la catena vernice e chiamare super.paintComponent(g) come prima chiamata in override paintComponent(..) metodo

Ecco un esempio:

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.SwingUtilities; 

public class JInternalFrameDemo { 

    private JDesktopPane jdpDesktop; 
    private static int openFrameCount = 0; 
    private BufferedImage img; 

    public JInternalFrameDemo() { 
     JFrame frame = new JFrame("JInternalFrame Usage Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     try { 
      img = ImageIO.read(new URL("http://images1.wikia.nocookie.net/__cb20120817224359/villains/images/6/6a/Nine-Tailed_Fox_(Naruto).jpg")); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     // A specialized layered pane to be used with JInternalFrames 
     jdpDesktop = new JDesktopPane() { 
      @Override 
      protected void paintComponent(Graphics grphcs) { 
       super.paintComponent(grphcs); 
       grphcs.drawImage(img, 0, 0, null); 
      } 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(img.getWidth(), img.getHeight()); 
      } 
     }; 



     createFrame(); // Create first window 

     frame.setContentPane(jdpDesktop); 

     frame.setJMenuBar(createMenuBar()); 

     // Make dragging faster by setting drag mode to Outline 
     jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline"); 

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

    protected JMenuBar createMenuBar() { 
     JMenuBar menuBar = new JMenuBar(); 
     JMenu menu = new JMenu("Frame"); 
     menu.setMnemonic(KeyEvent.VK_N); 
     JMenuItem menuItem = new JMenuItem("New IFrame"); 
     menuItem.setMnemonic(KeyEvent.VK_N); 
     menuItem.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       createFrame(); 
      } 
     }); 
     menu.add(menuItem); 
     menuBar.add(menu); 
     return menuBar; 
    } 

    protected void createFrame() { 
     MyInternalFrame frame = new MyInternalFrame(); 
     frame.setVisible(true); 
     // Every JInternalFrame must be added to content pane using JDesktopPane 
     jdpDesktop.add(frame); 
     try { 
      frame.setSelected(true); 
     } catch (java.beans.PropertyVetoException e) { 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new JInternalFrameDemo(); 
      } 
     }); 
    } 

    class MyInternalFrame extends JInternalFrame { 

     static final int xPosition = 30, yPosition = 30; 

     public MyInternalFrame() { 
      super("IFrame #" + (++openFrameCount), true, // resizable 
        true, // closable 
        true, // maximizable 
        true);// iconifiable 
      setSize(300, 300); 
      // Set the window's location. 
      setLocation(xPosition * openFrameCount, yPosition 
        * openFrameCount); 
     } 
    } 
} 
+1

Grazie per l'aiuto grande e il suo funzionamento bene per me .... –

0

risolvo in un separato funzione per creare un oggetto desktop.

codice, come di seguito

private JDesktopPane intializeDesktop(JDesktopPane mydesktop,String imagePath,int scalx,int scaly) { 

     // A specialized layered pane to be used with JInternalFrames 
     mydesktop = new JDesktopPane() { 
      ImageIcon icon = new ImageIcon(imagePath); 
      Image image = icon.getImage(); 

      Image newimage = image.getScaledInstance(scalx, scaly, Image.SCALE_SMOOTH); 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.drawImage(newimage, 0, 0, this); 
      } 
     }; 

     return mydesktop; 
    } 
+0

niente di nuovo rispetto alla risposta accettata, è lì ;-) – kleopatra

+0

i nuovi elementi come di seguito: - -That funzione riutilizzabile è possibile chiamare la directory quando si crea un oggetto JDesktop o è possibile aggiungerlo nell'utilità -Cosa funziona su file immagine locale nella directory images –

+0

riutilizzo e il nome dell'immagine hardcoded/i parametri di ridimensionamento è una contraddizione in sé ;-) – kleopatra

Problemi correlati