0% found this document useful (0 votes)
17 views

Swing

The document contains multiple Java Swing applications demonstrating various GUI components and functionalities. These include a rectangle area calculator, a simple calculator, checkbox demos, combo box examples, and color chooser applications. Each problem showcases different aspects of Swing, such as event handling, layout management, and user interaction.

Uploaded by

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

Swing

The document contains multiple Java Swing applications demonstrating various GUI components and functionalities. These include a rectangle area calculator, a simple calculator, checkbox demos, combo box examples, and color chooser applications. Each problem showcases different aspects of Swing, such as event handling, layout management, and user interaction.

Uploaded by

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

Problem1

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class AreaRectangle implements ActionListener{

JTextField lengthField, widthField, resultField;

JButton calculateButton;

AreaRectangle()

// Create JFrame

JFrame frame = new JFrame("Rectangle Area Calculator");

frame.setSize(300, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(null);

// Create JLabels

JLabel lengthLabel = new JLabel("Length:");

lengthLabel.setBounds(20, 20, 60, 20);

JLabel widthLabel = new JLabel("Width:");

widthLabel.setBounds(20, 50, 60, 20);

JLabel resultLabel = new JLabel("Area:");

resultLabel.setBounds(20, 80, 60, 20);

// Create JTextFields

lengthField = new JTextField();


lengthField.setBounds(90, 20, 150, 20);

widthField = new JTextField();

widthField.setBounds(90, 50, 150, 20);

resultField = new JTextField();

resultField.setBounds(90, 80, 150, 20);

resultField.setEditable(false);

// Create JButton

calculateButton = new JButton("Calculate");

calculateButton.setBounds(90, 110, 100, 30);

calculateButton.addActionListener(this);

// Add components to the frame

frame.add(lengthLabel);

frame.add(widthLabel);

frame.add(resultLabel);

frame.add(lengthField);

frame.add(widthField);

frame.add(resultField);

frame.add(calculateButton);

// Make the frame visible

frame.setVisible(true);

public static void main(String[] args) {

new AreaRectangle();

@Override

public void actionPerformed(ActionEvent e) { // Calculate area when the button is clicked

// Calculate area when the button is clicked

if (e.getSource() == calculateButton) {
try {

// Get length and width from text fields

double length = Double.parseDouble(lengthField.getText());

double width = Double.parseDouble(widthField.getText());

// Calculate area

double area = length * width;

// Display result in resultField

resultField.setText(String.format("%.2f", area));

} catch (NumberFormatException ex) {

// Handle invalid input

JOptionPane.showMessageDialog(null, "Please enter valid numbers for length and width.");

} }

Problem2

package packswing;

import java.awt.*;

import javax.swing.*;

public class Border

JFrame f;

Border()

f = new JFrame();

// creating buttons

JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH

JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH

JButton b3 = new JButton("EAST");; // the button will be labeled as EAST


JButton b4 = new JButton("WEST");; // the button will be labeled as WEST

JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER

f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction

f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction

f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction

f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction

f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center

f.setSize(300, 300);

f.setVisible(true);

public static void main(String[] args) {

new Border();

} problem 3

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class ButtonExample {

public static void main(String[] args) {

JFrame f=new JFrame("Button Example");

JButton b1=new JButton("Click Here");

b1.setBounds(50 , 100, 95, 30);


JTextField tf=new JTextField();

tf.setBounds(50,50, 150,20);

f.add(b1);

f.add(tf);

b1.addActionListener(new ActionListener()

@Override

public void actionPerformed(ActionEvent e) {

tf.setText("welcome to disneyland");

});

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

Problem 4

package packswing;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

public class ButtonImage {

ButtonImage()

{
JFrame f=new JFrame("Button Example");

JButton b=new JButton(new


ImageIcon("D:\\eclipse\\JSwing\\src\\packswing\\register.png"));

b.setBounds(100,100,300, 40);

f.add(b);

f.setSize(600,600);

f.setLayout(null);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String[] args) {

new ButtonImage();

Problem5

package packswing;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class CalculatorApp {

// Declare the components

private JFrame frame;

private JTextField textField;

private JButton[] numberButtons;

private JButton[] operatorButtons;

private JButton equalsButton, clearButton;


private String currentOperator = "";

private double firstOperand = 0;

private double secondOperand = 0;

private double result = 0;

public CalculatorApp() {

// Initialize the frame

frame = new JFrame("Simple Calculator");

frame.setSize(400, 500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

// Create the text field to display numbers and results

textField = new JTextField();

frame.add(textField, BorderLayout.NORTH);

// Create the panel to hold buttons

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(4, 4, 10, 10));

// Initialize the number buttons (0-9)

numberButtons = new JButton[10];

for (int i = 0; i < 10; i++) {

numberButtons[i] = new JButton(String.valueOf(i));

numberButtons[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = e.getActionCommand();

textField.setText(textField.getText() + number);

});
}

// Initialize the operator buttons (+, -, *, /)

operatorButtons = new JButton[4];

String[] operators = {"+", "-", "*", "/"};

for (int i = 0; i < 4; i++) {

operatorButtons[i] = new JButton(operators[i]);

operatorButtons[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String operator = e.getActionCommand();

firstOperand = Double.parseDouble(textField.getText());

currentOperator = operator;

textField.setText("");

});

// Initialize the equals button

equalsButton = new JButton("=");

equalsButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

secondOperand = Double.parseDouble(textField.getText());

switch (currentOperator) {

case "+":

result = firstOperand + secondOperand;

break;

case "-":

result = firstOperand - secondOperand;

break;

case "*":
result = firstOperand * secondOperand;

break;

case "/":

if (secondOperand != 0) {

result = firstOperand / secondOperand;

} else {

textField.setText("Error");

return;

break;

textField.setText(String.valueOf(result));

firstOperand = result; // Allow continuous operations

currentOperator = "";

});

// Initialize the clear button (C)

clearButton = new JButton("C");

clearButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText("");

firstOperand = secondOperand = result = 0;

currentOperator = "";

});

// Add number buttons to the panel

for (int i = 1; i <= 9; i++) {

panel.add(numberButtons[i]);
}

panel.add(numberButtons[0]);

// Add operator buttons to the panel

for (JButton operatorButton : operatorButtons) {

panel.add(operatorButton);

// Add equals and clear buttons

panel.add(equalsButton);

panel.add(clearButton);

// Add panel to the frame

frame.add(panel, BorderLayout.CENTER);

// Method to start the application

public void display() {

frame.setVisible(true);

public static void main(String[] args) {

// Create and display the calculator

CalculatorApp calculator = new CalculatorApp();

calculator.display();

Problem6

package packswing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

public class CheckBoxDemo extends JFrame implements ActionListener{

JLabel l;

JCheckBox cb1,cb2,cb3;

JButton b;

CheckBoxDemo(){

l=new JLabel("Food Ordering System");

l.setBounds(50,50,300,20);

cb1=new JCheckBox("Pizza @ 100");

cb1.setBounds(100,100,150,20);

cb2=new JCheckBox("Burger @ 30");

cb2.setBounds(100,150,150,20);

cb3=new JCheckBox("Tea @ 10");

cb3.setBounds(100,200,150,20);

b=new JButton("Order");

b.setBounds(100,250,80,30);

b.addActionListener(this);

add(l);add(cb1);add(cb2);add(cb3);add(b);

setSize(400,400);

setLayout(null);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public static void main(String[] args) {

new CheckBoxDemo();

@Override

public void actionPerformed(ActionEvent e) {

float amount=0;

String msg="";

if(cb1.isSelected()){

amount+=100;

msg="Pizza: 100\n";

if(cb2.isSelected()){

amount+=30;

msg+="Burger: 30\n";

if(cb3.isSelected()){

amount+=10;

msg+="Tea: 10\n";

msg+="-----------------\n";

JOptionPane.showMessageDialog(this,msg+"Total: "+amount);

Problem 7

package packswing;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;
import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class CheckBoxExample {

CheckBoxExample(){

JFrame f= new JFrame("CheckBox Example");

JLabel label = new JLabel();

label.setHorizontalAlignment(JLabel.CENTER);

label.setSize(400,100);

JCheckBox checkbox1 = new JCheckBox("C++");

checkbox1.setBounds(150,100, 50,50);

JCheckBox checkbox2 = new JCheckBox("Java");

checkbox2.setBounds(150,150, 50,50);

f.add(checkbox1); f.add(checkbox2); f.add(label);

checkbox1.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

label.setText("C++ Checkbox: " +

(e.getStateChange() == ItemEvent.SELECTED ? "checked" : "unchecked"));

});

checkbox2.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

label.setText("Java Checkbox: " +

(e.getStateChange() == ItemEvent.SELECTED ? "checked" : "unchecked"));

});

f.setSize(400,400);
f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

new CheckBoxExample();

Problem8

package packswing;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JColorChooser;

import javax.swing.JFrame;

public class ColorChooserEx extends JFrame implements ActionListener{

JButton b;

Container c;

ColorChooserEx(){

c=getContentPane();

c.setLayout(new FlowLayout());

b=new JButton("color");

b.addActionListener(this);

c.add(b);
}

public void actionPerformed(ActionEvent e) {

Color initialcolor=Color.RED;

Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);

c.setBackground(color);

public static void main(String[] args) {

ColorChooserEx ch=new ColorChooserEx();

ch.setSize(400,400);

ch.setVisible(true);

ch.setDefaultCloseOperation(EXIT_ON_CLOSE);

Problem9

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class ComboBoxDemo implements ActionListener {

JFrame f; JComboBox cb; JLabel label;

ComboBoxDemo(){

f=new JFrame("ComboBox Example");

label = new JLabel();

label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);

JButton b=new JButton("Show");

b.setBounds(200,100,75,20);

String languages[]={"C","C++","C#","Java","PHP"};

cb=new JComboBox(languages);

cb.setBounds(50, 100,90,20);

b.addActionListener(this);

f.add(cb); f.add(label); f.add(b);

f.setLayout(null);

f.setSize(350,350);

f.setVisible(true);

public static void main(String[] args) {

new ComboBoxDemo();

@Override

public void actionPerformed(ActionEvent e) {

String data = "Programming language Selected: "

+ cb.getItemAt(cb.getSelectedIndex());

label.setText(data);

Problem10

package packswing;

import javax.swing.JComboBox;

import javax.swing.JFrame;
public class ComboBoxExample {

JFrame f;

ComboBoxExample(){

f=new JFrame("ComboBox Example");

String country[]={"India","Aus","U.S.A","England","Newzealand"};

JComboBox cb=new JComboBox(country);

cb.setBounds(50, 50,90,20);

f.add(cb);

f.setLayout(null);

f.setSize(400,500);

f.setVisible(true);

public static void main(String[] args) {

new ComboBoxExample();

Problem11

package packswing;

import javax.swing.JButton;

import javax.swing.JFrame;

//We can also write all the codes of creating JFrame,

//JButton and method call inside the java constructor.

public class Demo {

JFrame f;

Demo()

f=new JFrame();

JButton b=new JButton("click");


b.setBounds(130,100,100, 40);

f.add(b);

f.setSize(400,500);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

Demo d=new Demo();

Problem 12

//We can also inherit the JFrame class,

//so there is no need to create the instance of JFrame class explicitly.

package packswing;

import javax.swing.JButton;

import javax.swing.JFrame;

public class DemoSwing extends JFrame{

JFrame f;

DemoSwing()

JButton b=new JButton("Click!!!");

add(b);

setSize(400,500);

b.setSize(100, 100);

b.setBounds(130,100,100, 40);

setLayout(null);
setVisible(true);

public static void main(String[] args) {

DemoSwing d=new DemoSwing();

Problem 13

package packswing;

import javax.swing.*;

public class FirstSwingExample {

public static void main(String[] args) {

JFrame f=new JFrame();

JButton b=new JButton("click");

b.setBounds(130,100,100, 40);

f.add(b);

f.setSize(400,500);

f.setLayout(null);

f.setVisible(true);

Problem 14

package packswing;

//import statements

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

public class GridLayoutExample

JFrame frameObj;

//constructor

GridLayoutExample()

frameObj = new JFrame();

//creating 9 buttons

JButton btn1 = new JButton("1");

JButton btn2 = new JButton("2");

JButton btn3 = new JButton("3");

JButton btn4 = new JButton("4");

JButton btn5 = new JButton("5");

JButton btn6 = new JButton("6");

JButton btn7 = new JButton("7");

JButton btn8 = new JButton("8");

JButton btn9 = new JButton("9");

//adding buttons to the frame

//since, we are using the parameterless constructor, therfore;

//the number of columns is equal to the number of buttons we

//are adding to the frame. The row count remains one.

frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);

frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);

frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);

//setting the grid layout using the parameterless constructor


frameObj.setLayout(new GridLayout());

frameObj.setSize(300, 300);

frameObj.setVisible(true);

//main method

public static void main(String argvs[])

new GridLayoutExample();

Problem 15

package packswing;

//import statements

import java.awt.*;

import javax.swing.*;

public class GridLayoutExample1

JFrame frameObj;

//constructor

GridLayoutExample1()

frameObj = new JFrame();

//creating 9 buttons
JButton btn1 = new JButton("1");

JButton btn2 = new JButton("2");

JButton btn3 = new JButton("3");

JButton btn4 = new JButton("4");

JButton btn5 = new JButton("5");

JButton btn6 = new JButton("6");

JButton btn7 = new JButton("7");

JButton btn8 = new JButton("8");

JButton btn9 = new JButton("9");

//adding buttons to the frame

//since, we are using the parameterless constructor, therefore;

//the number of columns is equal to the number of buttons we

//are adding to the frame. The row count remains one.

frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);

frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);

frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);

//setting the grid layout

//a 3 * 3 grid is created with the horizontal gap 20

//and vertical gap 25

frameObj.setLayout(new GridLayout(3, 3, 20, 25));

frameObj.setSize(300, 300);

frameObj.setVisible(true);

//main method

public static void main(String argvs[])

new GridLayoutExample1();

Problem 16
package packswing;

import javax.swing.JFileChooser;

public class HelloWorld {

public static void main(String[] args) {

JFileChooser jf = new JFileChooser("c:"); // parameterised constructor JFileChooser(


File currentDirectory) is called.

jf.showSaveDialog(null); // opening the saved dialogue

Problem17

package packswing;

import java.awt.Frame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.net.UnknownHostException;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JTextField;

public class LabelDemo extends Frame implements ActionListener {

JTextField tf; JLabel l; JButton b;

LabelDemo(){

tf=new JTextField();

tf.setBounds(50,50, 150,20);

l=new JLabel();

l.setBounds(50,100, 250,20);

b=new JButton("Find IP");

b.setBounds(50,150,95,30);

b.addActionListener(this);
add(b);add(tf);add(l);

setSize(400,400);

setLayout(null);

setVisible(true);

public static void main(String[] args) {

new LabelDemo();

@Override

public void actionPerformed(ActionEvent e) {

String host=tf.getText(); String ip ;

try {

ip = java.net.InetAddress.getByName(host).getHostAddress();

l.setText("IP of "+host+" is: "+ip);

} catch (UnknownHostException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}}

Problem 18

package packswing;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class LabelExample {


public static void main(String[] args) {

JFrame f= new JFrame("Label Example");

JLabel l1,l2;

l1=new JLabel("First Label:");

l1.setBounds(50,50, 100,30);

l2=new JLabel("Second Label:");

l2.setBounds(50,100, 100,30);

f.add(l1); f.add(l2);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

Problem 19

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

public class ListExample implements ActionListener{

JList<String> list1;JList<String> list2;JLabel label;

ListExample(){

JFrame f= new JFrame();

label= new JLabel();


label.setSize(500,100);

JButton b=new JButton("Show");

b.setBounds(200,150,80,30);

DefaultListModel<String> l1 = new DefaultListModel<>();

l1.addElement("C");

l1.addElement("C++");

l1.addElement("Java");

l1.addElement("PHP");

list1= new JList<>(l1);

list1.setBounds(100,100, 75,75);

DefaultListModel<String> l2 = new DefaultListModel<>();

l2.addElement("Turbo C++");

l2.addElement("Struts");

l2.addElement("Spring");

l2.addElement("YII");

list2 = new JList<>(l2);

list2.setBounds(100,200, 75,75);

f.add(list1); f.add(list2); f.add(b); f.add(label);

b.addActionListener(this);

f.setSize(450,450);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

new ListExample();

@Override

public void actionPerformed(ActionEvent e) {

String data = "";


if (list1.getSelectedIndex() != -1) {

data = "Programming language Selected: " + list1.getSelectedValue();

label.setText(data);

if(list2.getSelectedIndex() != -1){

data += ", FrameWork Selected: ";

for(Object frame :list2.getSelectedValues()){

data += frame + " ";

label.setText(data);

Problem20

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class LoginForm {

public static void main(String[] args) {


JFrame frame = new JFrame("Login Form");

// Create a panel to hold components

JPanel panel = new JPanel();

// Create text fields for username and password

JTextField usernameField = new JTextField(20);

JPasswordField passwordField = new JPasswordField(20);

// Create a button for login

JButton loginButton = new JButton("Login");

// Add components to the panel

panel.add(new JLabel("Username: "));

panel.add(usernameField);

panel.add(new JLabel("Password: "));

panel.add(passwordField);

panel.add(loginButton);

frame.add(panel);

loginButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// Retrieve username and password entered by the user

String username = usernameField.getText();

String password = new String(passwordField.getPassword());

// Validate username and password

if (username.equals("admin") && password.equals("password")) {

// Show a success message if login is successful

JOptionPane.showMessageDialog(frame, "Login successful!");

} else {

// Show an error message if login fails

JOptionPane.showMessageDialog(frame, "Invalid username or password");

}
});

// Set frame properties

frame.setSize(300, 150);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

Problem 21

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JTextArea;

public class MenuDemo implements ActionListener{

JFrame f;

JMenuBar mb;

JMenu file,edit,help;

JMenuItem cut,copy,paste,selectAll;

JTextArea ta;

MenuDemo(){

f=new JFrame();
cut=new JMenuItem("cut");

copy=new JMenuItem("copy");

paste=new JMenuItem("paste");

selectAll=new JMenuItem("selectAll");

cut.addActionListener(this);

copy.addActionListener(this);

paste.addActionListener(this);

selectAll.addActionListener(this);

mb=new JMenuBar();

file=new JMenu("File");

edit=new JMenu("Edit");

help=new JMenu("Help");

edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);

mb.add(file);mb.add(edit);mb.add(help);

ta=new JTextArea();

ta.setBounds(5,5,360,320);

f.add(mb);f.add(ta);

f.setJMenuBar(mb);

f.setLayout(null);

f.setSize(400,400);

f.setVisible(true);

public void actionPerformed(ActionEvent e) {

if(e.getSource()==cut)

ta.cut();

if(e.getSource()==paste)

ta.paste();

if(e.getSource()==copy)

ta.copy();

if(e.getSource()==selectAll)

ta.selectAll();
}

public static void main(String[] args) {

new MenuDemo();

Problem 22

package packswing;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

public class MenuExample {

JMenu menu, submenu;

JMenuItem i1, i2, i3, i4, i5;

JFrame f;JMenuBar mb;

MenuExample(){

f= new JFrame("Menu and MenuItem Example");

mb=new JMenuBar();

menu=new JMenu("Menu");

submenu=new JMenu("Sub Menu");

i1=new JMenuItem("Item 1");

i2=new JMenuItem("Item 2");

i3=new JMenuItem("Item 3");

i4=new JMenuItem("Item 4");

i5=new JMenuItem("Item 5");

menu.add(i1); menu.add(i2); menu.add(i3);

submenu.add(i4); submenu.add(i5);

menu.add(submenu);

mb.add(menu);
f.setJMenuBar(mb);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

new MenuExample();

Problem 23

package packswing;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class OptionPaneDemo extends WindowAdapter{

JFrame f;

OptionPaneDemo(){

f=new JFrame();

f.addWindowListener(this);

f.setSize(300, 300);

f.setLayout(null);

f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

f.setVisible(true);

public void windowClosing(WindowEvent e) {

int a=JOptionPane.showConfirmDialog(f,"Are you sure?");


if(a==JOptionPane.YES_OPTION){

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String[] args) {

new OptionPaneDemo();

Problem 24

package packswing;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class OptionPaneExample {

JFrame f;

OptionPaneExample(){

f=new JFrame();

JOptionPane.showMessageDialog(f,"Hello, Welcome to Javatpoint.");

JOptionPane.showMessageDialog(f,"Successfully
Updated.","Alert",JOptionPane.WARNING_MESSAGE);

String name=JOptionPane.showInputDialog(f,"Enter Name");

public static void main(String[] args) {

new OptionPaneExample();

}
}

Problem 25

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class PasswordFieldDemo implements ActionListener{

JLabel username,password;

JTextField user,pass;

JButton login;

JLabel label = new JLabel();

PasswordFieldDemo()

JFrame f=new JFrame();

username=new JLabel("UserName:");

username.setBounds(30, 90,120, 20);

user=new JTextField();

user.setBounds(110, 90, 120, 20);

password=new JLabel("Password:");

password.setBounds(30, 120,120, 20);

pass=new JPasswordField();
pass.setBounds(110,120, 120, 20);

login=new JButton("Login");

login.setBounds(110, 150, 120, 30);

login.addActionListener(this);

label.setBounds(30,180, 250,50);

f.add(username);

f.add(user);

f.add(password);

f.add(pass);

f.add(login);

f.add(label);

f.setLayout(null);

f.setSize(400,400);

f.setVisible(true);

public static void main(String[] args) {

new PasswordFieldDemo();

@Override

public void actionPerformed(ActionEvent e) {

String data = "Username " + user.getText();

data += ", Password: "

+ new String(((JPasswordField) pass).getPassword());

label.setText(data);

Problem 26
package packswing;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

public class PasswordFieldExample {

public static void main(String[] args) {

JFrame f=new JFrame("Password Field Example");

JPasswordField value = new JPasswordField();

JLabel l1=new JLabel("Password:");

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

value.setBounds(100,100,100,30);

f.add(value); f.add(l1);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

Problem 27

package packswing;

import javax.swing.JFrame;

import javax.swing.JProgressBar;

public class ProgressBarExample extends JFrame{

JProgressBar jb;

int i=0,num=0;
ProgressBarExample(){

jb=new JProgressBar(0,2000);

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(250,150);

setLayout(null);

public void iterate(){

while(i<=2000){

jb.setValue(i);

i=i+20;

try{Thread.sleep(150);}catch(Exception e){}

public static void main(String[] args) {

ProgressBarExample m=new ProgressBarExample();

m.setVisible(true);

m.iterate();

Problem 28

package packswing;

import java.awt.Component;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JRadioButton;

public class RadioButtonExample extends JFrame implements ActionListener{

JRadioButton rb1,rb2;

JButton b;

RadioButtonExample(){

rb1=new JRadioButton("Male");

rb1.setBounds(100,50,100,30);

rb2=new JRadioButton("Female");

rb2.setBounds(100,100,100,30);

ButtonGroup bg=new ButtonGroup();

bg.add(rb1);bg.add(rb2);

b=new JButton("click");

b.setBounds(100,150,80,30);

b.addActionListener(this);

add(rb1);add(rb2);add(b);

setSize(300,300);

setLayout(null);

setVisible(true);

public static void main(String[] args) {

new RadioButtonExample();

@Override

public void actionPerformed(ActionEvent e) {

if(rb1.isSelected()){

JOptionPane.showMessageDialog(this,"You are Male.");


}

if(rb2.isSelected()){

JOptionPane.showMessageDialog(this,"You are Female.");

Problem 29

package packswing;

import java.awt.GridLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JSeparator;

public class SeparatorExample {

public static void main(String[] args) {

JFrame f = new JFrame("Separator Example");

f.setLayout(new GridLayout(0, 1));

JLabel l1 = new JLabel("Above Separator");

f.add(l1);

JSeparator sep = new JSeparator();

f.add(sep);

JLabel l2 = new JLabel("Below Separator");

f.add(l2);

f.setSize(400, 100);
f.setVisible(true);

}}

Problem 30

package packswing;

import javax.swing.*;

public class SliderExample extends JFrame{

public SliderExample() {

JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);

slider.setMinorTickSpacing(2);

slider.setMajorTickSpacing(10);

slider.setPaintTicks(true);

slider.setPaintLabels(true);

JPanel panel=new JPanel();

panel.add(slider);

add(panel);

public static void main(String s[]) {

SliderExample frame=new SliderExample();

frame.pack();

frame.setVisible(true);

Problem 31

package packswing;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

public class TableDemo {

public static void main(String[] args) {

JFrame f = new JFrame("Table Example");

String data[][]={ {"101","Amit","670000"},

{"102","Jai","780000"},

{"101","Sachin","700000"}

};

String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);

jt.setCellSelectionEnabled(true);

ListSelectionModel select= jt.getSelectionModel();

select.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

select.addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {

String Data = null;

int[] row = jt.getSelectedRows();

int[] columns = jt.getSelectedColumns();

for (int i = 0; i < row.length; i++) {

for (int j = 0; j < columns.length; j++) {

Data = (String) jt.getValueAt(row[i], columns[j]);

}}

System.out.println("Table element selected is: " + Data);

});

JScrollPane sp=new JScrollPane(jt);

f.add(sp);

f.setSize(300, 200);
f.setVisible(true);

Problem 32

package packswing;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

public class TableExample {

JFrame f;

TableExample(){

f=new JFrame();

String data[][]={ {"101","Amit","670000"},

{"102","Jai","780000"},

{"103","Sachin","700000"}};

String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);

jt.setBounds(30,40,200,300);

JScrollPane sp=new JScrollPane(jt);

f.add(sp);

f.setSize(300,400);

f.setVisible(true);

public static void main(String[] args) {

new TableExample();

}
}

Problem 33

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextArea;

public class TextAreaDemo implements ActionListener {

JLabel l1,l2;

JTextArea area;

JButton b;

TextAreaDemo() {

JFrame f= new JFrame();

l1=new JLabel();

l1.setBounds(50,25,100,30);

l2=new JLabel();

l2.setBounds(160,25,100,30);

area=new JTextArea();

area.setBounds(20,75,250,200);

b=new JButton("Count Words");

b.setBounds(100,300,120,30);

b.addActionListener(this);

f.add(l1);f.add(l2);f.add(area);f.add(b);

f.setSize(450,450);

f.setLayout(null);

f.setVisible(true);
}

public static void main(String[] args) {

new TextAreaDemo();

@Override

public void actionPerformed(ActionEvent e) {

String text=area.getText();

String words[]=text.split("\\s");

l1.setText("Words: "+words.length);

l2.setText("Characters: "+text.length());

Problem 34

package packswing;

import javax.swing.JFrame;

import javax.swing.JTextArea;

public class TextAreaExample {

TextAreaExample()

JFrame f= new JFrame("Text Area Demo");

JTextArea area=new JTextArea("Welcome to world of python");

area.setBounds(10,30, 200,200);

f.add(area);

f.setSize(300,300);
f.setLayout(null);

f.setVisible(true);

public static void main(String[] args) {

new TextAreaExample();

Problem 35

package packswing;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class TextFieldExample {

public static void main(String[] args) {

// Creating a JFrame object with title "TextField Example."

JFrame f= new JFrame("TextField Example");

// Creating two JTextField objects

JTextField t1, t2;

// Initializing the first JTextField with default text "Welcome to Javatpoint."

t1 = new JTextField("Welcome to Javatpoint.");

// Setting the position and size of the first JTextField

t1.setBounds(50,100, 200,30);

// Initializing the second JTextField with default text "AWT Tutorial"

t2 = new JTextField("AWT Tutorial");

// Setting the position and size of the second JTextField

t2.setBounds(50,150, 200,30);

// Adding JTextFields to the JFrame

f.add(t1);

f.add(t2);
// Setting the size of the JFrame

f.setSize(400,400);

// Setting layout to null to use absolute positioning

f.setLayout(null);

// Making the JFrame visible

f.setVisible(true);

Problem 36

package packswing;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class TextFieldExample2 implements ActionListener{

JTextField tf1, tf2, tf3;

JButton b1, b2;

// Constructor

TextFieldExample2() {

// Creating a JFrame object

JFrame f = new JFrame();

// Creating JTextField objects

tf1 = new JTextField();

tf1.setBounds(50, 50, 150, 20);

tf2 = new JTextField();

tf2.setBounds(50, 100, 150, 20);


tf3 = new JTextField();

tf3.setBounds(50, 150, 150, 20);

// Making tf3 non-editable

tf3.setEditable(false);

// Creating JButton objects

b1 = new JButton("+");

b1.setBounds(50, 200, 50, 50);

b2 = new JButton("-");

b2.setBounds(120, 200, 50, 50);

// Adding ActionListener to buttons

b1.addActionListener(this);

b2.addActionListener(this);

// Adding components to the JFrame

f.add(tf1);

f.add(tf2);

f.add(tf3);

f.add(b1);

f.add(b2);

// Setting JFrame size and layout

f.setSize(300, 300);

f.setLayout(null);

// Making JFrame visible

f.setVisible(true);

public static void main(String[] args) {

new TextFieldExample2();

@Override

public void actionPerformed(ActionEvent e) {


// Retrieving text from text fields

String s1 = tf1.getText();

String s2 = tf2.getText();

// Converting string inputs to integers

int a = Integer.parseInt(s1);

int b = Integer.parseInt(s2);

// Variable to hold the result

int c = 0;

// Checking which button is clicked

if (e.getSource() == b1) {

c = a + b; // Addition

} else if (e.getSource() == b2) {

c = a - b; // Subtraction

// Converting result back to a string

String result = String.valueOf(c);

// Setting the result in the third text field

tf3.setText(result);

}}

Problem 37

package packswing;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTextArea;
public class WCC extends JFrame implements ActionListener{

JTextArea ta;

JButton b1,b2;

WCC(){

super("Word Character Counter - JavaTpoint");

ta=new JTextArea();

ta.setBounds(50,50,300,200);

ta.setBackground(Color.CYAN);

b1=new JButton("Word");

b1.setBounds(50,300,100,30);

b2=new JButton("Character");

b2.setBounds(180,300,100,30);

b1.addActionListener(this);

b2.addActionListener(this);

add(b1);add(b2);add(ta);

setSize(400,400);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e){

String text=ta.getText();

if(e.getSource()==b1){

String words[]=text.split("\\s");

JOptionPane.showMessageDialog(this,"Total words: "+words.length);

if(e.getSource()==b2){

JOptionPane.showMessageDialog(this,"Total Characters with space: "+text.length());


}

public static void main(String[] args) {

new WCC();

1. Calculator
🧮
📋
📝
🎨
⏱❌

🕒🔹
📂
input
division.
multiplication,
subtraction,
addition,
performs
that
calculator
operations.
To
Array
an
tasks
Stores
tasks.
remove
add
can
manager
task
time
update
javax.swing.Timer
buttons.
reset
stop,
start,
application
timer
Stopwatch
4.
saving/loading
writing
JTextArea
functionality.
edit
save,
open,
with
editor
text
Notepad
3.
database.
of
instead
Picker
Color
5.
RG
its
displays
pick
users
JCombo
Kelvin.
Fahrenheit,
Celsius,
between
Converts
Converter
Temperature
6.
color
JColorChooser
value.
a3x3
game.
player
two
simple
Toe
Tac
Tic
7.
input/output.
JTextField
selection
unit
computer.
against
plays
user
where
Game
Scissors
Paper
Rock
8.
Java.
in
logic
game
grid
9.
moves.
AI
Math.random()
choices
JClock
second.
every
updating
time,
current
the
javax.swing.Timer.
File
selection.
for
JFileChooser
Uses
files.
view
and
open
to
browser
file
basic
A ets
ist
xplorer
.isplays
0.
oabel
igital
abel.
utton
uttons
/Hex
ist ox

You might also like