2013-04-05 84 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

尽管没有布局管理器是可能的,但如果可能的话,您应该使用布局管理器