CREATIVE JAVA
Tuesday, 5 February 2013
Login form
Login Form in Swing:
To create a Login Form, we have used two class files:
1) NextPage.java
2) LoginDemo.java
Here is the code of NextPage.java
import javax.swing.*;
import java.awt.*;
class NextPage extends JFrame
{
NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}
Here is the code of LoginDemo.javaimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("roseindia") && value2.equals("roseindia")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
Output Window:
create Animation in basic
How to create Animation:
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class CreateAnimation extends JFrame {
private static int DELAY = 90;
Insets insets;
Color colors[] = { Color.PINK, Color.YELLOW, Color.GREEN, Color.RED,
Color.BLUE, Color.CYAN };
public void paint(Graphics graphics) {
super.paint(graphics);
if (insets == null) {
insets = getInsets();
}
int steps=colors.length;
int x = insets.left;
int y = insets.top;
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
synchronized (colors) {
for (int i = 0; i < steps; i++) {
graphics.setColor(colors[i]);
graphics.fillRect(x, y, width, height);
}
}
}
public void go() {
TimerTask timerTask = new TimerTask() {
public void run() {
Color color = colors[0];
synchronized (colors) {
System.arraycopy(colors, 1, colors, 0, colors.length - 1);
colors[colors.length - 1] = color;
}
repaint();
}
};
Timer timer = new Timer();
timer.schedule(timerTask, 0, DELAY);
}
public static void main(String args[]) {
CreateAnimation animation = new CreateAnimation();
animation.setSize(250, 200);
animation.show();
animation.go();
}
}
Monday, 4 February 2013
Text Area In Java
Text Area In Java:
Sample program:
import javax.swing.*;
public class TextAreaExample {
public static void main(String[] args){
JFrame frame= new JFrame("TextArea frame");
JPanel panel=new JPanel();
JTextArea jt= new JTextArea("Welcome Roseindia",5,20);
frame.add(panel);
panel.add(jt);
frame.setSize(250,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class TextAreaExample {
public static void main(String[] args){
JFrame frame= new JFrame("TextArea frame");
JPanel panel=new JPanel();
JTextArea jt= new JTextArea("Welcome Roseindia",5,20);
frame.add(panel);
panel.add(jt);
frame.setSize(250,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Tab menu in java
Add and Remove Tab to a JTabbedPane Container:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class AddRemoveTab{
JTabbedPane tab;
public static void main(String[] args){
AddRemoveTab ar = new AddRemoveTab();
}
public AddRemoveTab(){
JFrame frame = new JFrame("Add Remove Tab Frame");
tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton button = new JButton("Add Tab");
button.addActionListener(new MyAction());
panel.add(button);
tab.add("Add Tab", panel);
JPanel panel1 = new JPanel();
JButton button1 = new JButton("Remove Tab");
button1.addActionListener(new MyAction());
panel1.add(button1);
tab.add("Remove Tab", panel1);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(str.equals("Add Tab")){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel2 = new JPanel();
JLabel label = new JLabel("Your program is working successfully.");
panel2.add(label);
tab.add(st, panel2);
}
}
else if(str.equals("Remove Tab")){
tab.remove(tab.getTabCount()-1);
}
}
}
}
Progress Bar
Progress Bar in java swing
Sample program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;
public class SwingProgressBar{
final static int interval = 1000;
int i;
JLabel label;
JProgressBar pb;
Timer timer;
JButton button;
public SwingProgressBar() {
JFrame frame = new JFrame("Swing Progress Bar");
button = new JButton("Start");
button.addActionListener(new ButtonListener());
pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);
label = new JLabel("Roseindia.net");
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a timer.
timer = new Timer(interval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (i == 20){
Toolkit.getDefaultToolkit().beep();
timer.stop();
button.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" +
"Downloading completed." + "</b>" + "</font>" + "</html>";
label.setText(str);
}
i = i + 1;
pb.setValue(i);
}
});
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
button.setEnabled(false);
i = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" +
"Downloading is in process......." + "</b>" + "</font>" + "</html>";
label.setText(str);
timer.start();
}
}
public static void main(String[] args) {
SwingProgressBar spb = new SwingProgressBar();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;
public class SwingProgressBar{
final static int interval = 1000;
int i;
JLabel label;
JProgressBar pb;
Timer timer;
JButton button;
public SwingProgressBar() {
JFrame frame = new JFrame("Swing Progress Bar");
button = new JButton("Start");
button.addActionListener(new ButtonListener());
pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);
label = new JLabel("Roseindia.net");
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a timer.
timer = new Timer(interval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (i == 20){
Toolkit.getDefaultToolkit().beep();
timer.stop();
button.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" +
"Downloading completed." + "</b>" + "</font>" + "</html>";
label.setText(str);
}
i = i + 1;
pb.setValue(i);
}
});
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
button.setEnabled(false);
i = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" +
"Downloading is in process......." + "</b>" + "</font>" + "</html>";
label.setText(str);
timer.start();
}
}
public static void main(String[] args) {
SwingProgressBar spb = new SwingProgressBar();
}
}
Slider in java swing
JSlider Component of Java Swing
Sample program:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class CreateSlider{
JSlider slider;
JLabel label;
public static void main(String[] args){
CreateSlider cs = new CreateSlider();
}
public CreateSlider(){
JFrame frame = new JFrame("Slider Frame");
slider = new JSlider();
slider.setValue(70);
slider.addChangeListener(new MyChangeAction());
label = new JLabel("Roseindia.net");
JPanel panel = new JPanel();
panel.add(slider);
panel.add(label);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyChangeAction implements ChangeListener{
public void stateChanged(ChangeEvent ce){
int value = slider.getValue();
String str = Integer.toString(value);
label.setText(str);
}
}
}
Subscribe to:
Posts (Atom)