The Example demonstrate a login application using java swing. On button click user enter into next screen. copy, paste and Run it. Better use an Eclispe IDE
Login JFrame Example
/**
*
*/
package com.blog.belazy.Frame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
/**
* @author Belazy *
*/
public class FrameLogin extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton loginButton = null;
private MainFrame mainFrame = null;
private FrameLogin thisClass = null;
/**
* This method initializes loginButton
*
* @return javax.swing.JButton
*/
private JButton getLoginButton() {
if (loginButton == null) {
loginButton = new JButton();
loginButton.setBounds(new Rectangle(96, 67, 98, 41));
loginButton.setText("Login");
loginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
mainFrame = new MainFrame();
mainFrame.setVisible(true);
dispose();
}
});
}
return loginButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FrameLogin thisClass = new FrameLogin();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public FrameLogin() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getLoginButton(), null);
}
return jContentPane;
}
}
---------------------------------------------------------------------------------------------
/**
*
*/
package com.blog.belazy.Frame;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
/**
* @author Belazy *
*/
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private FrameLogin frame = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public MainFrame(FrameLogin f) {
super();
this.frame = f;
initialize();
}
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(79, 69, 121, 38));
jButton.setText("delete");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
deleteOld();
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
protected void deleteOld() {
frame.dispose();
}
}
Thanking you....
No comments:
Post a Comment
Your feedback may help others !!!