2011-03-24 12 views
0

배경 이미지를 어떻게 추가합니까? 회색 배경을 이미지로 쉽게 바꾸고 싶습니다. 여러 가지 방법을 시도했지만 제대로 작동하지 않습니다. 여기에 내 코드가 약간 지저분 해지고 유일한 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(); 
      } 

     } 
    } 
} 

답변

0

ImageIcon을 Jpanel에 추가하면됩니다. 당신은 한 :

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

이 JPanel의 명시 적 JLabel의와 JPanel에 그것을 containg의 크기를 설정

하는 것도 시도해 볼 (도 logga,도 l_logo)로 설정되지 않은 것 같다.

+0

@Fabian : 정확히 무엇이 작동하지 않습니까? 너는 무엇을보고 무엇을하지 않느냐? – Heisenbug

+0

로고가 작동하고 맨 위에 문제가 backgroundBild (영어 및 스웨덴어 코드 혼합에 대해 유감스럽게 생각 됨)로 표시됩니다. 지금은 회색 배경을 대체하고 싶습니다. 모든 프레임이 보이도록 내부 프레임을 설정했습니다. – Fabian

+0

@Fabian : JPanel 또는 JLabel의 크기를 명시 적으로 설정하려 했습니까? 어쩌면 맞지만 크기가 0 인 – Heisenbug

0

이미지를 패널에 추가하는 코드 0 이외에도 패널에서 paint() 또는 repaint()를 호출 해보십시오. logga.paint() 또는 logga.repaint()와 같은 것입니다.