2012-08-14 50 views
1

我正在使用netbeans來構建使用swing的圖像查看器..使用netbeans swing builder GUI我得到了下面的代碼,我把jpanel放在了一個父jframe中。所有我想要做的的,現在是開放的JPanel中的圖像,並將其添加到JFrame中使用jpanel變量在Jframe中加載圖像

這裏是代碼:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package javaapplication1; 

import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JPanel; 

/** 
* 
* @author Tempo 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() throws IOException { 
     initComponents(); 
     final BufferedImage gray_img=ImageIO.read(new File("C:/Users/Tempo/Pictures/sucess.png")); 
        this.setBounds(0, 0, gray_img.getWidth(), gray_img.getHeight()); 
        this.jPanel1 =new JPanel(){ 
        @Override 
        public void paint(Graphics g) { 
          // TODO Auto-generated method stub 
          super.paint(g); 
          g.drawImage(gray_img, 0, 0, null); 
        } 

            }; 
        this.add(jPanel1); 

    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 440, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 276, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(153, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(21, 21, 21) 
       .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        NewJFrame frame=new NewJFrame(); 
//     frame.setPreferredSize(new Dimension(800,600)); 
//     frame.setMinimumSize(new Dimension(200,400)); 


        frame.setVisible(true); 
       } catch (IOException ex) { 
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex); 
       } 

      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration     
} 
當文件被運行

,它只是顯示一個空白的JFrame沒有圖像/(Jpanel)在裏面!

+0

而不是使用絕對路徑的圖像,更好地提供相對路徑,以便它們可以在任何機器上運行,而無需修改任何有關圖像路徑的種類。看看[這個答案](http://stackoverflow.com/a/9866659/1057230)我的更多見解。雖然如果你手動執行,還有一個[answer](http://stackoverflow.com/a/11372350/1057230)在這裏獲得更多幫助:-) – 2012-08-14 09:34:06

回答

4

您沒有對LayoutManager提供任何約束,它似乎忽略了面板。

另一種方案是,創建與您在一個單獨的類文件需要油漆例程的自定義面板,編譯項目,拖放自定義面板到NewJFrame形式

或者,直到你得到一握爲此,請手動編碼佈局。它會讓你更好地理解正在發生的事情,並在將來更輕鬆地解決這些問題。

另外,除非你有這樣一個很好的理由,否則不要重寫'油漆',而是用paintComponent來代替。

哦,這個this.setBounds(0, 0, gray_img.getWidth(), gray_img.getHeight())是一個非常糟糕的主意。你可能會發現佈局管理器已經重寫了這些值。

更好地使用JScrollPane可變寬度/高度組件。並且您可能需要提供首選大小的細節

+0

編寫自定義面板幫助:)...但只有部分的圖像越來越多地顯示在面板上,想要使圖像根據面板尺寸改變其分辨率。 – shridatt 2012-08-14 06:23:56

+1

那麼,你想縮小它?最簡單的方法是使用'Image.getScaledInstance',它是'BufferedImage'的成員。請記住,您對寬高比負責。 'getScaledInstance'不是最快的,也不是最好的縮放算法,但它是最簡單的。如果您想要了解更多信息,請閱讀http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html – MadProgrammer 2012-08-14 06:39:59

+1

並使用paintComponent()而不是油漆() – mKorbel 2012-08-14 06:44:42