0% found this document useful (0 votes)
20 views11 pages

077file Oop

Uploaded by

vy9wmg2mzn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

077file Oop

Uploaded by

vy9wmg2mzn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Name: ASHBIL ALI

Roll number fa23-bse-071


Object oriented language Lab assignment:
Submitted to: Ma’am Samia Zafar.
Activity 01:
package com.mycompany.testing1;

/**
*
* @author Student
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Testing1 extends JFrame{


private JLabel label;
private JButton b;
public Testing1()
{
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
label=new JLabel("HELLO");
this.add(label);
b=new JButton("Toggle");
b.addActionListener(new myHandler());
add(b);
}
class myHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
label.setText("Bye");
}
}

public static void main(String[] args) {

Testing1 f =new Testing1();


f.setTitle("Hi and Bye");
f.setSize(400,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);

}
}
Output:

Activity 2:
package com.mycompany.testing1;
/**
*
* @author Student
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Testing1 extends JFrame {


private JTextField tf1;
private JLabel label;
private JButton b;

public Testing1(){

this.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
tf1=new JTextField(8);
this.add(tf1);
label=new JLabel("New Text");
this.add(label);
b=new JButton("Change");
b.addActionListener(new myHandler());
add(b);
}

class myHandler implements ActionListener{

public void actionPerformed(ActionEvent e)


{
String s=tf1.getText();
label.setText(s);
tf1.setText("");
}
}

public static void main(String[] args) {


Testing1 f=new Testing1();
f.setTitle("Copy Text");
f.setSize(400, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);;
f.setVisible(true);

}
Output:
Activity 3:
package com.mycompany.testing1;

/**
*
* @author Student
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Testing1 extends JFrame {


private JTextField tf1;
private JLabel label;
private JButton b;
private JButton b1;

public Testing1(){

this.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
tf1=new JTextField(8);
this.add(tf1);
label=new JLabel("New Text");
this.add(label);
b=new JButton("Change");
b.addActionListener(new myHandler());
add(b);
b1=new JButton("Disappear");
b1.addActionListener(new myHandler());
add(b1);
}

class myHandler implements ActionListener{


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
String s=tf1.getText();
label.setText(s);
tf1.setText("");
}
if(e.getSource()==b1)
b.setVisible(false);
}
}

public static void main(String[] args) {

Testing1 f=new Testing1();


f.setTitle("Copy Text"); f.setSize(400, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null);;
f.setVisible(true);

}
Output:
Home Activities 1:
package com.mycompany.displaytextbook;

/**
*
* @author Student
*/
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Displaytextbook {

public static class TextFrame extends JFrame {


private JLabel label;
private JTextField textField;
private JButton button;
public TextFrame() {
setTitle("Display Text Example");
label = new JLabel("Enter something:");
textField = new JTextField(20);
button = new JButton("Display Text");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(label);
add(textField);
add(button);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String enteredText = textField.getText();
label.setText("You entered: " + enteredText);
}
});
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

public static void main(String[] args) {


new TextFrame();
}
}
Output:
Home activities 2:
package com.mycompany.radiobuttontest;

/**
*
* @author Student
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RadioButtonTest extends JFrame {


private JTextField textField;
private JRadioButton plainButton;
private JRadioButton boldButton;
private JRadioButton italicButton;
private JRadioButton boldItalicButton;
private ButtonGroup group;

public RadioButtonTest() {
super("RadioButton Test");
setLayout(new FlowLayout());
textField = new JTextField("Watch the font style change", 20);
textField.setFont(new Font("Serif", Font.PLAIN, 14));
add(textField);
plainButton = new JRadioButton("Plain", true);
boldButton = new JRadioButton("Bold", false);
italicButton = new JRadioButton("Italic", false);
boldItalicButton = new JRadioButton("BoldItalic", false);
group = new ButtonGroup();
group.add(plainButton);
group.add(boldButton);
group.add(italicButton);
group.add(boldItalicButton);
add(plainButton);
add(boldButton);
add(italicButton);
add(boldItalicButton);
RadioButtonHandler handler = new RadioButtonHandler();
plainButton.addActionListener(handler);
boldButton.addActionListener(handler);
italicButton.addActionListener(handler);
boldItalicButton.addActionListener(handler);
}
private class RadioButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (plainButton.isSelected()) {
textField.setFont(new Font("Serif", Font.PLAIN, 14));
} else if (boldButton.isSelected()) {
textField.setFont(new Font("Serif", Font.BOLD, 14));
} else if (italicButton.isSelected()) {
textField.setFont(new Font("Serif", Font.ITALIC, 14));
} else if (boldItalicButton.isSelected()) {
textField.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 14));
}
}
}
public static void main(String[] args) {
RadioButtonTest frame = new RadioButtonTest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Output:

You might also like