在JTabbedPane中设置背景图像

时间:2014-02-23 03:26:11

标签: java image swing jframe jtabbedpane

我想在简单的JTabbedPane中设置背景图像。我使用Jlabel来设置我的背景图像。但我无法这样做,因为我在运行时得到一个空指针异常。 任何人都可以提供更多想法吗?

这是我的代码:

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.*;

class ImageTest 
{
 JTabbedPane tp;
 JLabel lab1
 JPanel  welcome;
 JFrame frame;
 ImageIcon image2;
 public void createUI()
 {
    frame=new JFrame("JTabbedPane");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    welcome= new JPanel(null);
    welcome.setPreferredSize(new Dimension(1366,786));
    image2 = new ImageIcon("icloud.jpg");
    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);
    tp.addTab("Welcome", welcome);

    lab1.setIcon(image2);
    lab1.setBounds(0,0,1500,700);

    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{

    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }
    ImageTest  tbp = new ImageTest ();
    tbp.createUI();     
}
   }

编辑:

 import javax.swing.ImageIcon;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextField;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.UIManager.LookAndFeelInfo;

 import java.awt.*;
 import java.awt.event.*;

 class ImageTest 
  {
 JTabbedPane tp;
 JLabel lab1;
 JPanel  welcome,w;
 JFrame frame;
 ImageIcon image2;
 JButton b1;

public void createUI()
{
    frame=new JFrame("JTabbedPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b1=new JButton();
    welcome= new JPanel(new GridLayout(1,1,15,15));
    w=new JPanel (new GridLayout(1, 1, 15, 15));
    welcome.setPreferredSize(new Dimension(1366,786));

    ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

    tp=new JTabbedPane();
            Container pane = frame.getContentPane();
    pane.add(tp);


    lab1=new JLabel();
    lab1.setIcon(icon);
    w.setOpaque(false);
    w.add(b1);


    b1.setVisible(true);
            welcome.add(lab1);
            welcome.add(w);
            tp.addTab("Welcome", welcome);

    frame.setSize(500,500);
    frame.pack();
    frame.setVisible(true);
    frame.setTitle("I-Cloud");

}   
public static void main(String[] args) 
{
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }

        ImageTest w = new ImageTest();
        w.createUI();   

    }
  }

2 个答案:

答案 0 :(得分:2)

您可能只想将图像绘制到用于JTabbedPane的面板上(如下所示)。但正如MadProgammer指出的那样。您很可能从图像文件的文件位置获取空值。

注意:将字符串传递给ImageIcon时,您要告诉它在文件系统中查找图像。虽然这可能在IDE的开发环境中有效,但在部署时无法正常工作而不是从文件系统中读取图像文件,而是要从类路径加载它,因为应该使用{{3} },使用URL,可以使用MyClass.class.getResource(path)

获取

所以加载图片的方式就是这样

ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

您的图片文件与ImageIcon.java位于同一个包中。您没有 将图像放在同一个包中,因为您可以指定不同的路径。您可以从上面的嵌入式资源标记wiki链接中找到更多信息。

但是回到绘制背景的选项,请参阅下面的示例。您可以按照上面指定的方式读取图像文件路径,而不是使用图像的URL Web链接。

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TabBackground {

    private BufferedImage bg;

    public TabBackground() {
        try {
            bg = ImageIO.read(new URL("http://2.bp.blogspot.com/-wWANHD-Dr00/TtSmeY57ZXI/AAAAAAAABB8/t-fpXmQZ0-Y/s1600/Vector_by_Karpiu23.png"));
        } catch (IOException ex) {
            Logger.getLogger(TabBackground.class.getName()).log(Level.SEVERE, null, ex);
        }

        JPanel tabPanel = new JPanel(new GridBagLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
        buttons.setOpaque(false);
        for (int i = 0; i < 4; i++) {
            buttons.add(new JButton("Button"));
        }
        tabPanel.add(buttons);

        JTabbedPane tabPane = new JTabbedPane();
        tabPane.add("Panel with Bachground", tabPanel);

        JFrame frame = new JFrame("Tabbed Pane with Background");
        frame.setContentPane(tabPane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(TabBackground.class.getName()).log(Level.SEVERE, null, ex);
                }
                new TabBackground();
            }
        });
    }
}

答案 1 :(得分:2)

您可以在Jframe上添加如下图像:

tp.addTab("Welcome",new JLabel(new ImageIcon("bksqla_xlargecover.jpg")));

enter image description here