PDA

Ver la Versión Completa : Como insertar una imagen en un formulario de java


JuanErasmo
26-06-2007, 18:53:42
Hola amigos,
la pregunta es clara, apenas estoy comenzando a usar java,,,,
asi que hice el famoso proyecto "holamundo.java"

el codigo es este /*
* HolaUI.java
*
* Created on 21 de junio de 2007, 05:10 PM
*/
/**
*
* @author Oscar Bedoya
*/
import java.awt.*;
public class HolaUI extends javax.swing.JFrame {

/** Creates new form HolaUI */
public HolaUI() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
etiqueta = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("CareNalga");
setResizable(false);
jButton1.setMnemonic('c');
jButton1.setText("Click");
jButton1.setToolTipText("Haga click aca");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
etiqueta.setFont(new java.awt.Font("Georgia", 0, 24));
etiqueta.setForeground(new java.awt.Color(255, 0, 0));
etiqueta.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
etiqueta.setText("Hola Mundo");
jPanel1.setBackground(new java.awt.Color(0, 23, 255));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 290, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(etiqueta, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(153, 153, 153)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(etiqueta, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addGap(33, 33, 33)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(26, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int r,g,b;
Color color;
r = (int)(Math.random()*255);
g = (int)(Math.random()*255);
b = (int)(Math.random()*255);
color = new Color(r,g,b);

etiqueta.setForeground(color);


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new HolaUI().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel etiqueta;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

Diavlo
25-08-2007, 17:50:09
Si estas usando NetBeans en las propiedades de la etiqueta bysca la que dice icon y ahi le das la direcion de la imagen q quieres q muestre.