• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add an Image to a frame  RSS feed

 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:
[<code>]
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
public class test2 extends javax.swing.JFrame {

private Image _image;
public test2() {
initComponents();
pack();
}
private void initComponents() {
topPanel = new javax.swing.JPanel();
bottomPanel = new javax.swing.JPanel();
lblUser = new javax.swing.JLabel();
txtUser = new javax.swing.JTextField();
lblPassword = new javax.swing.JLabel();
txtPassword = new javax.swing.JTextField();
btnEnter = new javax.swing.JButton();

btnEnter.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEnterActionPerformed(evt);
}
});


addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

try {
MediaTracker mt = new MediaTracker (this);
_image = Toolkit.getDefaultToolkit().getImage("C:/NetBeans/working/sampledir/email_sign.gif");
mt.addImage(_image, 0);
mt.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}

lblUser.setText("username:");
topPanel.add(lblUser);

txtUser.setText(" ");
topPanel.add(txtUser);

lblPassword.setText("password:");
topPanel.add(lblPassword);

txtPassword.setText(" ");
topPanel.add(txtPassword);

btnEnter.setText("enter");
bottomPanel.add(btnEnter);

getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);

getContentPane().add(bottomPanel, java.awt.BorderLayout.CENTER);

pack();
}

private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
test2 mv = new test2();
mv.show();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
mv.setBounds(0,0,d.width, d.height);
mv.setVisible (true);
mv.setBackground(java.awt.Color.white);
}

public void paint(Graphics g) {
super.paint(g);
topPanel.setBackground(java.awt.Color.white);
bottomPanel.setBackground(java.awt.Color.white);
btnEnter.setBackground(java.awt.Color.blue);
btnEnter.setForeground(java.awt.Color.gray);
}


private javax.swing.JPanel topPanel;
private javax.swing.JPanel bottomPanel;
private javax.swing.JLabel lblUser;
private javax.swing.JTextField txtUser;
private javax.swing.JLabel lblPassword;
private javax.swing.JTextField txtPassword;
private javax.swing.JButton btnEnter;
}
[</code>]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,
A couple of items that I see that are not good java programming.
First you should not use the paint method to set background colors to the same value. You should set the background/foreground colors of objects when you create them unless that will change dynamically.
Second, you should not use both the show and setVisible methods. Your call to the show method only adds flickering to the window appearance because after it gets displayed you change its size and position anyway. You can change the size and position before you show it (or setVisible to true).
To add the image you should have your paint method look like the one below.

The image should appear slightly below your enter button.
Regards,
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!