【问题标题】:i want to add background image in Jframe [duplicate]我想在 Jframe 中添加背景图像 [重复]
【发布时间】:2015-05-11 05:21:14
【问题描述】:

我只是在制作 kmap 程序,但代码有问题。我使用 JFrame,在其上使用 JLabel 设置背景图像,并使用另一个 JPanel,在其上使用 gridlayout 制作 4x4 表格。我添加了两个 JButton 计算和重置以及 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();

【问题讨论】:

  • 避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

标签: java jpanel jlabel


【解决方案1】:
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));

【讨论】:

    【解决方案2】:

    别担心,问题是你的背景 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);
    

    这现在可以工作了,你所有的组件都将在你的背景之上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2015-02-12
      相关资源
      最近更新 更多