Inserir imagem no Frame

4 respostas
DANIEL35
Olá preciso urgentemente inserir um logotipo em meu frame. Tentei diversas formas mas não consegui! Como faço? Como passar o caminho para o programa que criei, identificar a imagem que quero que apareça? Alguém poderia ajudar? Vejam o código;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
public class CadastroAluno extends JFrame{
    private JButton b1,  b2,  b3;
    private JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17, l18;
    private JTextField c1, c2, c3, c4, c5, c6, c7, c8, c10, c11, c12, c16, c17, c18;
    private TratarBotoesTelaAluno tba;
    private JComboBox combo, combo_2, combo_3, combo_4;
		
	public CadastroAluno() {
		setTitle("CADASTRO DE ALUNOS");			
	    JPanel p1 = new JPanel();     
        p1.setLayout(new FlowLayout(FlowLayout.LEFT));       
		JPanel p2 = new JPanel();     
        p2.setLayout(new FlowLayout ()); 
        p1.setBackground(Color.LIGHT_GRAY); 
        p2.setBackground(Color.BLACK);         
        
        JCheckBoxMenuItem ca = new JCheckBoxMenuItem ("mensalidade paga ");
         ca.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
             JCheckBoxMenuItem ca= (JCheckBoxMenuItem)e.getSource();
               if(ca.isSelected())
                  System.out.println(ca.getText() + "sim");
               else
                  System.out.println(ca.getText() + "nao");   
            }
        });
        
        String modalidades[] = {"MUSCULAÇÃO","NATAÇÃO","DANÇAS","LUTAS", "YOGA", "GINÁSTICA", "PILATES"};
    	combo = new JComboBox(modalidades);
   	 	combo.setBackground(Color.white);
    	combo.setForeground(Color.red);
    	combo.addItemListener(new ItemListener(){
      	public void itemStateChanged(ItemEvent ie){
        String str = (String)combo.getSelectedItem();
      }
    });
        
        String pagamentos[] = {"MENSAL","TRIMESTRAL","SEMESTRAL"};
    	combo_2 = new JComboBox(pagamentos);
   	 	combo_2.setBackground(Color.white);
    	combo_2.setForeground(Color.red);
    	combo_2.addItemListener(new ItemListener(){
      	public void itemStateChanged(ItemEvent ie){
        String str = (String)combo_2.getSelectedItem();
      }
    });
    
    	String uf[] = {"AC","AL","AP","AM","BA","CE","DF","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RR","RO","RJ","RN","RS","SC","SP","SE","TO"};
    	combo_3 = new JComboBox(uf);
   	 	combo_3.setBackground(Color.white);
    	combo_3.setForeground(Color.red);
    	combo_3.addItemListener(new ItemListener(){
      	public void itemStateChanged(ItemEvent ie){
        String str = (String)combo_3.getSelectedItem();
      }
    });
    
       String status[] = {"ATIVO","INATIVO"};
    	combo_4 = new JComboBox(status);
   	 	combo_4.setBackground(Color.white);
    	combo_4.setForeground(Color.red);
    	combo_4.addItemListener(new ItemListener(){
      	public void itemStateChanged(ItemEvent ie){
        String str = (String)combo_4.getSelectedItem();
      }
    });
    	
    	b1 = new JButton("Novo");
    	b2 = new JButton("Salvar");
    	b3 = new JButton("Limpar");    	
        l1 = new JLabel ("Matrícula nº: ");
    	l2 = new JLabel ("Nome: ");
    	l3 = new JLabel ("RG: ");
    	l4 = new JLabel ("Endereço: ");
    	l5 = new JLabel ("nº: ");
    	l6 = new JLabel ("Bairro: ");
    	l7 = new JLabel ("complemento: ");
    	l8 = new JLabel ("CEP: ");
    	l9 = new JLabel ("Estado: ");
    	l10 = new JLabel("Profissão: ");
    	l11 = new JLabel ("Telefone: ");
    	l12 = new JLabel ("Telefone cel: ");
    	l13 = new JLabel ("Modalidade: ");
    	l14 = new JLabel ("Pagamento: ");
    	l15 = new JLabel ("Status do aluno: ");
    	l16 = new JLabel ("data da matrícula:");
    	l17 = new JLabel ("data do cancelamento ou desitência das atividades na academia:");
    	l18 = new JLabel ("Reponsável por menor de idade:");    	    	    	
    	c1 = new JTextField (5);
    	c2 = new JTextField (35);
    	c3 = new JTextField (10);
    	c4 = new JTextField (33);
    	c5 = new JTextField (5);
    	c6 = new JTextField (10);
    	c7 = new JTextField (8); 
    	c8 = new JTextField (9);
    	c10 = new JTextField (23);
    	c11 = new JTextField (10);
    	c12 = new JTextField (10);     	
    	c16	= new JTextField (10);
    	c17 = new JTextField (10);
    	c18 = new JTextField (30);
    	
    	JTextField [] vetTF = { c1, c2, c3, c4, c5, c6, c7, c8, c10, c11, c12, c16, c17, c18};
    	
    	p1.add(l1);
    	p1.add(c1);
    	p1.add(l2);
    	p1.add(c2);
    	p1.add(l3);
    	p1.add(c3);
    	p1.add(l4);
    	p1.add(c4);
    	p1.add(l5);
    	p1.add(c5);
    	p1.add(l6);
    	p1.add(c6);
    	p1.add(l7);
    	p1.add(c7);
    	p1.add(l8);
    	p1.add(c8);
    	p1.add(ca);
    	p1.add(l9);
    	p1.add(combo_3);
    	p1.add(l10);
    	p1.add(c10);
    	p1.add(l11);
    	p1.add(c11);
    	p1.add(l12);
    	p1.add(c12);
   		p1.add(l13);
    	p1.add(combo);
    	p1.add(l14);
    	p1.add(combo_2);
    	p1.add(l15);
    	p1.add(combo_4);
    	p1.add(l16);
    	p1.add(c16);
    	p1.add(l17);
    	p1.add(c17);
    	p1.add(l18);
    	p1.add(c18);
  
    	p1.add(p2);

    	p2.add(b1);
    	p2.add(b2);
    	p2.add(b3);
    	b1.setForeground(Color.blue);
    	b2.setForeground(Color.blue);
    	b3.setForeground(Color.red);
    	
    	tba = new TratarBotoesTelaAluno(b1,b2,b3, vetTF); 
    	b1.addActionListener(tba);
		b2.addActionListener(tba);
		b3.addActionListener(tba);

		this.setContentPane(p1); 
		setSize(610,350);
		setResizable(false);
		setLocationRelativeTo(null);
	}
}

