Friday, November 4, 2016

simple user interface using java swing

import javax.swing.*;

import org.omg.CORBA.PRIVATE_MEMBER;

import java.awt.ActiveEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;

public class appframe extends JFrame{

private JLabel item1=new JLabel("Name");
private JLabel item2=new JLabel("Address");
private JTextField name;
private JTextField address;


private JButton okbutton;
private JButton cancelbutton;


private JPanel buttonpanel;
private JPanel textpanel;

public appframe()
{   //setting the title of the app
super("The title v 1.00");

setLayout(new FlowLayout());




//creating the button panel
buttonpanel=new JPanel();

//creating text fields

textpanel=new JPanel();

//creating text field
name=new JTextField(20);
address=new JTextField(25);


//creatin buttons
okbutton=new JButton("OK");
cancelbutton =new JButton("Cancel");
//adding textfield to textpanel
textpanel.add(item1);
textpanel.add(name);
textpanel.add(item2);
textpanel.add(address);




//adding the buttons to buttonpanel
buttonpanel.add(okbutton,BorderLayout.SOUTH);
buttonpanel.add(cancelbutton );
//adding button panel to frame

add(textpanel,BorderLayout.NORTH);
add(buttonpanel,BorderLayout.SOUTH);

thehandler handler = new thehandler();//action listener


okbutton.addActionListener(handler);
cancelbutton.addActionListener(handler);







}
private class thehandler implements ActionListener{


public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub



if(e.getActionCommand()=="OK"){
  buttonpanel.setBackground(Color.blue);

}
if(e.getActionCommand()=="Cancel"){
buttonpanel.setBackground(Color.RED);
}

}
}}



//
create a main method as follows
import javax.swing.JFrame;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;


public class test {

public static void main(String[] args) {
// TODO Auto-generated method stub
 appframe window1=new appframe();
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window1.setSize(640,600);


window1.setLocation(100,100);
window1.setVisible(true);
window1.setResizable(false);

}




}



No comments:

Post a Comment

Socket Programming in Java

Source Code: client Side: package learning; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event....