2010-06-09 111 views
2

我在向JFrame添加圖片時遇到了問題,有些東西缺少探測或寫入錯誤。 這裏是類:向JFrame添加圖像時出現問題

主類:

public class Tester 

    { 
     public static void main(String args[]) 
     { 
      BorderLayoutFrame borderLayoutFrame = new BorderLayoutFrame(); 
      borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      borderLayoutFrame.setSize(600,600); 
      borderLayoutFrame.setVisible(true); 
     } 
    } 

public class BorderLayoutFrame extends JFrame implements ActionListener 
{ 
    private JButton buttons[]; // array of buttons to hide portions 
    private final String names[] = { "North", "South", "East", "West", "Center" }; 
    private BorderLayout layout; // borderlayout object 
    private PicPanel picture = new PicPanel(); 

    // set up GUI and event handling 

    public BorderLayoutFrame() 
    { 
     super("Philosofic Problem"); 
     layout = new BorderLayout(5, 5); // 5 pixel gaps 
     setLayout(layout); // set frame layout 
     buttons = new JButton[ names.length ]; // set size of array 

     // create JButtons and register listeners for them 

     for (int count = 0; count < names.length; count++) 
     { 
      buttons[ count ] = new JButton(names[ count ]); 
      buttons[ count ].addActionListener(this); 
     } 
     add(buttons[ 0 ], BorderLayout.NORTH); // add button to north 
     add(buttons[ 1 ], BorderLayout.SOUTH); // add button to south 
     add(buttons[ 2 ], BorderLayout.EAST); // add button to east 
     add(buttons[ 3 ], BorderLayout.WEST); // add button to west 
     add(picture, BorderLayout.CENTER); // add button to center 
    } 

    // handle button events 

    public void actionPerformed(ActionEvent event) 
    { 

    } 

    } 

心中已經試圖將圖像添加到佈局的中心。

這裏是圖像類:

public class PicPanel extends JPanel 
{ 
    Image img; 
    private int width = 0; 
    private int height = 0; 

    public PicPanel() 
    { 
     super(); 
     img = Toolkit.getDefaultToolkit().getImage("table.jpg"); 
    } 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponents(g); 
     if ((width <= 0) || (height <= 0)) 
     { 
      width = img.getWidth(this); 
      height = img.getHeight(this); 
     } 
     g.drawImage(img,0,0,width,height,this); 
    } 
} 

請你的幫助,有什麼問題? 謝謝

順便說一句:我使用eclipse,圖像假設在哪個目錄?

+0

很好的問題 - 什麼是* *問題? ;-) – 2010-06-09 13:25:25

+0

您是否嘗試過將圖像面板添加到空的'JFrame'?如果你這樣做會發生什麼? – jjnguy 2010-06-09 13:41:30

回答

3

有幾個問題與你發佈的代碼:

  • 你應該在你BorderLayoutFrame類使用getContentPane().add()而不是簡單地add()
  • 您應該真的使用SwingUtilities.invokeLater()從測試儀類啓動您的JFrame。事情是這樣的:

SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
     System.setProperty("DEBUG_UI", "true"); 

     BorderLayoutFrame blf = new BorderLayoutFrame(); 
     blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     blf.setSize(600,600); 
     blf.setVisible(true); 
    } 
}); 
  • 不要使用工具包來加載圖片!在以下代碼中,如果「Table.jpg」與PicPanel位於同一個包中,則圖像將正確加載。

public PicPanel() { 
    super(); 
    try { 
     rUrl = getClass().getResource("Table.jpg"); 
     if (rUrl != null) { 
      img = ImageIO.read(rUrl); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
  • PicPanel.PaintComponent()你叫super.paintComponents()是 'S' 一個類型0?
  • PicPanel.PaintComponent(),你需要所有的寬度/高度的東西,只是這樣做:

    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

,避免調用超。paintComponent在一起,因爲你正在繪製一幅圖像,你爲什麼希望面板完全可以繪製?

我最終實現你的東西:

public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       System.setProperty("DEBUG_UI", "true"); 

       BorderLayoutFrame blf = new BorderLayoutFrame(); 
       blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       blf.setSize(600,600); 
       blf.setVisible(true); 
      } 
     }); 
    } 

} 

class BorderLayoutFrame extends JFrame implements ActionListener 
{ 
    private final BorderLayout layout; 
    private final JButton[] buttons; 
    private final String names[] = {"North", "South", "East", "West", "Center"}; 

    public BorderLayoutFrame() { 
     super("Philosofic Problem"); 
     layout = new BorderLayout(5, 5); 
     getContentPane().setLayout(layout); 
     buttons = new JButton[ names.length ]; 

     for (int i=0; i<names.length; i++) 
     { 
      buttons[i] = new JButton(names[i]); 
      buttons[i].addActionListener(this); 
     } 

     getContentPane().add(buttons[0], BorderLayout.NORTH); 
     getContentPane().add(buttons[1], BorderLayout.SOUTH); 
     getContentPane().add(buttons[2], BorderLayout.EAST); 
     getContentPane().add(buttons[3], BorderLayout.WEST); 
     getContentPane().add(new PicPanel(), BorderLayout.CENTER); 
    } 

    public void actionPerformed(ActionEvent e) { 
     // ignore 
    } 

} 

class PicPanel extends JPanel 
{ 
    private URL rUrl; 
    private BufferedImage img; 



    public PicPanel() { 
     super(); 
     try { 
      rUrl = getClass().getResource("UtilBtn.png"); 
      if (rUrl != null) { 
       img = ImageIO.read(rUrl); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     //super.paintComponent(g); 

     g.drawImage(img, 0, 0, getWidth(), getHeight(), this); 
    } 

} 
+1

很好 - 工作很好! :-) – firestruq 2010-06-09 14:11:35

+0

希望我不只是解決你的功課;) – 2010-06-09 14:30:33

2

在Eclipse中,如果您使用路徑"table.jpg",那麼您需要該圖像位於項目的頂層。

有關Eclipse項目結構更多的信息請參見this Stack Overflow question.

您可能需要使用ImageIcon來顯示圖像,而不是繪製它的面板上的嘗試。

以下代碼將創建一個ImageIcon,您可以將其添加到JPanel

ImageIcon pic = new ImageIcon(getClass().getResource("myimage.jpeg"); 

這使用了一種奇特的方式來加載圖片,以便它可以在放入Jar文件時起作用。

+0

我試過了,我沒有錯誤,但沒有任何內容出現在框架上,除了按鈕。 – firestruq 2010-06-09 13:38:38

+0

@firestruq,我加了別的東西,你可能想試試我的答案。 – jjnguy 2010-06-09 13:47:29

+0

ImageIcon將像我在答案中發佈的ImageIO代碼一樣工作。 – 2010-06-09 13:53:05