Exp 9-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

EXP 9

X. Program Code
1. Write a program code to generate the following output.
Code

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

public class X1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Progress Bar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);

JProgressBar progressBar = new JProgressBar(0, 100);


progressBar.setValue(100);
progressBar.setStringPainted(true); // To display the percentage

frame.add(progressBar, BorderLayout.CENTER);
frame.setVisible(true);
}
}

XIII. Exercise
1. Develop a program to demonstrate the use of JProgressBar.
Code

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

public class XIII1 extends JFrame {


public XIII1() {
// Create the frame
setTitle("JProgressBar Example");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a JProgressBar
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(50);
progressBar.setStringPainted(true);
// Add the progress bar to the frame
add(progressBar, BorderLayout.CENTER);
}

public static void main(String[] args) {


XIII1 frame = new XIII1();
frame.setVisible(true);
}
}

2. Write a Program using JProgressBar to show the progress of


Progressbar when user clicks on JButton.
Code

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

public class XIII2 extends JFrame {


private JProgressBar progressBar;
private JButton startButton;
private Timer timer;
private int counter = 0;

public XIII2() {
// Create the frame
setTitle("JProgressBar with Button Example");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

// Create a JProgressBar
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);

// Create a JButton
startButton = new JButton("Start");

// Add ActionListener to the button


startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startProgress();
}
});
// Add components to the frame
add(progressBar, BorderLayout.CENTER);
add(startButton, BorderLayout.SOUTH);
}

private void startProgress() {


// Timer to update the progress bar
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter += 5;
progressBar.setValue(counter);

if (counter >= 100) {


timer.stop();
}
}
});
timer.start();
}

public static void main(String[] args) {


XIII2 frame = new XIII2();
frame.setVisible(true);
}
}
After Click on Start

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy