Hi guys thank you for visiting my site in this article I would like to share with you a sample program that I wrote using Netbeans and Swing in Java. I called this program sum of two numbers using swing in Java. The code is very simple and easy to understand. From my experience writing a GUI application for desktop application in Netbeans is very easy and simple I hope you will find my work uesful. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Sample Program Output
Program Listing
sum_two.java
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jacob
*/
public class sum_two extends javax.swing.JFrame {
/**
* Creates new form sum_two
*/
public sum_two() {
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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txt_val1 = new javax.swing.JTextField();
txt_val2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txt_sum = new javax.swing.JTextField();
btn_quit = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
btn_sum = new javax.swing.JButton();
btn_clear1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Enter Value Number One");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel2.setText("Enter Value Number Two");
txt_val1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
txt_val2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel3.setText("The total sum is");
txt_sum.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btn_quit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btn_quit.setText("Quit");
btn_quit.setToolTipText("Click here to quit program.");
btn_quit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_quitActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setText("Sum of Two Numbers in Java Swing");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel5.setText("Created By Mr. Jake R. Pomperada");
btn_sum.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btn_sum.setText("Sum");
btn_sum.setToolTipText("Click here to find the sum of two numbers.");
btn_sum.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_sumActionPerformed(evt);
}
});
btn_clear1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btn_clear1.setText("Clear");
btn_clear1.setToolTipText("Click here to clear the text box.");
btn_clear1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_clear1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txt_val2)
.addComponent(txt_val1)
.addComponent(txt_sum, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(btn_sum)
.addGap(59, 59, 59)
.addComponent(btn_clear1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_quit)
.addGap(21, 21, 21))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(67, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel4))
.addGap(91, 91, 91))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel4)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txt_val1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_val2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_sum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_sum)
.addComponent(btn_clear1)
.addComponent(btn_quit))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addComponent(jLabel5)
.addGap(23, 23, 23))
);
pack();
}// </editor-fold>
private void btn_quitActionPerformed(java.awt.event.ActionEvent evt) {
String message = "Do you want to quit the program?";
String title = "Really Program Quit?";
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (reply ==JOptionPane.NO_OPTION)
{
this.txt_val1.setText("");
this.txt_val2.setText("");
this.txt_sum.setText("");
this.txt_val1.requestFocus();
}
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
private void btn_sumActionPerformed(java.awt.event.ActionEvent evt) {
int a = Integer.parseInt(this.txt_val1.getText());
int b = Integer.parseInt(this.txt_val2.getText());
int total_sum = (a+b);
this.txt_sum.setText(String.valueOf(total_sum));
}
private void btn_clear1ActionPerformed(java.awt.event.ActionEvent evt) {
this.txt_val1.setText("");
this.txt_val2.setText("");
this.txt_sum.setText("");
this.txt_val1.requestFocus();
}
/**
* @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(sum_two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(sum_two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(sum_two.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(sum_two.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 sum_two().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_clear1;
private javax.swing.JButton btn_quit;
private javax.swing.JButton btn_sum;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField txt_sum;
private javax.swing.JTextField txt_val1;
private javax.swing.JTextField txt_val2;
// End of variables declaration
}