4 Respostas

J

Olá!
Olha essa classe que usei em um trabalho da faculadade, acho que pode te ajudar.
abraço!

public class ArquivoImagens {
    private static java.net.URL imgURL=null;
    private ArquivoImagens(){}
    private static ArquivoImagens instancia = null;

    public static ArquivoImagens getInstancia(){
        if(instancia==null){
            instancia = new ArquivoImagens();
        }
        return instancia;
    }
    public static ImageIcon createImageIcon(String path) {
        imgURL=ArquivoImagens.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Arquivo não encontrado" + path);
            return null;
        }
    }
}
DANIEL35

Correto, mas não estou sabendo como adaptar esta sua classe dentro desse meu código. Teria como dar essa força? Obrigado.

ViniGodoy

Não duplique topicos:
http://www.guj.com.br/posts/list/206905.java

Esse aqui será trancado. Se você ainda tinha dúvida, responda em seu tópico original.

No outro tópico, já havia sido apresentadas diversas soluções para você.

J

seguinte, você chama o método assim:

ImageIcon img =  ArquivoImagens.getInstancia().createImageIcon("caminho da sua imagem");
   JLabel lbImg = new JLabel(img);

Ele te retorna um ImageIcon aí só você inserir em um JLabel,JButton,etc…

Criado 25 de maio de 2010
Ultima resposta 25 de mai. de 2010
Respostas 4
Participantes 3