簡體   English   中英

在JTabbedPane中設置背景圖片

[英]Setting a background image in 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();   

    }
  }

您可能只想將圖像繪制到用於JTabbedPane的面板上(如下所示)。 但是正如MadProgammer指出的那樣。 最有可能是您從圖像文件的文件位置獲取了空值。

注意:將String傳遞給ImageIcon ,是在告訴它在文件系統中查找圖像。 盡管這可能在您的IDE的開發環境中可行,但在部署時將無法正常工作,而不是從文件系統中讀取映像文件,而是要從類路徑中加載它,就像使用嵌入式資源時一樣 ,請使用URL,可以使用MyClass.class.getResource(path)

因此,您加載圖像的方式是這樣的

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

並且您的圖像文件將與ImageIcon.java放在同一包中。 不必把圖像在同一個包,你可以指定一個不同的路徑。 您可以從上面的嵌入式資源標簽Wiki鏈接中找到更多信息。

但是回到繪制背景的選項,請參見下面的示例。 但是,您可以使用上面指定的方式讀取圖像文件路徑,而不是使用圖像的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();
            }
        });
    }
}

您可以在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