简体   繁体   English

在JTabbedPane中设置背景图片

[英]Setting a background image in JTabbedPane

I want to set a background image in a simple JTabbedPane. 我想在一个简单的JTabbedPane中设置背景图像。 I used a Jlabel to set my background image. 我使用了Jlabel来设置背景图片。 But i haven't been able to do so as i get a null pointer exception upon running. 但是我无法执行此操作,因为运行时出现空指针异常。 Can anyone help with some more ideas? 谁能提供更多想法?

Here is my code: 这是我的代码:

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();     
}
   }

EDIT: 编辑:

 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();   

    }
  }

You may just want to paint the image onto the panel you use for the JTabbedPane (as seen below). 您可能只想将图像绘制到用于JTabbedPane的面板上(如下所示)。 But as MadProgammer pointed out. 但是正如MadProgammer指出的那样。 It's most likely you are getting a null from the file location of your image file. 最有可能是您从图像文件的文件位置获取了空值。

Note: when passing a String to the ImageIcon you are telling it to look for image in the file system. 注意:将String传递给ImageIcon ,是在告诉它在文件系统中查找图像。 though this may work in a development environment from your IDE, it will not work at time of deployment Instead of reading the image file from the file system, you want to load it from the class path, as should be don with embedded resources , using a URL, which can be obtained by using MyClass.class.getResource(path) 尽管这可能在您的IDE的开发环境中可行,但在部署时将无法正常工作,而不是从文件系统中读取映像文件,而是要从类路径中加载它,就像使用嵌入式资源时一样 ,请使用URL,可以使用MyClass.class.getResource(path)

So the way you should loading your image is like this 因此,您加载图像的方式是这样的

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

And your image file would be in the same package as ImageIcon.java . 并且您的图像文件将与ImageIcon.java放在同一包中。 You don't have to put the image in the same package, as you can specify a different path. 不必把图像在同一个包,你可以指定一个不同的路径。 You can find more info from the embedded resource tag wiki link above. 您可以从上面的嵌入式资源标签Wiki链接中找到更多信息。

But back to the option of painting the background, see the example below. 但是回到绘制背景的选项,请参见下面的示例。 Instead of using a URL web link for the image though, you would read in your image file path as specified above. 但是,您可以使用上面指定的方式读取图像文件路径,而不是使用图像的URL Web链接。

在此处输入图片说明

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();
            }
        });
    }
}

You can add an image like this on Jframe: 您可以在Jframe上添加这样的图像:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM