2013-04-05 61 views
1

我想從src文件夾實現一個200 * 200圖片到我的JTabbed Pane佈局。如何將圖片添加到JTabbedPane - 空面板佈局上?

我的問題是什麼都沒有顯示 - 沒有錯誤沒有例外,也沒有圖片。

我不認爲我必須聲明目錄並將其設置爲私有,因爲它已經包含在src文件夾中。

import java.awt.*; 
import javax.swing.*; 

import java.awt.event.KeyEvent; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JFrame; 



public class Example1 
     extends JFrame 


{ 
    private  JTabbedPane tabbedPane; 
    private  JPanel  panel1; 

    public Example1() 
    { 
     // NOTE: to reduce the amount of code in this example, it uses 
     // panels with a NULL layout. This is NOT suitable for 
     // production code since it may not display correctly for 
     // a look-and-feel. 

     setTitle("Program"); 
     setSize(800, 400); 
     setBackground(Color.gray); 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new BorderLayout()); 
     getContentPane().add(topPanel); 

     // Create the tab pages 
     createPage1(); 


     // Create a tabbed pane 
     tabbedPane = new JTabbedPane(); 
     tabbedPane.addTab("Tab Page", panel1); 
     topPanel.add(tabbedPane, BorderLayout.CENTER); 
    } 

    public void createPage1() 
    { 
     panel1 = new JPanel(); 
     panel1.setLayout(null); 

     ImageIcon pic = new ImageIcon("test.png"); 
     JLabel label = new JLabel (pic); 
     panel1.add(label); 
     label.setVisible (true); 
     label.setBounds(200, 200, 200, 400); 
} 

// Main method to get things started 
    public static void main(String args[]) 
    { 
     // Create an instance of the test application 
     Example1 mainFrame = new Example1(); 
     mainFrame.setVisible(true); 


    } 
} 

如果您要我提供更多信息,請詢問。

回答

1

不要在面板上使用null佈局(並獲取setBounds())。

該面板沒有首選大小,因此Swing認爲沒有任何內容可以繪製。

0

ImageIcon不顯示任何錯誤,如果圖像不能被發現 - 確保圖像位於應用

重新使用null佈局的應用程序目錄,從docs

儘管沒有佈局管理器是可能的,但如果可能的話,您應該使用佈局管理器