我想在Jframe中添加背景图片

时间:2015-05-11 05:21:14

标签: java jpanel jlabel

我只是制作kmap程序,但代码中存在问题。我使用JFrame在其上使用JLabel设置背景图像和另一个JPanel,我使用gridlayout在其上创建4x4的表。我添加两个JButtons计算和重置以及8个JLabel指示位置(A'B'等)。但背景图片出现在按钮标签和表格的前面。

package kmap;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class kmapping extends JFrame {

    static String output = "";
    static int Arr[][] = new int[4][4];
    static int checked[][] = new int[4][4];
    static int value[] = new int[16];
    JButton btn[] = new JButton[16];
    JLabel lbl[] = new JLabel[10];
    JPanel table;
    ;
        JLabel text, bg;
    JButton compute, reset;

    kmapping() {
        JFrame f = new JFrame();
        f.setTitle("KARNAUGH MAP");
        f.setSize(580, 430);
        f.setVisible(true);
        f.setLocation(300, 50);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setLayout(null);
        bg = new JLabel(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\background.png"));
        bg.setBounds(0, 0, 579, 429);
        f.add(bg);
        table = new JPanel();
        text = new JLabel();
        table.setBounds(140, 30, 400, 300);
        table.setBackground(Color.white);

        text.setBounds(50, 300, 620, 110);
        text.setBackground(Color.blue);
        //adding button in table
        table.setLayout(new GridLayout(4, 4));

        for (int i = 0; i < 16; i++) {
            btn[i] = new JButton();
            btn[i].setIcon(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\images\\x.png"));
            table.add(btn[i]);
            table.validate();
            value[i] = 2;
        }
        //
        lbl[0] = new JLabel("A'B'");
        lbl[0].setBounds(110, 30, 30, 75);
        f.add(lbl[0]);
        lbl[1] = new JLabel("A'B");
        lbl[1].setBounds(110, 105, 30, 75);
        f.add(lbl[1]);
        lbl[2] = new JLabel("AB");
        lbl[2].setBounds(110, 180, 30, 75);
        f.add(lbl[2]);
        lbl[3] = new JLabel("AB'");
        lbl[3].setBounds(110, 255, 30, 75);
        f.add(lbl[3]);
        lbl[4] = new JLabel("C'D'");
        lbl[4].setBounds(160, 0, 80, 30);
        f.add(lbl[4]);
        lbl[5] = new JLabel("C'D");
        lbl[5].setBounds(260, 0, 80, 30);
        f.add(lbl[5]);
        lbl[6] = new JLabel("CD");
        lbl[6].setBounds(360, 0, 80, 30);
        f.add(lbl[6]);
        lbl[7] = new JLabel("CD'");
        lbl[7].setBounds(460, 0, 80, 30);
        f.add(lbl[7]);
        f.validate();

        compute = new JButton("COMPUTE");
        compute.setBounds(5, 100, 100, 40);
        f.add(compute);
        reset = new JButton("RESET");
        reset.setBounds(5, 160, 100, 40);
        f.add(reset);
        f.add(table);
        compute.validate();
        reset.validate();
        f.add(text);
        f.validate();

2 个答案:

答案 0 :(得分:0)

try this ..thanks  


1. Create a subclass of JComponent.
2. Override the paintComponent(Graphics g) method to paint the image that you want to display.
3. Set the content pane of the JFrame to be this subclass.
   // elsewhere

      BufferedImage myImage = ImageIO.load(...);
        JFrame myJFrame = new JFrame("Image pane");
        myJFrame.setContentPane(new ImagePanel(myImage));

答案 1 :(得分:0)

不要担心伴侣的问题是你的背景JLabel应该是你在构造函数上放置的最后一件事而不需要做出重大改变尝试这个希望它有效,根据我的经验我总是这样做我的JFrame上的背景图像。

package kmap;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class kmapping extends JFrame {

static String output = "";
static int Arr[][] = new int[4][4];
static int checked[][] = new int[4][4];
static int value[] = new int[16];
JButton btn[] = new JButton[16];
JLabel lbl[] = new JLabel[10];
JPanel table;
;
    JLabel text, bg;
JButton compute, reset;

kmapping() {
    JFrame f = new JFrame();
    f.setTitle("KARNAUGH MAP");
    f.setSize(580, 430);
    f.setVisible(true);
    f.setLocation(300, 50);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLayout(null);
    table = new JPanel();
    text = new JLabel();
    table.setBounds(140, 30, 400, 300);
    table.setBackground(Color.white);

    text.setBounds(50, 300, 620, 110);
    text.setBackground(Color.blue);
    //adding button in table
    table.setLayout(new GridLayout(4, 4));

    for (int i = 0; i < 16; i++) {
        btn[i] = new JButton();
        btn[i].setIcon(new ImageIcon("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\kmap\\images\\x.png"));
        table.add(btn[i]);
        table.validate();
        value[i] = 2;
    }
    //
    lbl[0] = new JLabel("A'B'");
    lbl[0].setBounds(110, 30, 30, 75);
    f.add(lbl[0]);
    lbl[1] = new JLabel("A'B");
    lbl[1].setBounds(110, 105, 30, 75);
    f.add(lbl[1]);
    lbl[2] = new JLabel("AB");
    lbl[2].setBounds(110, 180, 30, 75);
    f.add(lbl[2]);
    lbl[3] = new JLabel("AB'");
    lbl[3].setBounds(110, 255, 30, 75);
    f.add(lbl[3]);
    lbl[4] = new JLabel("C'D'");
    lbl[4].setBounds(160, 0, 80, 30);
    f.add(lbl[4]);
    lbl[5] = new JLabel("C'D");
    lbl[5].setBounds(260, 0, 80, 30);
    f.add(lbl[5]);
    lbl[6] = new JLabel("CD");
    lbl[6].setBounds(360, 0, 80, 30);
    f.add(lbl[6]);
    lbl[7] = new JLabel("CD'");
    lbl[7].setBounds(460, 0, 80, 30);
    f.add(lbl[7]);
    f.validate();

    compute = new JButton("COMPUTE");
    compute.setBounds(5, 100, 100, 40);
    f.add(compute);
    reset = new JButton("RESET");
    reset.setBounds(5, 160, 100, 40);
    f.add(reset);
    f.add(table);
    compute.validate();
    reset.validate();
    f.add(text);
    f.validate();
//Setting up the background
bg = new JLabel(new ImageIcon("C:\\Documents and Settings\\user\\My  Documents\\NetBeansProjects\\kmap\\background.png"));
    bg.setBounds(0, 0, 579, 429);
    f.add(bg);

现在可以使用,所有组件都将在您的背景之上