2013-10-21 33 views
0

我的問題是,我加載我的圖像文件,我將它轉換爲JLabel格式,並且我嘗試將它添加到GridBagLayout樣式框架中,因爲我可以添加每個組件,如按鈕和組合框,我只是不能讓我的圖像文件顯示在框架中。這裏是代碼:將ImageIcon添加到我的幀

package learn; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.*; 

import java.awt.*; 

public class ComboBoxes extends JApplet { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 3404900054342131185L; 
    private String[] Metro21 = { "Indulás", "Örs Vezér tere", "Pillangó utca", "Puskás Ferenc Stadion", 
     "Keleti Pályaudvar", "Blaha Lujza tér", "Astoria", "Deák Ferenc tér", "Kossuth Lajos tér", "Batthyány tér", "Széll Kálmán tér", "Déli Pályaudvar" }; 
    private String[] Metro22 = { "Érkezés", "Örs Vezér tere", "Pillangó utca", "Puskás Ferenc Stadion", 
      "Keleti Pályaudvar", "Blaha Lujza tér", "Astoria", "Deák Ferenc tér", "Kossuth Lajos tér", "Batthyány tér", "Széll Kálmán tér", "Déli Pályaudvar" }; 
    private String[] Metro31 = {"Indulás","Köbánya-Kispest", "Határ úr", "Pöttyös utca", "Ecseri út", "Népliget", "Nagyvárad tér", "Klinikák", "Corvin-negyed", "Kálvin tér", "Ferenciek tere", "Deák Ferenc tér", "Arany János utca", "Nyugati Pályaudvar", "Lehel tér", "Dózsa György út", "Árpád híd", "Forgách utca", "Gyöngyösi utca", "Újpest-Városkapu", "Újpest-Központ"}; 
    private String[] Metro32 = {"Érkezés", "Újpest-Központ", "Újpest-Városkapu", "Gyöngyösi utca", "Forgách utca", "Árpád híd", "Dózsa György út", "Lehel tér", "NYugati Pályaudvar", "Arany János utca", "Deák Ferenc tér", "Ferenciek tere", "Kálvin tér", "Corvin negyed", "Klinikák", "Nagyvárad tér", "Népliget", "Ecseri út", "Pöttyös utca", "Határ út", "Köbánya-Kispest"}; 
    ImageIcon kepem = new ImageIcon("kep.png"); 
    JLabel label = new JLabel(kepem); 

    private JTextField t = new JTextField(15); 
    private JComboBox<String> veg = new JComboBox<String>(); 
    private JComboBox<String> c = new JComboBox<String>(); 


    private JButton b = new JButton("Hol szálljak fel?"); 
    private JButton m2 = new JButton("M2") ; 
    private JButton m3 = new JButton("M3") ; 

    public void init() { 



    c.setPreferredSize(new Dimension(170,25)); 
    veg.setPreferredSize(new Dimension(170,25)); 
    t.setEditable(false); 
    m3.setBackground(Color.LIGHT_GRAY); 
    m2.setBackground(Color.LIGHT_GRAY); 
    c.setEnabled(false); 
    veg.setEnabled(false); 
    c.setBackground(Color.white); 
    veg.setBackground(Color.white); 
    b.setForeground(Color.blue); 
    m2.setForeground(Color.blue); 
    m3.setForeground(Color.blue); 
    b.setEnabled(false); 
    m2.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      int count = 0; 
       int c2=0; 
       c.removeAllItems(); 
       veg.removeAllItems(); 
      m2.setBackground(Color.red); 
      m3.setBackground(Color.LIGHT_GRAY); 
      c.setEnabled(true); 
      veg.setEnabled(true); 
      for (int i = 0; i < 12; i++) 
        c.addItem(Metro21[count++]); 
       for (int i=0; i<12; i++) 
        veg.addItem(Metro22[c2++]); 
     } 

    }); 
    m3.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      int count = 0; 
       int c2=0; 
       c.removeAllItems(); 
       veg.removeAllItems(); 
      m3.setBackground(Color.red); 
      m2.setBackground(Color.LIGHT_GRAY); 
      c.setEnabled(true); 
      veg.setEnabled(true); 
      for (int i = 0; i < 21; i++) 
        c.addItem(Metro31[count++]); 
       for (int i=0; i<21; i++) 
        veg.addItem(Metro32[c2++]); 
     } 

    }); 
    /* b.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     if (count < Metro21.length) 
      c.addItem(Metro21[count++]); 
     } 

    } 
    );*/ 

    c.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      b.setEnabled(true); 
     } 

    }); 

    b.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      int i=veg.getSelectedIndex(); 
      int j=c.getSelectedIndex(); 
      if (i<1 || j<1){ 
       t.setText("Válassz indulást, illetve célt!"); 

      } 
      if (i==j) 
       t.setText("Válassz indulást, illetve célt!"); 
      else {if(i<j){ 
       t.setText("Hátsó résznél"); 

      } 
      else 
       t.setText("Elsö résznél"); 
      }} 
    }); 
    /* c.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ki){ 
      t.getText(); 
     } 
    })*/; 

    // button panel 
    // JFrame button_panel = new JFrame(); 
    // button_panel.add(b); 

    // placing controls to dialog 
    GridBagConstraints ppp; 

    GridBagLayout layout = new GridBagLayout(); 
    setLayout(layout); 

    ppp = new GridBagConstraints(); 
    ppp.gridx =0; 
    ppp.gridy=0; 
    ppp.anchor = GridBagConstraints.WEST; 
    add(m2, ppp); 


    ppp = new GridBagConstraints(); 
    ppp.anchor = GridBagConstraints.CENTER; 
    ppp.gridx=0; 
    ppp.gridy=0; 
    add(m3, ppp); 



    ppp = new GridBagConstraints(); 
    ppp.gridx = 0; 
    ppp.gridy = 1; 

    add(c, ppp); 

    ppp = new GridBagConstraints(); 
    ppp.gridx = 1; 
    ppp.gridy = 1; 
    ppp.anchor = GridBagConstraints.CENTER; 
    add(veg, ppp); 

    ppp = new GridBagConstraints(); 
    ppp.gridx = 2; 
    ppp.gridy = 1; 
    ppp.anchor = GridBagConstraints.CENTER; 

    add(b, ppp); 

    ppp = new GridBagConstraints(); 
    ppp.gridx = 3; 
    ppp.gridy = 1; 
    ppp.gridheight=2; 
    ppp.anchor = GridBagConstraints.WEST; 
    add(t, ppp); 

    ppp =new GridBagConstraints(); 
    ppp.gridx=0; 
    ppp.gridy=0; 
    add(label, ppp); 

    } 

    public static void main(String[] args) { 
    run(new ComboBoxes(), 800, 500); 
    } 

    public static void run(JApplet applet, int width, int height) { 
    JFrame frame = new JFrame("BKV Ajtófigyelö"); 
    frame.setResizable(false); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(applet); 
    frame.setSize(width, height); 
    applet.init(); 
    applet.start(); 
    frame.setVisible(true); 
    } 
} 

回答

0

這很可能是圖像不在應用程序目錄中。轉到System.getProperty("user.dir")指示的目錄,並確保圖像存在於該位置