How code the GUI JFrame | Back end coding of GUI | Lover Meter

Here Let's Programming presents the example program of back end coding of GUI JFrame in Java. This is the Swing in Java which offers us to design a program in seconds . Here we give an example of Swing / visual classes in Java. Copy the code and paste in the compiler and get the code for free.Get input form user and show the result by return method in Java.






Source Code:

Class No. 1
-------

import java.util.Random;

public class Method {

        // a back end code for the checker
        public String result (String a, String b)
        {
                // a random number % for everyOne
                Random rNo= new Random();
                int ranNo=rNo.nextInt(101);
                int sizeA=a.length();
                int sizeB=b.length();
               
                return a.substring(0, 1).toUpperCase()+a.substring(1,sizeA).toLowerCase() +" Loves "+ b.substring(0, 1).toUpperCase()+b.substring(1,sizeB).toLowerCase()+" "+ ranNo+ "%";
        }
       
}



Class No. 2
--------

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import java.awt.Window.Type;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

public class LoveChecker extends JFrame {

                private JPanel contentPane;
                private JTextField name1;
                private JTextField name2;


                public static void main(String[] args) {
                                EventQueue.invokeLater(new Runnable() {
                                                public void run() {
                                                                try {
                                                                                LoveChecker frame = new LoveChecker();
                                                                                frame.setVisible(true);
                                                                } catch (Exception e) {
                                                                                e.printStackTrace();
                                                                }
                                                }
                                }
                                );
                }

               
                public LoveChecker() {
                                // for frame
                               
                                setTitle("How mcuh you love me....!");
                                setForeground(SystemColor.inactiveCaptionBorder);
                                setBackground(new Color(222, 184, 135));
                                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                setBounds(100, 100, 450, 300);
                                contentPane = new JPanel();
                                contentPane.setBackground(new Color(205, 92, 92));
                                contentPane.setForeground(new Color(255, 228, 225));
                                contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
                                setContentPane(contentPane);
                                contentPane.setLayout(null);
                               
                                // for title
                                JLabel lblRelationChecker = new JLabel("Check Your Relation");
                                lblRelationChecker.setForeground(new Color(253, 245, 230));
                                lblRelationChecker.setFont(new Font("Andalus", Font.BOLD, 24));
                                lblRelationChecker.setBounds(112, 22, 223, 21);
                                contentPane.add(lblRelationChecker);
                               
                                // for your name
                                JLabel lblstName = new JLabel("Your Name");
                                lblstName.setBounds(10, 81, 77, 27);
                                contentPane.add(lblstName);
                               
                                // text field for get input
                                name1 = new JTextField();
                                name1.setFont(new Font("Tahoma", Font.PLAIN, 16));
                                name1.setForeground(Color.RED);
                                name1.setBackground(Color.PINK);
                                name1.setBounds(97, 78, 99, 30);
                                contentPane.add(name1);
                                name1.setColumns(10);
                               
                                // label only
                                JLabel lblndName = new JLabel("Lover Name");
                                lblndName.setBounds(221, 81, 77, 27);
                                contentPane.add(lblndName);
                               
                                // for get input by text field
                                name2 = new JTextField();
                                name2.setFont(new Font("Tahoma", Font.PLAIN, 16));
                                name2.setForeground(Color.RED);
                                name2.setBackground(Color.PINK);
                                name2.setBounds(308, 78, 99, 30);
                                contentPane.add(name2);
                                name2.setColumns(10);
                               
                                // result show string
                                JLabel lblNewLabel = new JLabel(" ");
                                lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
                                lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM);
                                lblNewLabel.setFont(lblNewLabel.getFont().deriveFont(lblNewLabel.getFont().getSize() + 7f));
                                lblNewLabel.setText("");
                                lblNewLabel.setBounds(26, 131, 381, 39);
               
                                contentPane.add(lblNewLabel);
                               
                                // object of previous class
                                Method m = new Method();
                               
                                // click-able button
                                JButton btnCheckIt = new JButton("Let's Check");
                                btnCheckIt.setBackground(new Color(224, 255, 255));
                                btnCheckIt.setForeground(Color.BLACK);
                                btnCheckIt.addActionListener(new ActionListener() {
                                               
                                                // even perform after press the check me button
                                                public void actionPerformed(ActionEvent e) {
                                                               
                                                                //the text is get from the name 1 and name 2 private member variables and then send as parameter to the method.
                                                                lblNewLabel.setText(m.result(name1.getText(), name2.getText()));
                                                               
                                                }
                                });
                               
                               
                                btnCheckIt.setBounds(152, 181, 141, 23);
                                contentPane.add(btnCheckIt);
                               
                                // for again check the love
                                JButton btnAgain = new JButton("Again");
                                btnAgain.setBackground(new Color(224, 255, 255));
                                btnAgain.addActionListener(new ActionListener() {
                                                // after pressing again button these events are perform
                                                public void actionPerformed(ActionEvent e) {
                                                               
                                                                // name1 is empty
                                                                name1.setText("");
                                                                // name2 is empty
                                                                name2.setText(" ");
                                                                // result is empty
                                                                lblNewLabel.setText(" ");
                                                               
                                                }
                                });
                                btnAgain.setBounds(179, 227, 80, 23);
                                contentPane.add(btnAgain);
                }
}

Comments

Post a Comment

Popular posts from this blog

Operating System | Best Definition of Opetating System

Umbrella activities in Software Engineering