背景图片Jpanels

时间:2011-03-24 11:25:40

标签: java jpanel

如何添加背景图像我想用一种简单的方式将灰色背景替换为我的代码,我尝试了多种方法,但我无法让它工作。这是我的代码现在有点乱,它只是GUI。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;

public class GUI{
    private JFrame tFrame;
    private JLabel l_name, l_ingrediens1, l_ingrediens2, l_typ, l_tid, l_bild, l_logo, l_matbild, l_background;
    private JTextField tf_name;
    private JTextArea ta_recept1;
    private JList jl_list;
    private JPanel background, background1, backgroundBild, topp, logga, mitt, matbild;
    private JComboBox cb_ingrediens1, cb_ingrediens2, cb_typ, cb_tid;
    private JButton b_find, b_add, b_delete;
    private Receptbas mat;
    private Recept find;

    String[] basIngrediensStrings = { "Fläsk", "Kyckling", "Fisk", "Nöt", "Korv" };
    String[] typStrings = { "Förrätt", "Varmrätt", "Efterrätt", "Övrigt" };
    String[] tidStrings = { "10min", "20min", "30min", "40min", "50min", "60min", "70min", "80min" };
    ImageIcon loggo = new ImageIcon("logga.png");
    ImageIcon bild = new ImageIcon("bild.jpg");
    ImageIcon i_background = new ImageIcon("background.png");

    public GUI(Receptbas mat){
        this.mat = mat;

        tFrame = new JFrame("Recept");
        tFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        l_name = new JLabel("Namn");
        l_ingrediens1 = new JLabel("Ingrediens");
        l_ingrediens2 = new JLabel("Ingrediens");
        l_typ = new JLabel("Typ");
        l_tid = new JLabel("Tid");
        l_logo = new JLabel(loggo);
        l_matbild = new JLabel(bild);
        l_background = new JLabel(i_background);
        tf_name = new JTextField(12);
        ta_recept1 = new JTextArea(40, 10);
        /*jl_list = new JList(<String>[name](mat));
        */
        backgroundBild = new JPanel();
        background1 = new JPanel();
        background = new JPanel();
        topp = new JPanel();
        mitt = new JPanel();
        logga = new JPanel();
        matbild = new JPanel();

        tFrame.setLayout(new FlowLayout());
        background1.setLayout(new OverlayLayout(background1));
        background.setLayout(new BorderLayout());
        mitt.setLayout(new BorderLayout(20,20));
        logga.setLayout(new FlowLayout());
        topp.setLayout(new FlowLayout());
        matbild.setLayout(new FlowLayout());

        b_add = new JButton("Skapa");
        b_delete = new JButton("Ta Bort");
        b_find = new JButton("Sök");
        cb_ingrediens1 = new JComboBox(basIngrediensStrings);
        cb_ingrediens2 = new JComboBox(basIngrediensStrings);
        cb_typ = new JComboBox(typStrings);
        cb_tid = new JComboBox(tidStrings);
        /*MediaTracker mt = new MediaTracker(background);*/

        b_delete.addActionListener(new Deleter());
        b_add.addActionListener(new Creater());
        b_find.addActionListener(new Finder());

        tFrame.add(background1);
        background1.add(background);
        background1.add(backgroundBild);
        background.add(logga, BorderLayout.NORTH);
        background.add(topp, BorderLayout.CENTER);
        background.add(mitt, BorderLayout.SOUTH);

        topp.add(l_name);
        topp.add(tf_name);
        topp.add(l_ingrediens1);
        topp.add(cb_ingrediens1);
        topp.add(l_ingrediens2);
        topp.add(cb_ingrediens2);
        topp.add(l_typ);
        topp.add(cb_typ);
        topp.add(l_tid);                
        topp.add(cb_tid);
        topp.add(b_find);
        topp.add(b_add);
        mitt.add(ta_recept1, BorderLayout.CENTER);
        mitt.add(matbild, BorderLayout.WEST);
        backgroundBild.add(l_background);
        /*mitt.add(jl_list, BorderLayout.EAST);
        mt.addImage(i_background, 0);*/
        matbild.add(l_matbild);
        logga.add(l_logo);

        background.setOpaque(true);
        tFrame.pack();
        tFrame.setVisible(true);


    }

    /*protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int imwidth = i_background.getWidth(null);
        int imheight = i_background.getHeight(null);
        g.drawImage(l_background, 1, 1, null);
    }*/



    public class Finder implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            String name = tf_name.getText(); 
            find = mat.receptFind(name);
            String recept1 = find.getRecept1();
            String ingrediens1 = find.getIngrediens1();
            String ingrediens2 = find.getIngrediens2();
            String typ = find.getTyp();
            String tid = find.getTid();
            cb_ingrediens1.setSelectedItem(ingrediens1);
            cb_ingrediens2.setSelectedItem(ingrediens2);
            cb_typ.setSelectedItem(typ);
            cb_tid.setSelectedItem(tid);
            ta_recept1.setText(recept1);

        }

    }

    public class Deleter implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            mat.delete(find.getName()); 
            tf_name.setText(""); 


            try{
                FileOutputStream fos = new FileOutputStream("save.srz");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                ObjectOutputStream out = new ObjectOutputStream(bos);
                out.writeObject(mat);

                Integer lastInt = new Integer(Unique.last());
                out.writeObject(lastInt);
                out.close();
            }catch(Exception e){
            e.printStackTrace();
            }
        }
    }

    public class Creater implements ActionListener{
        public void actionPerformed(ActionEvent ae){

            Recept s1 = new Recept(tf_name.getText(), (String)cb_ingrediens1.getSelectedItem(), (String)cb_ingrediens2.getSelectedItem(), (String)cb_typ.getSelectedItem(), (String)cb_tid.getSelectedItem(), ta_recept1.getText());
            mat.add(s1);

            try{
                FileOutputStream fos = new FileOutputStream("save.srz");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                ObjectOutputStream out = new ObjectOutputStream(bos);
                out.writeObject(mat);
                Integer lastInt = new Integer(Unique.last());
                out.writeObject(lastInt);
                out.close();
            }catch(Exception e){
            e.printStackTrace();
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

只需将ImageIcon添加到Jpanel即可。 你做了:

ImageIcon loggo = new ImageIcon("logga.png");
l_logo = new JLabel(loggo);
logga.add(l_logo);

似乎您的JPanel尚未设置为可见(也不是logga,也不是l_logo)

还要尝试明确设置包含它的JLabel和JPanel的大小。

答案 1 :(得分:0)

除了关于将图像添加到面板的0verbose代码之外,请尝试在面板上调用paint()或repaint()。 像logga.paint()或logga.repaint()。