Online-Academy

Look, Read, Understand, Apply

Menu

codeApril2023

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 class abbabby extends JFrame implements ActionListener{
    JLabel lbl,lbloutput;
    JTextField txt;
    JButton btn,btn2;

    public abbabby(){
        lbl = new JLabel("Enter String");
        txt = new JTextField(50);
        btn = new JButton("Click here");
        btn2 = new JButton("Do Math");
        lbloutput = new JLabel("Output");
        setLayout(null);
        lbl.setBounds(20,20,120,30);
        txt.setBounds(140,20,200,30);
        btn.setBounds(140,60,200,30);
        btn2.setBounds(140,100,200,30);
        lbloutput.setBounds(20,130,200,60);
        add(lbl);
        add(txt);
        add(btn);
        add(btn2);
        add(lbloutput);
        btn.addActionListener(this);
        btn2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource() == btn){
            String a = txt.getText();
            lbloutput.setText("First Char: "+a.charAt(0)+" Length: "+a.length()+" IndexOf: "+a.indexOf('e'));
        }else{
            int i,v=Integer.parseInt(txt.getText()); 
            for(i=2; i
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class buttons extends JFrame implements ActionListener{
    JButton [] btn;
    buttons(){
        btn = new JButton[6];
        btn[0] = new JButton("1");
        btn[1] = new JButton("2");
        btn[2] = new JButton("3");
        btn[3] = new JButton("4");
        btn[4] = new JButton("5");
        btn[5] = new JButton("6");
        //setLayout(new GridLayout(3,2));
        setSize(300,200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(btn[0],BorderLayout.NORTH);
        add(btn[1],BorderLayout.CENTER);
        add(btn[2]);
        add(btn[3]);
        add(btn[4]);
        add(btn[5]);
        for(int i=0;i<6;i++){
            btn[i].addActionListener(this);
        }
    }
    public static void main(String[] ullu){
        new buttons();
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource() == btn[0]){
            btn[0].setText("100");
        }
    }
}


import javax.swing.*; import java.awt.*; import java.awt.event.*; class mushow extends JFrame implements MouseListener{ JButton btn; mushow(){ btn = new JButton("Mushow"); setSize(500,300); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn.addMouseListener(this); setLayout(new FlowLayout()); add(btn); } public void mousePressed(MouseEvent me){ System.out.println("Hello"); for(int i=0;i<100;i++){ btn.setLocation((20+i),20); try{ Thread.sleep(100); }catch(Exception ie){ } } } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){ System.out.println("Hello Mouse Entered"); } public void mouseExited(MouseEvent e){ System.out.println("Hello MOuse Out"); } public void mouseReleased(MouseEvent e){} public static void main(String[] argg){ new mushow(); } }