Quantcast
Channel: Webmuch | RSS Feed
Viewing all articles
Browse latest Browse all 167

How To Create A Swing Application Using NetBeans Swing Builder

0
0

 

This article explains the few basic steps to create a String program on Java NetBean and showing a background image in the frame. There are two approaches to generate this feature, one is by heavy coding, second is to simply use drag and drop actions from the Jpanel of the Jframe windows.

 

 

 

                           Steps to create a new Program Swing Application Project:

  1. Choose a File as  ‘New Project’. This can be done alternately by clicking the New Project icon in the IDE toolbar.
  2. Select the Java node in the Categories pane, and in the Projects pane choose Java application.(Click Next)
  3. Enter Project Name as ProgramSwing and Specify the project location.
  4. Leave the check box for Use Dedicated Folder unchecked, for Storing Libraries.
  5. Make sure that the check box for set as Main Project is checked and clear the Create Main Class field.
  6. Click Finish.

    The IDE Swing Application creates the ProgramSwing folder in the designated location in your system. This folder contains all of the project’s associated files, including scripts, folders for storing source and tests as well as folders for project-specific metadata. To view the project structure, use the IDE’s File window.

 

1

Creating a Jframe Container

After creating the new application, you may have noticed that the source packages folder in the projects window contains an empty <ProgramSwing Package> node. To proceed with build our interface, we need to create a java container in which we will place the other required GUI components. In this step, we create a container using the Jframe component and place the container in a new package.

The IDE Swing Application creates the ProgramSwingUI form and also creates the ProgramSwing class with in the ProgramSwingUI.java application and opens the ProgramSwingUI form in the GUI Builder.

 

                          The following shows how the Default package is replaced by my.ProgramSwing package:

  1. In the project window, right-click on the ProgramSwing node and choose New>JFrame Form. Alternately, you will find a Jframe form by choosing New>JFrame Form.
  2. Enter ProgramSwingUI as the Class Name.
  3. Enter my.ProgramSwing as the package.
  4. Click Finish.

2

How to Add the BackGround Images for Jframe

Adding the Background image is just the matter of few drags and drops. Open the Jframe windows, drag the Panel from the Palette which is located in the upper right corner to the Design area.

 

3

 

  1. Add a Jlabel

Drag a Jlabel into the Design Area. Place it on the left of the JtextField, again look for visual cues that suggest an appropriate amount of spacing. Make sure that text base for this component is aligned with that of the JtextField. Go to the Jlable Properties, click the font option than select as Font Design, font size, bold as per your choice. Set the height and width, go to text option and write the name ‘Subscribe Us’ and select foreground color.

 

4

 

  1. Add a JtextField

Now, Drag a JtextField from the Palette to the upper right corner of the Design Area. As you approach the upper right corner, the GUI builder provides visual cues (dashed lines) that suggest the appropriate spacing. Using these cues as a guide, drop a JtextField into the upper Right hand corner. Go to the JtextField Properties click the font option than select FontDesign, fontsize, Bold as per your choice. Set the height and width.

 

5

 

  1. Add a Jbutton

Next, drag a Jbutton form the Palette and drop it in to the left side and underneath the JtextField. Go to the Jbutton properties select the button images, Button size, Height, Width.

 

6

 

  1. Add an another Jlable

And last, drag a Jlabel into the Design Area. Place it to the top of the Jpanel Field, again looking for visual cues that suggest an appropriate amount of spacing. Make sure that text base for this component is aligned with that of the Jform. Go to the Navigator window and drag the all three fields. Jlable, Jtextfield, Jbutton above the last Jlable3[Jlable].

7

 

Go to the Jlable Properties and select the Icon option, open the Jlable-icon window select the External image radio button and then choose the image in ProgramSwing/Src/programSwing and select images.

Click OK, and Finish up.

8

9

 

Image.java source code

To view the code for creating Swing Application, which is generated automatically, click on the source option.

package programswing;

public class Image extends javax.swing.JFrame
{

/**
* Creates new form Image
*/

	public Image()
	{

		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
*/

	@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

	private void initComponents()
	{

		jPanel1 = new javax.swing.JPanel();
		jLabel3 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setMaximumSize(new java.awt.Dimension(600, 600));
		jPanel1.setBackground(new java.awt.Color(205, 219, 232));
		jPanel1.setMinimumSize(new java.awt.Dimension(280, 280));
		jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
		jLabel3.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
		jLabel3.setForeground(java.awt.Color.white);
		jLabel3.setText("SUBSCRIBE US");
		jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 90, 180, 28));
		jPanel1.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 130, 200, 40));
		jButton1.setFont(new java.awt.Font("Ubuntu", 1, 14)); // NOI18N
		jButton1.setForeground(new java.awt.Color(59, 4, 4));
		jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programswing/buttonimg.png"))); // NOI18N
		jButton1.setMaximumSize(new java.awt.Dimension(64, 64));
		jButton1.setMinimumSize(new java.awt.Dimension(32, 32));
		jButton1.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt) 
			{
				jButton1ActionPerformed(evt);
			}
		});

		jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 20, 50, 50));
		jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programswing/1606264.jpg"))); // NOI18N
		jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, -10, 330, 260));
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
		);

		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
		);

		pack();
	}
	// </editor-fold>

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	/**
	* @param args the command line arguments
	*/

	public static void main(String args[])
	{
		/* Set the Nimbus look and feel 
		*/

		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

		/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		*/

		try{

			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} 
		catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} 
		catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
	 	catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} 
		catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}

		//</editor-fold>
		/* Create and display the form 
		*/

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Image().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify

	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JTextField jTextField1;

	// End of variables declaration
}
 

Show the output box message:BUILD SUCCESSFUL(total time: 0 seconds)

Press Shift+F6 to run the programSwing.java

 

10

Do subscribe to our newsletter if you think this tutorial was worth your time. Please share, and happy reading.

Cheers!

 

FOR MORE AWESOME TUTORIALS:

The post How To Create A Swing Application Using NetBeans Swing Builder appeared first on Webmuch.


Viewing all articles
Browse latest Browse all 167

Latest Images

Trending Articles





Latest Images