Created
August 4, 2012 14:20
-
-
Save tedhagos/3258037 to your computer and use it in GitHub Desktop.
RD Programming Concepts Training - Batch 1 (part 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.JFrame; | |
| import javax.swing.JButton; | |
| import java.awt.FlowLayout; | |
| import javax.swing.*; | |
| import java.awt.event.*; | |
| class Gui1 extends JFrame implements ActionListener { | |
| JLabel label = new JLabel("Your name: "); | |
| JTextField tf = new JTextField(20); | |
| JButton b = new JButton("Ok"); | |
| FlowLayout fl = new FlowLayout(); | |
| Gui1() { | |
| setLayout(fl); | |
| add(label); | |
| add(tf); | |
| add(b); | |
| b.addActionListener(this); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| setSize(300,300); | |
| setVisible(true); | |
| } | |
| public void actionPerformed(ActionEvent ae) { | |
| tf.setText("Hello World"); | |
| } | |
| public static void main(String[] args) { | |
| new Gui1(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.event.*; | |
| import java.awt.*; | |
| import javax.swing.*; | |
| class Gui2 extends JFrame { | |
| JTextField tf = new JTextField(20); | |
| JButton btn = new JButton("Click me"); | |
| MyListener ml;ja | |
| Gui2() { | |
| ml = new MyListener(this); | |
| setLayout(new FlowLayout()); | |
| add(tf); | |
| add(btn); | |
| btn.addActionListener(ml); | |
| setDefaultCloseOperation(3); | |
| pack(); | |
| setVisible(true); | |
| } | |
| public static void main(String[] args) { | |
| new Gui2(); | |
| } | |
| } | |
| class MyListener implements ActionListener { | |
| Gui2 g2; | |
| MyListener(Gui2 var) { | |
| g2 = var; | |
| } | |
| @Override | |
| public void actionPerformed(ActionEvent ae) { | |
| g2.tf.setText("Hello World"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.JFrame; | |
| import javax.swing.JTextField; | |
| import javax.swing.JButton; | |
| import java.awt.event.ActionListener; | |
| import java.awt.event.ActionEvent; | |
| class Gui3 extends JFrame implements ActionListener { | |
| JTextField tf; | |
| JButton btn; | |
| int counter = 0; | |
| Gui3() { | |
| setLayout(new java.awt.FlowLayout()); | |
| tf = new JTextField(20); | |
| btn = new JButton("Okay"); | |
| add(tf);add(btn); | |
| btn.addActionListener(this); | |
| setDefaultCloseOperation(3); | |
| pack(); | |
| setVisible(true); | |
| } | |
| @Override | |
| public void actionPerformed(ActionEvent ae) { | |
| btn.setText(++counter + ""); | |
| tf.setText(++counter + ""); | |
| } | |
| public static void main(String[] args) { | |
| new Gui3(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.*; | |
| import java.awt.event.*; | |
| import java.awt.*; | |
| class Gui4 extends JFrame { | |
| JTextField tf = new JTextField(20); | |
| JButton btn = new JButton("OK"); | |
| JButton btnx = new JButton("Cancel"); | |
| Gui4() { | |
| InListener in = new InListener(); | |
| setLayout(new FlowLayout()); | |
| add(tf); | |
| add(btn); | |
| add(btnx); | |
| btn.addActionListener(in); | |
| btnx.addActionListener( | |
| new ActionListener() { | |
| public void actionPerformed(ActionEvent ae) { | |
| tf.setText("From anonymous class"); | |
| } | |
| } | |
| ); | |
| pack(); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| setVisible(true); | |
| } | |
| // INNER CLASS | |
| class InListener implements ActionListener { | |
| public void actionPerformed(ActionEvent ae) { | |
| tf.setText("From Inner class"); | |
| } | |
| } | |
| // END INNNER CLASS | |
| public static void main(String[] args) { | |
| new Gui4(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RandomWord extends Gui3 { | |
| String animals[]; | |
| int counter = 0; | |
| RandomWord() { | |
| animals = new String[] {"Dog","Cat","Lion","Mountain Lion","Leopard"}; | |
| } | |
| public void actionPerformed(java.awt.event.ActionEvent ae){ | |
| //tf.setText(this.getClass().getName()); | |
| //tf.setText(animals[++counter]); | |
| tf.setText(ae.getActionCommand()); | |
| } | |
| public static void main(String[] args) { | |
| new RandomWord(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Str { | |
| public static void main(String[] args) { | |
| String s = "Hello"; | |
| reverse(s); | |
| } | |
| static void reverse(String var) { | |
| char temp[] = new char[var.length()]; | |
| char pmet[] = new char[var.length()]; | |
| for(int i=0;i<var.length();i++) { | |
| temp[i] = var.charAt(i); | |
| } | |
| for(int i = var.length()-1; i >= 0; i--) { | |
| pmet[i] = var.charAt(i); | |
| } | |
| System.out.println(pmet); | |
| System.out.println(var.charAt(3)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.event.*; | |
| class ThreadG extends Gui3 { | |
| public void actionPerformed(ActionEvent ae){ | |
| MyThread t = new MyThread(); | |
| t.start(); | |
| System.out.println(""); | |
| } | |
| public static void main(String[] args) { | |
| new ThreadG(); | |
| } | |
| } | |
| class MyThread extends Thread { | |
| public void run(){ | |
| for(long i=1;i<100000000;i++){ | |
| System.out.println(i); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.*; | |
| import java.awt.FlowLayout; | |
| import java.awt.GridLayout; | |
| import java.awt.BorderLayout; | |
| class UserForm extends JFrame { | |
| JLabel lblfname = new JLabel("First name : "); | |
| JLabel lbllname = new JLabel("Last name :"); | |
| JLabel lblemail = new JLabel("Email :"); | |
| JTextField tffname = new JTextField(20); | |
| JTextField tflname = new JTextField(20); | |
| JTextField tfemail = new JTextField(20); | |
| JButton btnok = new JButton("Okay"); | |
| JButton btncancel = new JButton("Cancel"); | |
| JPanel pcenter = new JPanel(); | |
| JPanel psouth = new JPanel(); | |
| GridLayout gl = new GridLayout(3,2); | |
| FlowLayout fl = new FlowLayout(); | |
| UserForm() { | |
| pcenter.setLayout(gl); | |
| psouth.setLayout(fl); | |
| psouth.add(btnok); | |
| psouth.add(btncancel); | |
| pcenter.add(lblfname); | |
| pcenter.add(tffname); | |
| pcenter.add(lbllname); | |
| pcenter.add(tflname); | |
| pcenter.add(lblemail); | |
| pcenter.add(tfemail); | |
| add(pcenter, BorderLayout.CENTER); | |
| add(psouth, BorderLayout.SOUTH); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| pack(); | |
| setVisible(true); | |
| } | |
| public static void main(String[] args) { | |
| new UserForm(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment