0% found this document useful (0 votes)
11 views5 pages

SetText&GetText Example

Uploaded by

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

SetText&GetText Example

Uploaded by

bibriandave7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LAB_ACT1:

import [Link].*;
import [Link].*;
public class TextGetSetExample extends JFrame implements ActionListener {
private JTextField textField;
private JLabel label;
public TextGetSetExample() {
super("Text Get/Set Example");
textField = new JTextField(20);
label = new JLabel("Enter your name: ");
JButton button = new JButton("Set Greeting");

[Link](this);

JPanel panel = new JPanel();


[Link](label);
[Link](textField);
[Link](button);
add(panel);

pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = [Link]();
[Link]("Hello, " + name + "!");
}

public static void main(String[] args) {


new TextGetSetExample();
}
}
LAB_ACT2:
import javax. swing.*;
import [Link].*;
public class NumberAdder extends JFrame implements ActionListener {
private JTextField num1Field;
private JTextField num2Field;
private JLabel resultLabel;

public NumberAdder() {
super("Number Adder");

num1Field = new JTextField(5);


num2Field = new JTextField(5);

resultLabel = new JLabel("Result: ");

JButton addButton = new JButton("Add");

[Link](this);

JPanel panel = new JPanel();


[Link](new JLabel("Number 1: "));
[Link](num1Field);
[Link](new JLabel("Number 2: "));
[Link](num2Field);
[Link](addButton);
[Link](resultLabel);
add(panel);

pack();
setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
String text1 = [Link]();
String text2 = [Link]();

try {
int num1 = [Link](text1);
int num2 = [Link](text2);
int sum = num1 + num2;
[Link]("Result: " + sum);
} catch (NumberFormatException ex) {
[Link]("Error: Please enter valid numbers.");
}
}

public static void main(String[] args) {


new NumberAdder();
}
}
LAB_ACT3:
package javaapplication16;
import [Link].*;
import [Link].*;

public class JavaApplication16 extends Frame {


private Label lblCount;
private TextField tfCount;
private Button btnCount;
private int count = 0;

public JavaApplication16() {
setLayout(new FlowLayout());

lblCount = new Label("Counter");


add(lblCount);

tfCount = new TextField(count + "", 10);


[Link](false);
add(tfCount);

btnCount = new Button("Count");


add(btnCount);

[Link](new BtnCountListener());

setTitle("AWT Counter");
setSize(300, 100);

setVisible(true);

public static void main(String[] args) {


JavaApplication16 app = new JavaApplication16();

private class BtnCountListener implements ActionListener {


@Override
public void actionPerformed(ActionEvent evt) {
++count;

[Link](count + "");
}
}
}
LAB_ACT4:
package javaapplication17;
import [Link].*;
import [Link].*;

public class JavaApplication17 extends Frame {


private TextField tfInput;
private TextField tfOutput;
private int sum = 0;

public JavaApplication17() {
setLayout(new GridLayout(2, 2));

add(new Label("Enter an Integer: "));

tfInput = new TextField(10);


add(tfInput);

[Link](new TFInputListener());

add(new Label("The Accumulated Sum is: "));

tfOutput = new TextField(10);


[Link](false);
add(tfOutput);

setTitle("AWT Accumulator");
setSize(350, 120);
setVisible(true);
}

public static void main(String[] args) {


new JavaApplication17();
}

private class TFInputListener implements ActionListener {


@Override
public void actionPerformed(ActionEvent evt) {
int numberIn = [Link]([Link]());
sum += numberIn;
[Link]("");
[Link](sum + "");
}
}
}
LAB_ACT5:

package javaapplication18;
import [Link].*;
import [Link].*;

public class JavaApplication18 extends Frame {

public JavaApplication18() {
super("Drawing Example");
setSize(400, 400);
setVisible(true);
}

@Override
public void paint(Graphics g) {
[Link](g);
[Link]([Link]);
[Link](50, 50, 100, 100);
[Link]([Link]);
[Link](new Font("Arial", [Link], 16));
[Link]("Hello, AWT Graphics!", 150, 150);
}

public static void main(String[] args) {


new JavaApplication18();
}
}

You might also like