Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 背景图像Jpanels_Java_Jpanel - Fatal编程技术网

Java 背景图像Jpanels

Java 背景图像Jpanels,java,jpanel,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_nam

如何添加背景图像我想以一种简单的方式将灰色背景替换为我的代码中的图像,我已经尝试了多种方法,但无法使其正常工作。这是我的代码,现在有点凌乱,只有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();
            }

        }
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.io.*;
导入java.lang.*;
公共类GUI{
私有JFrame-tFrame;
私人JLabel l_name、l_ingrediens1、l_ingrediens2、l_typ、l_tid、l_bild、l_logo、l_matbild、l_background;
私有jtextf_名称;
私人JTextArea ta_recept1;
私人名单;
私人JPanel背景、背景1、背景图片、topp、logga、mitt、matbild;
私人JComboBox cb_ingrediens1、cb_ingrediens2、cb_typ、cb_tid;
私有JButton b_find、b_add、b_delete;
私人收纳台;
私人接收发现;
String[]basingredienstrings={“Fläsk”、“Kyckling”、“Fisk”、“Nöt”、“Korv”};
String[]typStrings={“Förrätt”、“Varmrätt”、“Efterrätt”、“Övrigt”};
字符串[]tidStrings={“10min”、“20min”、“30min”、“40min”、“50min”、“60min”、“70min”、“80min”};
ImageIcon loggo=新的ImageIcon(“logga.png”);
ImageIcon bild=新的ImageIcon(“bild.jpg”);
ImageIcon i_background=新的ImageIcon(“background.png”);
公共图形用户界面(Receptbas-mat){
this.mat=mat;
tFrame=新JFrame(“接收”);
tFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l_name=新的JLabel(“Namn”);
l_ingrediens1=新的JLabel(“Ingrediens”);
l_ingrediens2=新的JLabel(“Ingrediens”);
l_typ=新JLabel(“typ”);
l_tid=新的JLabel(“tid”);
l_徽标=新的JLabel(loggo);
l_matbild=新的JLabel(bild);
l_背景=新的JLabel(i_背景);
tf_name=新的JTextField(12);
ta_recept1=新的JTextArea(40,10);
/*jl_list=新的JList([名称](mat));
*/
backgroundBild=新JPanel();
背景1=新的JPanel();
背景=新的JPanel();
topp=新的JPanel();
米特=新的JPanel();
logga=新的JPanel();
matbild=新的JPanel();
setLayout(新的FlowLayout());
背景1.设置布局(新覆盖图(背景1));
setLayout(新的BorderLayout());
mitt.setLayout(新边界布局(20,20));
logga.setLayout(新的FlowLayout());
topp.setLayout(新的FlowLayout());
matbild.setLayout(新的FlowLayout());
b_add=新的JButton(“Skapa”);
b_delete=new JButton(“Ta Bort”);
b_find=newjbutton(“Sök”);
cb_Ingredins1=新的JComboBox(基本编辑字符串);
cb_Ingredins2=新的JComboBox(基本编辑字符串);
cb_typ=新JComboBox(类型字符串);
cb_tid=新的JComboBox(tidStrings);
/*MediaTracker mt=新的MediaTracker(背景)*/
b_delete.addActionListener(新的Deleter());
b_add.addActionListener(新的Creater());
b_find.addActionListener(newfinder());
t添加(背景1);
背景1.添加(背景);
背景1.添加(背景图片);
添加(logga,BorderLayout.NORTH);
添加(topp,BorderLayout.CENTER);
背景。添加(手套,边界布局。南部);
顶部添加(l_名称);
顶部添加(tf_名称);
顶部添加(l_Ingredins1);
顶部添加(cb_Ingredins1);
顶部添加(l_Ingredins2);
顶部添加(cb_Ingredins2);
顶部添加(l_类型);
顶部添加(cb_类型);
顶部添加(l_tid);
顶部添加(cb_tid);
顶部添加(b_查找);
顶部添加(b_添加);
mitt.add(ta_recept1,BorderLayout.CENTER);
mitt.add(matbild,BorderLayout.WEST);
背景图片添加(l_背景);
/*mitt.add(jl_列表,边界布局,东部);
mt.ADIMAGE(i_背景,0)*/
添加(l_matbild);
logga.添加(l_徽标);
背景设置不透明(真);
tFrame.pack();
tFrame.setVisible(true);
}
/*受保护组件(图形g){
超级组件(g);
int imwidth=i_background.getWidth(null);
int imheight=i_background.getHeight(null);
g、 drawImage(l_背景,1,1,空);
}*/
公共类查找器实现ActionListener{
已执行的公共无效行动(行动事件ae){
String name=tf_name.getText();
查找=材料接收查找(名称);
字符串recept1=find.getRecept1();
String ingrediens1=find.getIngrediens1();
String ingrediens2=find.getIngrediens2();
字符串类型=find.getTyp();
字符串tid=find.getTid();
cb_Ingredins1.设置选定项(Ingredins1);
cb_Ingredins2.设置选定项(Ingredins2);
cb_典型设置选择项(典型);
cb_tid.setSelectedItem(tid);
tau recept1.setText(recept1);
}
}
公共类Deleter实现ActionListener{
已执行的公共无效行动(行动事件ae){
mat.delete(find.getName());
tf_name.setText(“”);
试一试{
FileOutputStream fos=新的FileOutputStream(“save.srz”);
BufferedOutputStream bos=新的BufferedOutputStream(fos);
ObjectOutputStream out=新的ObjectOutputStream(bos);
out.writeObject(mat);
整数lastInt=新整数(Unique.last());
out.writeObject(lastInt);
out.close();
}捕获(例外e){
e、 printStackTrace();
}
}
}
公共类Creater实现ActionListener{
已执行的公共无效行动(行动事件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();
材料添加(s1);
ImageIcon loggo = new ImageIcon("logga.png");
l_logo = new JLabel(loggo);
logga.add(l_logo);