12/22/09

example in swing

package swingkeyboard;
import javax.swing.*;
/**
*
 * @author raghuram
*/
public class SwingFrame extends JFrame {
SwingPanel sp;

public SwingFrame() {
sp=new SwingPanel();
this.setBounds(0,0, 400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(sp);
}

}

------------------------------------------


package swingkeyboard;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
*
 * @author raghuram
*/
public class SwingPanel extends JPanel{
JLabel jl;
JTextField jt;

public SwingPanel() {
jl=new JLabel("Type In The TextField");
jt=new JTextField(20);
jt.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e)
{
if((e.getKeyCode()>=0) && (e.getKeyCode()<=127))
{
JOptionPane.showMessageDialog(jt, "You Have Pressed : "+e.getKeyChar(),"Valid",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(jt, "Not Valid","Error",JOptionPane.ERROR_MESSAGE);
jt.setText("");
}
}

});
jl.setBounds(50, 50, 150,15 );
jt.setBounds(200, 50, 100, 15);
this.setSize(400,400);
this.add(jl);
this.add(jt);

}



}


------------------------------




package swingkeyboard;

/**
*
 * @author raghuram
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new SwingFrame();
}

}


No comments:

Post a Comment

Popular Posts