Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

java.lab

The document contains multiple Java Swing applications demonstrating various GUI components such as JFrame, JLabel, JButton, JTextField, JTextArea, and JPanel. Each example illustrates the creation of a simple window with specific functionalities, including displaying text, handling button clicks, and performing basic arithmetic operations. The code snippets are organized into separate classes, each showcasing different aspects of Java Swing programming.

Uploaded by

Appu K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java.lab

The document contains multiple Java Swing applications demonstrating various GUI components such as JFrame, JLabel, JButton, JTextField, JTextArea, and JPanel. Each example illustrates the creation of a simple window with specific functionalities, including displaying text, handling button clicks, and performing basic arithmetic operations. The code snippets are organized into separate classes, each showcasing different aspects of Java Swing programming.

Uploaded by

Appu K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1)1st FRAME

package ajaybca;
import javax.swing.*;
public class NewMain {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setTitle("1st Frame");
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

2)Textdemo

package ajaybca;
import java.awt.*;
import javax.swing.*;
public class Textdemo extends JFrame {
public Textdemo(){
setTitle("First Frame");
setSize(300,300);
setLayout(null);
repaint();
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawString("Welcome to Strings", 50, 50);
}
public static void main(String[] args) {
Textdemo f=new Textdemo();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
3)LABELIMAGE

package ajaybca;
import java.awt.*;
import javax.swing.*;
public class labelimage {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setTitle("First Frame");
f.setSize(300, 300);
f.setLayout(new FlowLayout());
ImageIcon i=new
ImageIcon("C:\\Users\\my.ac.u3bca23206\\Downloads\\vlzg1js1jbz71.jpg");
JLabel label=new JLabel("ABC Pvt Ltd",i,JLabel.CENTER);
f.add(label);
f.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

4)ANIMALS

package ajaybca;
import java.awt.*;
import javax.swing.*;
public class labelimage {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setTitle("First Frame");
f.setSize(300, 300);
f.setLayout(new FlowLayout());
ImageIcon i1=new
ImageIcon("C:\\Users\\my.ac.u3bca23206\\Downloads\\1istockphoto-1356481088-61
2x612.jpg");
ImageIcon i2=new
ImageIcon("C:\\Users\\my.ac.u3bca23206\\Downloads\\2istockphoto-1356481088-61
2x612.jpg");
JLabel label=new JLabel ("Animals");
label.setBounds(10,50,100,40);
JLabel label1=new JLabel("EAGLE",i1,JLabel.CENTER);
label.setBounds(10,50,100,40);
JLabel label2=new JLabel("LION",i2,JLabel.CENTER);
label.setBounds(150,50,100,40);
f.add(label);
f.add(label1);
f.add(label2);
f.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

5)JPANEL

package ajaybca;
import java.awt.*;
import javax.swing.*;
public class jpanel {
public static void main(String[] args) {
JFrame frame = new JFrame("JPanel Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
frame.setVisible(true);
}
}

6)JTEXTFIELD
package ajaybca;
import java.awt.*;
import javax.swing.*;
public class jtf {
public static void main(String[] args) {
JFrame frame = new JFrame("Jtextfield demo");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JTextField textField = new JTextField();
textField.setBounds(50,50,200,30);
textField.setText("Enter your Name");
frame.add(textField);
frame.setVisible(true);
}
}

7)JLABEL PRGM
package ajaybca;
import java.awt.*;
import javax.swing.*;
public class frmprg {
frmprg(){
JFrame f = new JFrame("JLabel Prg Example");
JLabel l1 = new JLabel("1stName");
l1.setBounds(100,100,150,50);
f.add(l1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new frmprg();
}

8)JBUTTON
package ajaybca;
import java.awt.*;
import javax.swing.*;
public class jb {
jb(){
JFrame f = new JFrame("JButton Eg");
JButton b1 = new JButton("Add");
b1.setBounds(100,100,150,50);
f.add(b1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new jb();
}

9)ADD

package ajaybca;

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

public class add extends JFrame {


JLabel l1, l2, l3;
JTextField tf1, tf2;
JButton b;

add() {
setSize(500, 500);
setLayout(null);

l1 = new JLabel("No 1 : ");


l1.setBounds(20, 30, 100, 30);
add(l1);

tf1 = new JTextField();


tf1.setBounds(130, 30, 150, 30);
add(tf1);

l2 = new JLabel("No 2 : ");


l2.setBounds(20, 80, 100, 30);
add(l2);

tf2 = new JTextField();


tf2.setBounds(130, 80, 150, 30);
add(tf2);

b = new JButton("Add");
b.setBounds(130, 130, 100, 30);
add(b);

l3 = new JLabel("Result: ");


l3.setBounds(20, 180, 200, 30);
add(l3);

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(tf1.getText());
int num2 = Integer.parseInt(tf2.getText());
int sum = num1 + num2;
l3.setText("Result: " + sum);
} catch (NumberFormatException ex) {
l3.setText("Invalid input");
}
}
});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {


new add();
}
}

TEXTAREA

package ajaybca;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class txtarea {


public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

final JTextArea textArea = new JTextArea(5, 20);


JButton button = new JButton("Show Text");
final JLabel label = new JLabel("Enter something:");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("<html>You entered: <br>" + textArea.getText().replace("\n",
"<br>") + "</html>");
}
});

frame.add(new JScrollPane(textArea));
frame.add(button);
frame.add(label);

frame.setVisible(true);
}
}

You might also like