Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How can i set background image in this code?

Hello World/Hi

i've finished chat application, but i want to making it look great by adding background image to it, so i hope that i find my anser in this Forum

thanx anway

Code:
[code]/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Client;
/* Imports the javax.swing package classes. */
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.BorderFactory;
/*Imports the java.awt package classes.*/
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.FlowLayout;
import java.awt.Font;
/*Imports the javax.swing.event package classes.*/
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JSeparator;
/*
class ChatLogin - Creates the Chat Login window for the chat application.
Method:
- actionPerformed()
- main()
*/
/**
*
* @author Aymane
*/
public class ChatLogin extends JDialog implements ActionListener
{

JPanel panel;
JPanel pane;
/* Declares the objects of the JLabel class. */
JLabel titleLabel;
JLabel nameLabel;
JLabel pwdLabel;
JLabel ipLabel;
/* Declares the objects of JTextField class */
JTextField nameText;
JPasswordField pwdText;
JTextField ipText;
/*Declares the objects of the JButton class.*/
JButton connect;
JButton cancel;
GridBagLayout gbl;
GridBagConstraints gbc;
int value;
/* Defines the default constructor.*/
public ChatLogin()
{
/*Sets the title of the Font dialog box.*/
setTitle("Zenga Zenga Messenger");
/*Sets the size of Font dialog box.*/
setSize(350, 400);
/*Sets resizable button to FALSE.*/
setResizable(false);
/*Initializes the object of the GridBagLayout class*/
gbl = new GridBagLayout();
/* Sets the Layout. */
getContentPane().setLayout(gbl);
/*Creates an object of the GridBagConstraints class.*/
gbc = new GridBagConstraints();

/*Put test code here*/
//ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
/*Test code end here*/

/*Images inser code
ImageIcon icon = new ImageIcon("D:\ISMIG\Projet de Fin d'Etude\Chat Programme\Java Projet Work\bg.png");
JLabel label = new JLabel(icon);
Container contentPane = getContentPane();
Component add = contentPane.add(label);

Image insert code end*/

/* creation barre des menus */
barreMenus = new JMenuBar() ;
setJMenuBar(barreMenus) ;
/* creation menu Fichier et ses options */
file = new JMenu ("File") ;
barreMenus.add(file) ;
connection = new JMenuItem ("Connect") ;
file.add (connection) ;
changepicture = new JMenuItem ("Change Your Avatar") ;
file.add (changepicture) ;
exit = new JMenuItem ("Exit") ;
file.add (exit) ;
/* creation menu Edition et ses options */
about = new JMenu ("About") ;
barreMenus.add (about) ;
zengazenga = new JMenuItem ("Zenga Zenga Messenger") ;
about.add (zengazenga) ;
author = new JMenuItem ("Author") ;
about.add (author) ;
// Use our version of JScrollPane

/*
Initializes the title label object and adds it to the 1, 1, 2, 1 position with WEST
alignment.
*/
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.NORTH;
pane = new JPanel();
pane.setLayout(new FlowLayout());
titleLabel = new JLabel("Sign in :");
titleLabel.setFont(new Font("Verdana",Font.BOLD,20));
pane.add(titleLabel);
getContentPane().add(pane, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
getContentPane().add(new JSeparator(), gbc);
/*
Initializes the IP address label object and adds it to the 1, 3, 1, 1 position with
EAST alignment
*/
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
ipLabel = new JLabel("Localhost : ");
getContentPane().add(ipLabel, gbc);
/*
Initializes the IP address text field object and adds it to the 2, 3, 1, 1 position with WEST alignment
*/
gbc.gridx = 2;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
ipText = new JTextField("192.168.0.10", 15);
ipText.setFont(new Font("Verdana",Font.PLAIN,12));
getContentPane().add(ipText, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
getContentPane().add(new JSeparator(), gbc);
/*
Initializes the user name label object and adds it to the 1, 5, 1, 1 position with EAST alignment.
*/
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
nameLabel = new JLabel("Username: ");
getContentPane().add(nameLabel, gbc);
/*
Initializes the user name text field object and adds it to the 2, 5, 1, 1 position with
WEST alignment.
*/
gbc.gridx = 2;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
nameText = new JTextField(15);
nameText.setFont(new Font("Verdana",Font.PLAIN,12));
getContentPane().add(nameText, gbc);
gbc.gridx = 1;
gbc.gridy = 6;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
getContentPane().add(new JSeparator(), gbc);
/*
Initializes the password label object and adds it to the 1, 7, 1, 1 position with EAST
alignment.
*/
gbc.gridx = 1;
gbc.gridy = 7;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
pwdLabel = new JLabel("Password: ");
getContentPane().add(pwdLabel, gbc);
/*
Initializes the password text field object and adds it to the 2, 7, 1, 1 position with
WEST alignment.
*/
gbc.gridx = 2;
gbc.gridy = 7;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
pwdText = new JPasswordField(15);
pwdText.setFont(new Font("Verdana",Font.PLAIN,12));
pwdText.addActionListener(this);
getContentPane().add(pwdText, gbc);
gbc.gridx = 111;
gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
getContentPane().add(new JSeparator(), gbc);
/*
Initializes the OK and Cancel button. Adds the button to the 2, 9, 1, 1 position with
WEST alignment.
*/
gbc.gridx = 2;
gbc.gridy = 9;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
connect = new JButton("Connect");
connect.addActionListener(this);
cancel = new JButton("Exit");
cancel.addActionListener(this);
panel.add(connect);
panel.add(cancel);
getContentPane().add(panel, gbc);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}

private JMenuBar barreMenus ;
private JMenu file, about ;
private JMenuItem connection, changepicture, exit, zengazenga, author ;
/*
actionPerformed() - This method is called when the user clicks the any button.
Parameters: ae: Represents an object of the ActionEvent class that contains the details of the event.
Return Value: NA
*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == connect)
{
/*Creates an object of the ChatClient class.*/
ChatClient tcc = new ChatClient();
/*Calls the con() method to connect to the server.*/
tcc.con(nameText.getText(), pwdText.getText(), ipText.getText());
this.setVisible(false);
}
else if(ae.getSource() == cancel)
{
System.exit(0);
}
else if(ae.getSource() == pwdText)
{
/*Creates an object of the ChatClient class.*/
ChatClient tcc = new ChatClient();
/*Calls the con() method to connect to the server.*/
tcc.con(nameText.getText(), pwdText.getText(), ipText.getText());
this.setVisible(false);
}
}
/*Main method.*/
public static void main(String args[])
{

try
{
/*Sets the window look and feel to the application.*/
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){}
ChatLogin cl = new ChatLogin();
cl.show();
}

}

[/code]

Sign In or Register to comment.