0

I just making kmap program but there is a problem in the code . I take JFrame on which I use JLabel to set background image and another JPanel on which I make table of 4x4 using gridlayout. I add two JButtons compute and reset and 8 JLabels indicating the positions(A'B' etc). but the background image comes infront of buttons labels and table.

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();
  • Something like [this for example](http://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503)? – MadProgrammer May 11 '15 at 05:29
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer May 11 '15 at 05:30

2 Answers2

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));
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
0

Don't worry mate the problem is that your background JLabel should be the last thing you put on your constructor no need to make drastic changes try this hope it works , base on my experience I always do this when I put a back ground image on my 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);

This will now work and all you components are going to be on top of your background

Arturouu
  • 13
  • 5